2016-05-03 22:06:19 +01:00
|
|
|
package selector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random is a random strategy algorithm for node selection
|
|
|
|
func Random(services []*registry.Service) Next {
|
|
|
|
var nodes []*registry.Node
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
nodes = append(nodes, service.Nodes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() (*registry.Node, error) {
|
|
|
|
if len(nodes) == 0 {
|
2016-05-07 00:04:08 +01:00
|
|
|
return nil, ErrNoneAvailable
|
2016-05-03 22:06:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
i := rand.Int() % len(nodes)
|
|
|
|
return nodes[i], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundRobin is a roundrobin strategy algorithm for node selection
|
|
|
|
func RoundRobin(services []*registry.Service) Next {
|
|
|
|
var nodes []*registry.Node
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
nodes = append(nodes, service.Nodes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
var i int
|
|
|
|
var mtx sync.Mutex
|
|
|
|
|
|
|
|
return func() (*registry.Node, error) {
|
|
|
|
if len(nodes) == 0 {
|
2016-05-07 00:04:08 +01:00
|
|
|
return nil, ErrNoneAvailable
|
2016-05-03 22:06:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mtx.Lock()
|
|
|
|
node := nodes[i%len(nodes)]
|
|
|
|
i++
|
|
|
|
mtx.Unlock()
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
}
|