|
@@ -17,7 +17,6 @@ func (r *epsilonHostPoolResponse) Mark(err error) {
|
|
|
r.ended = time.Now()
|
|
r.ended = time.Now()
|
|
|
doMark(err, r)
|
|
doMark(err, r)
|
|
|
})
|
|
})
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type epsilonGreedyHostPool struct {
|
|
type epsilonGreedyHostPool struct {
|
|
@@ -26,6 +25,7 @@ type epsilonGreedyHostPool struct {
|
|
|
decayDuration time.Duration
|
|
decayDuration time.Duration
|
|
|
EpsilonValueCalculator // embed the epsilonValueCalculator
|
|
EpsilonValueCalculator // embed the epsilonValueCalculator
|
|
|
timer
|
|
timer
|
|
|
|
|
+ quit chan bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Construct an Epsilon Greedy HostPool
|
|
// Construct an Epsilon Greedy HostPool
|
|
@@ -54,6 +54,7 @@ func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonV
|
|
|
decayDuration: decayDuration,
|
|
decayDuration: decayDuration,
|
|
|
EpsilonValueCalculator: calc,
|
|
EpsilonValueCalculator: calc,
|
|
|
timer: &realTimer{},
|
|
timer: &realTimer{},
|
|
|
|
|
+ quit: make(chan bool),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// allocate structures
|
|
// allocate structures
|
|
@@ -65,6 +66,11 @@ func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonV
|
|
|
return p
|
|
return p
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func (p *epsilonGreedyHostPool) Close() {
|
|
|
|
|
+ // No need to do p.quit <- true as close(p.quit) does the trick.
|
|
|
|
|
+ close(p.quit)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func (p *epsilonGreedyHostPool) SetEpsilon(newEpsilon float32) {
|
|
func (p *epsilonGreedyHostPool) SetEpsilon(newEpsilon float32) {
|
|
|
p.Lock()
|
|
p.Lock()
|
|
|
defer p.Unlock()
|
|
defer p.Unlock()
|
|
@@ -83,10 +89,15 @@ func (p *epsilonGreedyHostPool) SetHosts(hosts []string) {
|
|
|
|
|
|
|
|
func (p *epsilonGreedyHostPool) epsilonGreedyDecay() {
|
|
func (p *epsilonGreedyHostPool) epsilonGreedyDecay() {
|
|
|
durationPerBucket := p.decayDuration / epsilonBuckets
|
|
durationPerBucket := p.decayDuration / epsilonBuckets
|
|
|
- ticker := time.Tick(durationPerBucket)
|
|
|
|
|
|
|
+ ticker := time.NewTicker(durationPerBucket)
|
|
|
for {
|
|
for {
|
|
|
- <-ticker
|
|
|
|
|
- p.performEpsilonGreedyDecay()
|
|
|
|
|
|
|
+ select {
|
|
|
|
|
+ case <-p.quit:
|
|
|
|
|
+ ticker.Stop()
|
|
|
|
|
+ return
|
|
|
|
|
+ case <-ticker.C:
|
|
|
|
|
+ p.performEpsilonGreedyDecay()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
func (p *epsilonGreedyHostPool) performEpsilonGreedyDecay() {
|
|
func (p *epsilonGreedyHostPool) performEpsilonGreedyDecay() {
|