First commit in strategy rework

This commit is contained in:
Asim
2016-05-03 22:06:19 +01:00
parent b13361d010
commit 0a4484b406
11 changed files with 235 additions and 284 deletions

View File

@@ -2,7 +2,6 @@ package cache
import (
"log"
"math/rand"
"sync"
"time"
@@ -33,10 +32,6 @@ var (
DefaultTTL = time.Minute
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func (c *cacheSelector) quit() bool {
select {
case <-c.exit:
@@ -329,7 +324,10 @@ func (c *cacheSelector) Options() selector.Options {
}
func (c *cacheSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
var sopts selector.SelectOptions
sopts := selector.SelectOptions{
Strategy: c.so.Strategy,
}
for _, opt := range opts {
opt(&sopts)
}
@@ -352,29 +350,7 @@ func (c *cacheSelector) Select(service string, opts ...selector.SelectOption) (s
return nil, selector.ErrNotFound
}
var nodes []*registry.Node
for _, service := range services {
for _, node := range service.Nodes {
nodes = append(nodes, node)
}
}
if len(nodes) == 0 {
return nil, selector.ErrNotFound
}
return func() (*registry.Node, error) {
i := rand.Int()
j := i % len(services)
if len(services[j].Nodes) == 0 {
return nil, selector.ErrNotFound
}
k := i % len(services[j].Nodes)
return services[j].Nodes[k], nil
}, nil
return sopts.Strategy(services), nil
}
func (c *cacheSelector) Mark(service string, node *registry.Node, err error) {
@@ -407,7 +383,9 @@ func (c *cacheSelector) String() string {
}
func NewSelector(opts ...selector.Option) selector.Selector {
var sopts selector.Options
sopts := selector.Options{
Strategy: selector.Random,
}
for _, opt := range opts {
opt(&sopts)