浏览代码

rm markSuccess and ilk from interface, bind response and hp types

Dan Frank 12 年之前
父节点
当前提交
5b6ec7b806
共有 4 个文件被更改,包括 10 次插入17 次删除
  1. 1 1
      epsilon_greedy.go
  2. 3 3
      example_test.go
  3. 5 12
      hostpool.go
  4. 1 1
      hostpool_test.go

+ 1 - 1
epsilon_greedy.go

@@ -97,7 +97,7 @@ func (p *epsilonGreedyHostPool) Get() HostPoolResponse {
 	host := p.getEpsilonGreedy()
 	started := time.Now()
 	return &epsilonHostPoolResponse{
-		HostPoolResponse: &standardHostPoolResponse{host: host, pool: p},
+		HostPoolResponse: &standardHostPoolResponse{host: host, pool: &p.standardHostPool},
 		started:          started,
 		pool:             p,
 	}

+ 3 - 3
example_test.go

@@ -1,13 +1,13 @@
 package hostpool
 
 import (
-	"github.com/bitly/go-hostpool"
+	"errors"
 )
 
 func ExampleNewEpsilonGreedy() {
-	hp := hostpool.NewEpsilonGreedy([]string{"a", "b"}, 0, &hostpool.LinearEpsilonValueCalculator{})
+	hp := NewEpsilonGreedy([]string{"a", "b"}, 0, &LinearEpsilonValueCalculator{})
 	hostResponse := hp.Get()
 	hostname := hostResponse.Host()
-	err := nil // (make a request with hostname)
+	err := errors.New("I am your http error from " + hostname) // (make a request with hostname)
 	hostResponse.Mark(err)
 }

+ 5 - 12
hostpool.go

@@ -27,7 +27,7 @@ type HostPoolResponse interface {
 
 type standardHostPoolResponse struct {
 	host string
-	pool HostPool
+	pool *standardHostPool
 }
 
 // --- HostPool structs and interfaces ----
@@ -37,9 +37,6 @@ type standardHostPoolResponse struct {
 // get the list of all Hosts, and use ResetAll to reset state.
 type HostPool interface {
 	Get() HostPoolResponse
-	// keep the marks separate so we can override independently
-	markSuccess(HostPoolResponse)
-	markFailed(HostPoolResponse)
 
 	ResetAll()
 	Hosts() []string
@@ -87,15 +84,11 @@ func (r *standardHostPoolResponse) Host() string {
 	return r.host
 }
 
-func (r *standardHostPoolResponse) hostPool() HostPool {
-	return r.pool
-}
-
 func (r *standardHostPoolResponse) Mark(err error) {
 	if err == nil {
-		r.hostPool().markSuccess(r)
+		r.pool.markSuccess(r)
 	} else {
-		r.hostPool().markFailed(r)
+		r.pool.markFailed(r)
 	}
 }
 
@@ -147,7 +140,7 @@ func (p *standardHostPool) doResetAll() {
 	}
 }
 
-func (p *standardHostPool) markSuccess(hostR HostPoolResponse) {
+func (p *standardHostPool) markSuccess(hostR *standardHostPoolResponse) {
 	host := hostR.Host()
 	p.Lock()
 	defer p.Unlock()
@@ -159,7 +152,7 @@ func (p *standardHostPool) markSuccess(hostR HostPoolResponse) {
 	h.dead = false
 }
 
-func (p *standardHostPool) markFailed(hostR HostPoolResponse) {
+func (p *standardHostPool) markFailed(hostR *standardHostPoolResponse) {
 	host := hostR.Host()
 	p.Lock()
 	defer p.Unlock()

+ 1 - 1
hostpool_test.go

@@ -17,7 +17,7 @@ func TestHostPool(t *testing.T) {
 
 	dummyErr := errors.New("Dummy Error")
 
-	p := New([]string{"a", "b", "c"})
+	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")