Kaynağa Gözat

Exported methods should only interface type

Dan Frank 13 yıl önce
ebeveyn
işleme
90acd42c31
2 değiştirilmiş dosya ile 6 ekleme ve 10 silme
  1. 4 8
      hostpool.go
  2. 2 2
      hostpool_test.go

+ 4 - 8
hostpool.go

@@ -16,7 +16,6 @@ type timer interface {
 
 type realTimer struct{}
 
-
 // --- Response interfaces and structs ----
 
 type HostPoolResponse interface {
@@ -81,7 +80,6 @@ type hostEntry struct {
 	epsilonPercentage float64
 }
 
-
 // --- Value Calculators -----------------
 
 type EpsilonValueCalculator interface {
@@ -103,7 +101,7 @@ const minEpsilon = 0.01   // explore one percent of the time
 const initialEpsilon = 0.3
 const defaultDecayDuration = time.Duration(5) * time.Minute
 
-func New(hosts []string) *standardHostPool {
+func New(hosts []string) HostPool {
 	p := &standardHostPool{
 		hosts:             make(map[string]*hostEntry, len(hosts)),
 		hostList:          make([]*hostEntry, len(hosts)),
@@ -170,13 +168,14 @@ func (r *epsilonHostPoolResponse) Mark(err error) {
 // a good overview of Epsilon Greedy is here http://stevehanov.ca/blog/index.php?id=132
 //
 // decayDuration may be set to 0 to use the default value of 5 minutes
-func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonValueCalculator) *epsilonGreedyHostPool {
+func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonValueCalculator) HostPool {
 
 	if decayDuration <= 0 {
 		decayDuration = defaultDecayDuration
 	}
+	stdHP := New(hosts).(*standardHostPool)
 	p := &epsilonGreedyHostPool{
-		standardHostPool:       *New(hosts),
+		standardHostPool:       *stdHP,
 		epsilon:                float32(initialEpsilon),
 		decayDuration:          decayDuration,
 		EpsilonValueCalculator: calc,
@@ -196,14 +195,12 @@ 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
 }
 
-
 func (p *epsilonGreedyHostPool) epsilonGreedyDecay() {
 	durationPerBucket := p.decayDuration / epsilonBuckets
 	ticker := time.Tick(durationPerBucket)
@@ -223,7 +220,6 @@ func (p *epsilonGreedyHostPool) performEpsilonGreedyDecay() {
 	p.Unlock()
 }
 
-
 // return an upstream entry from the HostPool
 func (p *standardHostPool) Get() HostPoolResponse {
 	p.Lock()

+ 2 - 2
hostpool_test.go

@@ -1,6 +1,7 @@
 package hostpool
 
 import (
+	"errors"
 	"github.com/bmizerany/assert"
 	"io/ioutil"
 	"log"
@@ -8,7 +9,6 @@ import (
 	"os"
 	"testing"
 	"time"
-	"errors"
 )
 
 func TestHostPool(t *testing.T) {
@@ -65,7 +65,7 @@ func TestEpsilonGreedy(t *testing.T) {
 	rand.Seed(10)
 
 	iterations := 12000
-	p := NewEpsilonGreedy([]string{"a", "b"}, 0, &LinearEpsilonValueCalculator{})
+	p := NewEpsilonGreedy([]string{"a", "b"}, 0, &LinearEpsilonValueCalculator{}).(*epsilonGreedyHostPool)
 
 	timings := make(map[string]int64)
 	timings["a"] = 200