micro/client/node_selector.go

30 lines
561 B
Go
Raw Normal View History

2015-11-08 04:48:48 +03:00
package client
import (
"math/rand"
"time"
2015-11-20 19:17:33 +03:00
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/registry"
2015-11-08 04:48:48 +03:00
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func nodeSelector(service []*registry.Service) (*registry.Node, error) {
if len(service) == 0 {
return nil, errors.NotFound("go.micro.client", "Service not found")
}
i := rand.Int()
j := i % len(service)
if len(service[j].Nodes) == 0 {
return nil, errors.NotFound("go.micro.client", "Service not found")
}
n := i % len(service[j].Nodes)
return service[j].Nodes[n], nil
}