Selaa lähdekoodia

bugfixes, got tests passing!

Dan Frank 13 vuotta sitten
vanhempi
commit
1ad0ecc5d4
4 muutettua tiedostoa jossa 17 lisäystä ja 16 poistoa
  1. 2 1
      epsilon_decay.go
  2. 8 9
      epsilon_greedy.go
  3. 1 4
      hostpool.go
  4. 6 2
      hostpool_test.go

+ 2 - 1
epsilon_decay.go

@@ -17,6 +17,7 @@ const defaultDecayDuration = time.Duration(5) * time.Minute
 type EpsilonDecayStore interface {
 type EpsilonDecayStore interface {
 	Record(score float64)
 	Record(score float64)
 	GetWeightedAvgScore() float64
 	GetWeightedAvgScore() float64
+	performDecay() // this is only exposed in the interface for testing
 }
 }
 
 
 type defEpsDecayStore struct {
 type defEpsDecayStore struct {
@@ -86,7 +87,7 @@ func (ds *defEpsDecayStore) muxRequests(decayTicker <-chan time.Time) {
 		case <-decayTicker:
 		case <-decayTicker:
 			ds.performDecay()
 			ds.performDecay()
 		case req := <-ds.getWAScoreReqChan:
 		case req := <-ds.getWAScoreReqChan:
-			avgScore := ds.GetWeightedAvgScore()
+			avgScore := ds.getWeightedAverageScore()
 			req.respChan <- avgScore
 			req.respChan <- avgScore
 		case req := <-ds.recordReqChan:
 		case req := <-ds.recordReqChan:
 			newScore := req.score
 			newScore := req.score

+ 8 - 9
epsilon_greedy.go

@@ -72,8 +72,10 @@ func ToEpsilonGreedy(pool HostPool, decayDuration time.Duration, calc EpsilonVal
 		EpsilonValueCalculator: calc,
 		EpsilonValueCalculator: calc,
 		timer:                  &realTimer{},
 		timer:                  &realTimer{},
 	}
 	}
-	for _, hostName := range p.Hosts() {
-		p.hosts[hostName] = toEGHostEntry(p.lookupHost(hostName))
+
+	p.hosts = make(map[string]epsilonGreedyHostEntry)
+	for _, hostName := range pool.Hosts() {
+		p.hosts[hostName] = toEGHostEntry(pool.lookupHost(hostName))
 	}
 	}
 	return p
 	return p
 }
 }
@@ -97,12 +99,12 @@ func (p *epsilonGreedyHostPool) getEpsilonGreedy() string {
 		epsilonValue      float64
 		epsilonValue      float64
 		epsilonPercentage float64
 		epsilonPercentage float64
 	}
 	}
-	var hostToUse epsilonChoice
-	var possibleHosts []epsilonChoice
+	var hostToUse *epsilonChoice
+	var possibleHosts []*epsilonChoice
 	now := time.Now()
 	now := time.Now()
 	var sumValues float64
 	var sumValues float64
 	for _, h := range p.hosts {
 	for _, h := range p.hosts {
-		ec := epsilonChoice{he: h}
+		ec := &epsilonChoice{he: h}
 		if h.canTryHost(now) {
 		if h.canTryHost(now) {
 			v := h.GetWeightedAvgScore() // score is the response time
 			v := h.GetWeightedAvgScore() // score is the response time
 			if v > 0 {
 			if v > 0 {
@@ -119,7 +121,6 @@ func (p *epsilonGreedyHostPool) getEpsilonGreedy() string {
 		for _, ec := range possibleHosts {
 		for _, ec := range possibleHosts {
 			ec.epsilonPercentage = ec.epsilonValue / sumValues
 			ec.epsilonPercentage = ec.epsilonValue / sumValues
 		}
 		}
-
 		// do a weighted random choice among hosts
 		// do a weighted random choice among hosts
 		ceiling := 0.0
 		ceiling := 0.0
 		pickPercentage := rand.Float64()
 		pickPercentage := rand.Float64()
@@ -132,7 +133,7 @@ func (p *epsilonGreedyHostPool) getEpsilonGreedy() string {
 		}
 		}
 	}
 	}
 
 
-	if hostToUse.he == nil {
+	if hostToUse == nil {
 		if len(possibleHosts) != 0 {
 		if len(possibleHosts) != 0 {
 			log.Println("Failed to randomly choose a host, Dan loses")
 			log.Println("Failed to randomly choose a host, Dan loses")
 		}
 		}
@@ -156,8 +157,6 @@ func (p *epsilonGreedyHostPool) markSuccess(hostR HostPoolResponse) {
 	host := eHostR.host
 	host := eHostR.host
 	duration := p.between(eHostR.started, eHostR.ended)
 	duration := p.between(eHostR.started, eHostR.ended)
 
 
-	p.Lock()
-	defer p.Unlock()
 	h := p.hosts[host]
 	h := p.hosts[host]
 	h.Record(duration.Seconds())
 	h.Record(duration.Seconds())
 }
 }

+ 1 - 4
hostpool.go

@@ -77,7 +77,6 @@ func New(hosts []string) HostPool {
 		e := newHostEntry(h, p.initialRetryDelay, p.maxRetryInterval)
 		e := newHostEntry(h, p.initialRetryDelay, p.maxRetryInterval)
 		p.hosts[h] = e
 		p.hosts[h] = e
 	}
 	}
-
 	return p
 	return p
 }
 }
 
 
@@ -130,8 +129,6 @@ func (p *standardHostPool) Get() HostPoolResponse {
 }
 }
 
 
 func (p *epsilonGreedyHostPool) Get() HostPoolResponse {
 func (p *epsilonGreedyHostPool) Get() HostPoolResponse {
-	p.Lock()
-	defer p.Unlock()
 	host := p.getEpsilonGreedy()
 	host := p.getEpsilonGreedy()
 	started := time.Now()
 	started := time.Now()
 	return &epsilonHostPoolResponse{
 	return &epsilonHostPoolResponse{
@@ -208,7 +205,7 @@ func (p *standardHostPool) markFailed(hostR HostPoolResponse) {
 }
 }
 
 
 func (p *standardHostPool) Hosts() []string {
 func (p *standardHostPool) Hosts() []string {
-	hosts := make([]string, len(p.hosts))
+	hosts := make([]string, 0, len(p.hosts))
 	for host, _ := range p.hosts {
 	for host, _ := range p.hosts {
 		hosts = append(hosts, host)
 		hosts = append(hosts, host)
 	}
 	}

+ 6 - 2
hostpool_test.go

@@ -79,7 +79,9 @@ func TestEpsilonGreedy(t *testing.T) {
 
 
 	for i := 0; i < iterations; i += 1 {
 	for i := 0; i < iterations; i += 1 {
 		if i != 0 && i%100 == 0 {
 		if i != 0 && i%100 == 0 {
-			p.performEpsilonGreedyDecay()
+			for _, h := range p.hosts {
+				h.performDecay()
+			}
 		}
 		}
 		hostR := p.Get()
 		hostR := p.Get()
 		host := hostR.Host()
 		host := hostR.Host()
@@ -103,7 +105,9 @@ func TestEpsilonGreedy(t *testing.T) {
 
 
 	for i := 0; i < iterations; i += 1 {
 	for i := 0; i < iterations; i += 1 {
 		if i != 0 && i%100 == 0 {
 		if i != 0 && i%100 == 0 {
-			p.performEpsilonGreedyDecay()
+			for _, h := range p.hosts {
+				h.performDecay()
+			}
 		}
 		}
 		hostR := p.Get()
 		hostR := p.Get()
 		host := hostR.Host()
 		host := hostR.Host()