Skip to content

Commit be8d763

Browse files
author
Boyan Dimitrov
committed
Merge pull request bitly#1 from hailocab/sethosts
Allow hosts for an existing hostpool to be changed
2 parents 2e89d10 + a977ea4 commit be8d763

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

epsilon_greedy.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ func (p *epsilonGreedyHostPool) SetEpsilon(newEpsilon float32) {
7171
p.epsilon = newEpsilon
7272
}
7373

74+
func (p *epsilonGreedyHostPool) SetHosts(hosts []string) {
75+
p.Lock()
76+
defer p.Unlock()
77+
p.standardHostPool.setHosts(hosts)
78+
for _, h := range p.hostList {
79+
h.epsilonCounts = make([]int64, epsilonBuckets)
80+
h.epsilonValues = make([]int64, epsilonBuckets)
81+
}
82+
}
83+
7484
func (p *epsilonGreedyHostPool) epsilonGreedyDecay() {
7585
durationPerBucket := p.decayDuration / epsilonBuckets
7686
ticker := time.Tick(durationPerBucket)

hostpool.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type HostPool interface {
4545

4646
ResetAll()
4747
Hosts() []string
48+
SetHosts([]string)
4849
}
4950

5051
type standardHostPool struct {
@@ -146,6 +147,26 @@ func (p *standardHostPool) ResetAll() {
146147
p.doResetAll()
147148
}
148149

150+
func (p *standardHostPool) SetHosts(hosts []string) {
151+
p.Lock()
152+
defer p.Unlock()
153+
p.setHosts(hosts)
154+
}
155+
156+
func (p *standardHostPool) setHosts(hosts []string) {
157+
p.hosts = make(map[string]*hostEntry, len(hosts))
158+
p.hostList = make([]*hostEntry, len(hosts))
159+
160+
for i, h := range hosts {
161+
e := &hostEntry{
162+
host: h,
163+
retryDelay: p.initialRetryDelay,
164+
}
165+
p.hosts[h] = e
166+
p.hostList[i] = e
167+
}
168+
}
169+
149170
// this actually performs the logic to reset,
150171
// and should only be called when the lock has
151172
// already been acquired

0 commit comments

Comments
 (0)