Browse Source

change to selectHost to (sort of) remove knowledge of h.dead from epsilon greedy

Dan Frank 12 years ago
parent
commit
291ae4626e
2 changed files with 25 additions and 18 deletions
  1. 10 8
      epsilon_greedy.go
  2. 15 10
      hostpool.go

+ 10 - 8
epsilon_greedy.go

@@ -100,9 +100,9 @@ func (p *epsilonGreedyHostPool) Get() HostPoolResponse {
 	host, err := p.getEpsilonGreedy()
 	host, err := p.getEpsilonGreedy()
 	p.Unlock()
 	p.Unlock()
 	if err != nil {
 	if err != nil {
-		host = p.HostPool.Get().Host()
+		return p.toEpsilonHostPootResponse(p.HostPool.Get())
 	}
 	}
-	return p.responseForHostName(host)
+	return p.selectHost(host)
 }
 }
 
 
 func (p *epsilonGreedyHostPool) getEpsilonGreedy() (string, error) {
 func (p *epsilonGreedyHostPool) getEpsilonGreedy() (string, error) {
@@ -157,10 +157,6 @@ func (p *epsilonGreedyHostPool) getEpsilonGreedy() (string, error) {
 		}
 		}
 		return "", errors.New("No host chosen")
 		return "", errors.New("No host chosen")
 	}
 	}
-
-	if hostToUse.dead {
-		hostToUse.willRetryHost(p.HostPool.(*standardHostPool).maxRetryInterval)
-	}
 	return hostToUse.host, nil
 	return hostToUse.host, nil
 }
 }
 
 
@@ -178,10 +174,16 @@ func (p *epsilonGreedyHostPool) recordTiming(eHostR *epsilonHostPoolResponse) {
 	h.epsilonValues[h.epsilonIndex] += int64(duration.Seconds() * 1000)
 	h.epsilonValues[h.epsilonIndex] += int64(duration.Seconds() * 1000)
 }
 }
 
 
-func (p *epsilonGreedyHostPool) responseForHostName(host string) HostPoolResponse {
+func (p *epsilonGreedyHostPool) selectHost(host string) HostPoolResponse {
+	resp := p.HostPool.selectHost(host)
+	return p.toEpsilonHostPootResponse(resp)
+}
+
+// Convert regular response to one equipped for EG. Doesn't require lock, for now
+func (p *epsilonGreedyHostPool) toEpsilonHostPootResponse(resp HostPoolResponse) *epsilonHostPoolResponse {
 	started := time.Now()
 	started := time.Now()
 	return &epsilonHostPoolResponse{
 	return &epsilonHostPoolResponse{
-		HostPoolResponse: p.HostPool.responseForHostName(host),
+		HostPoolResponse: resp,
 		started:          started,
 		started:          started,
 		pool:             p,
 		pool:             p,
 	}
 	}

+ 15 - 10
hostpool.go

@@ -41,7 +41,7 @@ type HostPool interface {
 	ResetAll()
 	ResetAll()
 	Hosts() []string
 	Hosts() []string
 
 
-	responseForHostName(string) HostPoolResponse
+	selectHost(string) HostPoolResponse
 }
 }
 
 
 type standardHostPool struct {
 type standardHostPool struct {
@@ -97,9 +97,9 @@ func (r *standardHostPoolResponse) Mark(err error) {
 // return an entry from the HostPool
 // return an entry from the HostPool
 func (p *standardHostPool) Get() HostPoolResponse {
 func (p *standardHostPool) Get() HostPoolResponse {
 	p.Lock()
 	p.Lock()
-	defer p.Unlock()
 	host := p.getRoundRobin()
 	host := p.getRoundRobin()
-	return &standardHostPoolResponse{host: host, pool: p}
+	p.Unlock()
+	return p.selectHost(host)
 }
 }
 
 
 func (p *standardHostPool) getRoundRobin() string {
 func (p *standardHostPool) getRoundRobin() string {
@@ -110,12 +110,7 @@ func (p *standardHostPool) getRoundRobin() string {
 		currentIndex := (i + p.nextHostIndex) % hostCount
 		currentIndex := (i + p.nextHostIndex) % hostCount
 
 
 		h := p.hostList[currentIndex]
 		h := p.hostList[currentIndex]
-		if !h.dead {
-			p.nextHostIndex = currentIndex + 1
-			return h.host
-		}
-		if h.nextRetry.Before(now) {
-			h.willRetryHost(p.maxRetryInterval)
+		if h.canTryHost(now) {
 			p.nextHostIndex = currentIndex + 1
 			p.nextHostIndex = currentIndex + 1
 			return h.host
 			return h.host
 		}
 		}
@@ -178,6 +173,16 @@ func (p *standardHostPool) Hosts() []string {
 	return hosts
 	return hosts
 }
 }
 
 
-func (p *standardHostPool) responseForHostName(host string) HostPoolResponse {
+func (p *standardHostPool) selectHost(host string) HostPoolResponse {
+	p.Lock()
+	defer p.Unlock()
+	h, ok := p.hosts[host]
+	if !ok {
+		log.Fatalf("host %s not in HostPool %v", host, p.Hosts())
+	}
+	now := time.Now()
+	if h.dead && h.nextRetry.Before(now) {
+		h.willRetryHost(p.maxRetryInterval)
+	}
 	return &standardHostPoolResponse{host: host, pool: p}
 	return &standardHostPoolResponse{host: host, pool: p}
 }
 }