Skip to content

Commit

Permalink
Merge pull request #7 from wux/squash-add-close
Browse files Browse the repository at this point in the history
Add 'Close' function to properly release all resources for a host pool.
  • Loading branch information
jehiah committed Feb 18, 2015
2 parents 8c2964d + 8fedab7 commit e83c9e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
19 changes: 15 additions & 4 deletions epsilon_greedy.go
Expand Up @@ -17,7 +17,6 @@ func (r *epsilonHostPoolResponse) Mark(err error) {
r.ended = time.Now()
doMark(err, r)
})

}

type epsilonGreedyHostPool struct {
Expand All @@ -26,6 +25,7 @@ type epsilonGreedyHostPool struct {
decayDuration time.Duration
EpsilonValueCalculator // embed the epsilonValueCalculator
timer
quit chan bool
}

// Construct an Epsilon Greedy HostPool
Expand Down Expand Up @@ -54,6 +54,7 @@ func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonV
decayDuration: decayDuration,
EpsilonValueCalculator: calc,
timer: &realTimer{},
quit: make(chan bool),
}

// allocate structures
Expand All @@ -65,6 +66,11 @@ func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonV
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) {
p.Lock()
defer p.Unlock()
Expand All @@ -73,10 +79,15 @@ func (p *epsilonGreedyHostPool) SetEpsilon(newEpsilon float32) {

func (p *epsilonGreedyHostPool) epsilonGreedyDecay() {
durationPerBucket := p.decayDuration / epsilonBuckets
ticker := time.Tick(durationPerBucket)
ticker := time.NewTicker(durationPerBucket)
for {
<-ticker
p.performEpsilonGreedyDecay()
select {
case <-p.quit:
ticker.Stop()
return
case <-ticker.C:
p.performEpsilonGreedyDecay()
}
}
}
func (p *epsilonGreedyHostPool) performEpsilonGreedyDecay() {
Expand Down
9 changes: 9 additions & 0 deletions hostpool.go
Expand Up @@ -45,6 +45,9 @@ type HostPool interface {

ResetAll()
Hosts() []string

// Close the hostpool and release all resources.
Close()
}

type standardHostPool struct {
Expand Down Expand Up @@ -155,6 +158,12 @@ func (p *standardHostPool) doResetAll() {
}
}

func (p *standardHostPool) Close() {
for _, h := range p.hosts {
h.dead = true
}
}

func (p *standardHostPool) markSuccess(hostR HostPoolResponse) {
host := hostR.Host()
p.Lock()
Expand Down

0 comments on commit e83c9e8

Please sign in to comment.