mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
f23638c036
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package selector
|
|
|
|
import (
|
|
"math/rand"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/micro/go-micro/v2/registry"
|
|
)
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().UnixNano())
|
|
}
|
|
|
|
// Random is a random strategy algorithm for node selection
|
|
func Random(services []*registry.Service) Next {
|
|
nodes := make([]*registry.Node, 0, len(services))
|
|
|
|
for _, service := range services {
|
|
nodes = append(nodes, service.Nodes...)
|
|
}
|
|
|
|
return func() (*registry.Node, error) {
|
|
if len(nodes) == 0 {
|
|
return nil, ErrNoneAvailable
|
|
}
|
|
|
|
i := rand.Int() % len(nodes)
|
|
return nodes[i], nil
|
|
}
|
|
}
|
|
|
|
// RoundRobin is a roundrobin strategy algorithm for node selection
|
|
func RoundRobin(services []*registry.Service) Next {
|
|
nodes := make([]*registry.Node, 0, len(services))
|
|
|
|
for _, service := range services {
|
|
nodes = append(nodes, service.Nodes...)
|
|
}
|
|
|
|
var i = rand.Int()
|
|
var mtx sync.Mutex
|
|
|
|
return func() (*registry.Node, error) {
|
|
if len(nodes) == 0 {
|
|
return nil, ErrNoneAvailable
|
|
}
|
|
|
|
mtx.Lock()
|
|
node := nodes[i%len(nodes)]
|
|
i++
|
|
mtx.Unlock()
|
|
|
|
return node, nil
|
|
}
|
|
}
|