Просмотр исходного кода

refactor to public and package level methods

Dan Frank 12 лет назад
Родитель
Сommit
25b6b21c28
4 измененных файлов с 31 добавлено и 25 удалено
  1. 5 5
      epsilon_greedy.go
  2. 1 1
      example_test.go
  3. 11 5
      hostpool.go
  4. 14 14
      hostpool_test.go

+ 5 - 5
epsilon_greedy.go

@@ -95,14 +95,14 @@ func (p *epsilonGreedyHostPool) performEpsilonGreedyDecay() {
 	p.Unlock()
 }
 
-func (p *epsilonGreedyHostPool) Get() HostPoolResponse {
+func (p *epsilonGreedyHostPool) ChooseNextHost() string {
 	p.Lock()
 	host, err := p.getEpsilonGreedy()
 	p.Unlock()
 	if err != nil {
-		return p.toEpsilonHostPootResponse(p.HostPool.Get())
+		host = p.HostPool.ChooseNextHost()
 	}
-	return p.selectHost(host)
+	return host
 }
 
 func (p *epsilonGreedyHostPool) getEpsilonGreedy() (string, error) {
@@ -174,8 +174,8 @@ func (p *epsilonGreedyHostPool) recordTiming(eHostR *epsilonHostPoolResponse) {
 	h.epsilonValues[h.epsilonIndex] += int64(duration.Seconds() * 1000)
 }
 
-func (p *epsilonGreedyHostPool) selectHost(host string) HostPoolResponse {
-	resp := p.HostPool.selectHost(host)
+func (p *epsilonGreedyHostPool) DeliverHostResponse(host string) HostPoolResponse {
+	resp := p.HostPool.DeliverHostResponse(host)
 	return p.toEpsilonHostPootResponse(resp)
 }
 

+ 1 - 1
example_test.go

@@ -6,7 +6,7 @@ import (
 
 func ExampleNewEpsilonGreedy() {
 	hp := NewEpsilonGreedy([]string{"a", "b"}, 0, &LinearEpsilonValueCalculator{})
-	hostResponse := hp.Get()
+	hostResponse := Get(hp)
 	hostname := hostResponse.Host()
 	err := errors.New("I am your http error from " + hostname) // (make a request with hostname)
 	hostResponse.Mark(err)

+ 11 - 5
hostpool.go

@@ -36,12 +36,13 @@ type standardHostPoolResponse struct {
 // allow you to Get a HostPoolResponse (which includes a hostname to use),
 // get the list of all Hosts, and use ResetAll to reset state.
 type HostPool interface {
-	Get() HostPoolResponse
+	// Get() HostPoolResponse
 
 	ResetAll()
 	Hosts() []string
 
-	selectHost(string) HostPoolResponse
+	ChooseNextHost() string
+	DeliverHostResponse(string) HostPoolResponse
 }
 
 type standardHostPool struct {
@@ -95,11 +96,16 @@ func (r *standardHostPoolResponse) Mark(err error) {
 }
 
 // return an entry from the HostPool
-func (p *standardHostPool) Get() HostPoolResponse {
+func Get(p HostPool) HostPoolResponse {
+	host := p.ChooseNextHost()
+	return p.DeliverHostResponse(host)
+}
+
+func (p *standardHostPool) ChooseNextHost() string {
 	p.Lock()
 	host := p.getRoundRobin()
 	p.Unlock()
-	return p.selectHost(host)
+	return host
 }
 
 func (p *standardHostPool) getRoundRobin() string {
@@ -173,7 +179,7 @@ func (p *standardHostPool) Hosts() []string {
 	return hosts
 }
 
-func (p *standardHostPool) selectHost(host string) HostPoolResponse {
+func (p *standardHostPool) DeliverHostResponse(host string) HostPoolResponse {
 	p.Lock()
 	defer p.Unlock()
 	h, ok := p.hosts[host]

+ 14 - 14
hostpool_test.go

@@ -18,33 +18,33 @@ func TestHostPool(t *testing.T) {
 	dummyErr := errors.New("Dummy Error")
 
 	p := New([]string{"a", "b", "c"}).(*standardHostPool)
-	assert.Equal(t, p.Get().Host(), "a")
-	assert.Equal(t, p.Get().Host(), "b")
-	assert.Equal(t, p.Get().Host(), "c")
-	respA := p.Get()
+	assert.Equal(t, Get(p).Host(), "a")
+	assert.Equal(t, Get(p).Host(), "b")
+	assert.Equal(t, Get(p).Host(), "c")
+	respA := Get(p)
 	assert.Equal(t, respA.Host(), "a")
 
 	respA.Mark(dummyErr)
-	respB := p.Get()
+	respB := Get(p)
 	respB.Mark(dummyErr)
-	respC := p.Get()
+	respC := Get(p)
 	assert.Equal(t, respC.Host(), "c")
 	respC.Mark(nil)
 	// get again, and verify that it's still c
-	assert.Equal(t, p.Get().Host(), "c")
-	assert.Equal(t, p.Get().Host(), "c") // would be b if it were not dead
+	assert.Equal(t, Get(p).Host(), "c")
+	assert.Equal(t, Get(p).Host(), "c") // would be b if it were not dead
 	// now restore a
 	respA = &standardHostPoolResponse{host: "a", pool: p}
 	respA.Mark(nil)
-	assert.Equal(t, p.Get().Host(), "a")
-	assert.Equal(t, p.Get().Host(), "c")
+	assert.Equal(t, Get(p).Host(), "a")
+	assert.Equal(t, Get(p).Host(), "c")
 
 	// ensure that we get *something* back when all hosts fail
 	for _, host := range []string{"a", "b", "c"} {
 		response := &standardHostPoolResponse{host: host, pool: p}
 		response.Mark(dummyErr)
 	}
-	resp := p.Get()
+	resp := Get(p)
 	assert.NotEqual(t, resp, nil)
 }
 
@@ -79,7 +79,7 @@ func TestEpsilonGreedy(t *testing.T) {
 		if i != 0 && i%100 == 0 {
 			p.performEpsilonGreedyDecay()
 		}
-		hostR := p.Get()
+		hostR := Get(p)
 		host := hostR.Host()
 		hitCounts[host]++
 		timing := timings[host]
@@ -103,7 +103,7 @@ func TestEpsilonGreedy(t *testing.T) {
 		if i != 0 && i%100 == 0 {
 			p.performEpsilonGreedyDecay()
 		}
-		hostR := p.Get()
+		hostR := Get(p)
 		host := hostR.Host()
 		hitCounts[host]++
 		timing := timings[host]
@@ -136,7 +136,7 @@ func BenchmarkEpsilonGreedy(b *testing.B) {
 		if i != 0 && i%100 == 0 {
 			p.performEpsilonGreedyDecay()
 		}
-		hostR := p.Get()
+		hostR := Get(p)
 		p.timer = &mockTimer{t: int(timings[i])}
 		hostR.Mark(nil)
 	}