Browse Source

Allow hosts for an existing hostpool to be changed

Asim Aslam 11 years ago
parent
commit
a977ea43b7
2 changed files with 31 additions and 0 deletions
  1. 10 0
      epsilon_greedy.go
  2. 21 0
      hostpool.go

+ 10 - 0
epsilon_greedy.go

@@ -71,6 +71,16 @@ func (p *epsilonGreedyHostPool) SetEpsilon(newEpsilon float32) {
 	p.epsilon = newEpsilon
 }
 
+func (p *epsilonGreedyHostPool) SetHosts(hosts []string) {
+	p.Lock()
+	defer p.Unlock()
+	p.standardHostPool.setHosts(hosts)
+	for _, h := range p.hostList {
+		h.epsilonCounts = make([]int64, epsilonBuckets)
+		h.epsilonValues = make([]int64, epsilonBuckets)
+	}
+}
+
 func (p *epsilonGreedyHostPool) epsilonGreedyDecay() {
 	durationPerBucket := p.decayDuration / epsilonBuckets
 	ticker := time.Tick(durationPerBucket)

+ 21 - 0
hostpool.go

@@ -45,6 +45,7 @@ type HostPool interface {
 
 	ResetAll()
 	Hosts() []string
+	SetHosts([]string)
 }
 
 type standardHostPool struct {
@@ -146,6 +147,26 @@ func (p *standardHostPool) ResetAll() {
 	p.doResetAll()
 }
 
+func (p *standardHostPool) SetHosts(hosts []string) {
+	p.Lock()
+	defer p.Unlock()
+	p.setHosts(hosts)
+}
+
+func (p *standardHostPool) setHosts(hosts []string) {
+	p.hosts = make(map[string]*hostEntry, len(hosts))
+	p.hostList = make([]*hostEntry, len(hosts))
+
+	for i, h := range hosts {
+		e := &hostEntry{
+			host:       h,
+			retryDelay: p.initialRetryDelay,
+		}
+		p.hosts[h] = e
+		p.hostList[i] = e
+	}
+}
+
 // this actually performs the logic to reset,
 // and should only be called when the lock has
 // already been acquired