Browse Source

smashing some locks, still passing

Dan Frank 13 năm trước cách đây
mục cha
commit
61092fca5b
3 tập tin đã thay đổi với 6 bổ sung25 xóa
  1. 0 1
      epsilon_decay_test.go
  2. 3 2
      host_entry.go
  3. 3 22
      hostpool.go

+ 0 - 1
epsilon_decay_test.go

@@ -2,7 +2,6 @@ package hostpool
 
 import (
 	"github.com/bmizerany/assert"
-	"log"
 	"testing"
 )
 

+ 3 - 2
host_entry.go

@@ -1,6 +1,7 @@
 package hostpool
 
 import (
+	"log"
 	"math"
 	"time"
 )
@@ -103,7 +104,7 @@ func (he *hostEntry) IsDead() bool {
 	resp := <-req.respChan
 	isDeadResp, ok := resp.(bool)
 	if !ok {
-		// TODO
+		log.Fatal("Got incorrect response type from host_entry muxer in IsDead")
 	}
 	return isDeadResp
 }
@@ -130,7 +131,7 @@ func (he *hostEntry) canTryHost(now time.Time) bool {
 	resp := <-req.respChan
 	canTryResp, ok := resp.(bool)
 	if !ok {
-		// TODO
+		log.Fatal("Got incorrect response type from host_entry muxer in canTryHost")
 	}
 	return canTryResp
 }

+ 3 - 22
hostpool.go

@@ -41,8 +41,6 @@ type HostPool interface {
 	ResetAll()
 	Hosts() []string
 	lookupHost(string) HostEntry
-	// setHostMap(map[string]HostEntry)
-	sync.Locker
 }
 
 type standardHostPool struct {
@@ -114,12 +112,6 @@ func (rt *realTimer) between(start time.Time, end time.Time) time.Duration {
 	return end.Sub(start)
 }
 
-func (p *epsilonGreedyHostPool) SetEpsilon(newEpsilon float32) {
-	p.Lock()
-	defer p.Unlock()
-	p.epsilon = newEpsilon
-}
-
 // return an upstream entry from the HostPool
 func (p *standardHostPool) Get() HostPoolResponse {
 	p.Lock()
@@ -161,21 +153,13 @@ func (p *standardHostPool) getRoundRobin() string {
 	}
 
 	// all hosts are down. re-add them
-	p.doResetAll()
+	p.ResetAll()
 	p.nextHostIndex = 0
 	return p.hostList()[0].Host()
 }
 
 func (p *standardHostPool) ResetAll() {
-	p.Lock()
-	defer p.Unlock()
-	p.doResetAll()
-}
-
-// this actually performs the logic to reset,
-// and should only be called when the lock has
-// already been acquired
-func (p *standardHostPool) doResetAll() {
+	// SetDead is threadsafe
 	for _, h := range p.hosts {
 		h.SetDead(false)
 	}
@@ -183,8 +167,6 @@ func (p *standardHostPool) doResetAll() {
 
 func (p *standardHostPool) markSuccess(hostR HostPoolResponse) {
 	host := hostR.Host()
-	p.Lock()
-	defer p.Unlock()
 
 	h, ok := p.hosts[host]
 	if !ok {
@@ -195,8 +177,7 @@ func (p *standardHostPool) markSuccess(hostR HostPoolResponse) {
 
 func (p *standardHostPool) markFailed(hostR HostPoolResponse) {
 	host := hostR.Host()
-	p.Lock()
-	defer p.Unlock()
+
 	h, ok := p.hosts[host]
 	if !ok {
 		log.Fatalf("host %s not in HostPool %v", host, p.Hosts())