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-12-08 02:56:17 +03:00
|
|
|
"golang.org/x/net/context"
|
2015-11-08 04:48:48 +03:00
|
|
|
)
|
|
|
|
|
2015-12-08 02:56:17 +03:00
|
|
|
// NodeSelector is used to Retrieve a node to which a request
|
|
|
|
// should be routed. It takes context and Request and returns a
|
|
|
|
// single node. If a node cannot be selected it should return
|
|
|
|
// an error. Response is called to inform the selector of the
|
|
|
|
// response from a client call. Reset is called to zero out
|
|
|
|
// any state.
|
|
|
|
type NodeSelector interface {
|
|
|
|
Retrieve(context.Context, Request) (*registry.Node, error)
|
|
|
|
Response(*registry.Node, error)
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
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-12-08 02:56:17 +03:00
|
|
|
type nodeSelector struct {
|
|
|
|
r registry.Registry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nodeSelector) Retrieve(ctx context.Context, req Request) (*registry.Node, error) {
|
|
|
|
service, err := n.r.GetService(req.Service())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-11-08 04:48:48 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-12-08 02:56:17 +03:00
|
|
|
k := i % len(service[j].Nodes)
|
|
|
|
return service[j].Nodes[k], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nodeSelector) Response(node *registry.Node, err error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nodeSelector) Reset() {
|
|
|
|
return
|
2015-11-08 04:48:48 +03:00
|
|
|
}
|