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
|
|
|
)
|
|
|
|
|
2015-12-08 00:09:10 +03:00
|
|
|
// NodeSelector is used to retrieve a node to which a request
|
|
|
|
// should be routed. It takes a list of services and selects
|
|
|
|
// a single node. If a node cannot be selected it should return
|
|
|
|
// an error. A list of services is provided as a service may
|
|
|
|
// have 1 or more versions.
|
|
|
|
type NodeSelector func(service []*registry.Service) (*registry.Node, error)
|
|
|
|
|
2015-11-08 04:48:48 +03:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2015-12-08 00:09:10 +03:00
|
|
|
// Built in random hashed node selector
|
2015-11-08 04:48:48 +03:00
|
|
|
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
|
|
|
|
}
|