2016-05-04 00:06:19 +03:00
|
|
|
package selector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2016-05-04 00:06:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random is a random strategy algorithm for node selection
|
|
|
|
func Random(services []*registry.Service) Next {
|
2019-11-05 20:43:12 +03:00
|
|
|
nodes := make([]*registry.Node, 0, len(services))
|
2016-05-04 00:06:19 +03:00
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
nodes = append(nodes, service.Nodes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() (*registry.Node, error) {
|
|
|
|
if len(nodes) == 0 {
|
2016-05-07 02:04:08 +03:00
|
|
|
return nil, ErrNoneAvailable
|
2016-05-04 00:06:19 +03: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 {
|
2019-12-03 22:59:44 +03:00
|
|
|
nodes := make([]*registry.Node, 0, len(services))
|
2016-05-04 00:06:19 +03:00
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
nodes = append(nodes, service.Nodes...)
|
|
|
|
}
|
|
|
|
|
2016-10-29 11:31:32 +03:00
|
|
|
var i = rand.Int()
|
2016-05-04 00:06:19 +03:00
|
|
|
var mtx sync.Mutex
|
|
|
|
|
|
|
|
return func() (*registry.Node, error) {
|
|
|
|
if len(nodes) == 0 {
|
2016-05-07 02:04:08 +03:00
|
|
|
return nil, ErrNoneAvailable
|
2016-05-04 00:06:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mtx.Lock()
|
|
|
|
node := nodes[i%len(nodes)]
|
|
|
|
i++
|
|
|
|
mtx.Unlock()
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
}
|