Add selector code
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/registry"
|
||||
)
|
||||
|
||||
// Selector takes a Registry and returns a NodeSelector.
|
||||
// Used by the client to initialise a selector.
|
||||
type Selector func(registry.Registry) NodeSelector
|
||||
@@ -1,60 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/errors"
|
||||
"github.com/micro/go-micro/registry"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
Select(context.Context, Request) (*registry.Node, error)
|
||||
Response(*registry.Node, error)
|
||||
Reset()
|
||||
}
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
// Built in random hashed node selector
|
||||
type nodeSelector struct {
|
||||
r registry.Registry
|
||||
}
|
||||
|
||||
func (n *nodeSelector) Select(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())
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -12,12 +12,14 @@ type options struct {
|
||||
broker broker.Broker
|
||||
codecs map[string]codec.NewCodec
|
||||
registry registry.Registry
|
||||
selector registry.Selector
|
||||
transport transport.Transport
|
||||
wrappers []Wrapper
|
||||
selector Selector
|
||||
}
|
||||
|
||||
type callOptions struct{}
|
||||
type callOptions struct {
|
||||
selectOptions []registry.SelectOption
|
||||
}
|
||||
|
||||
type publishOptions struct{}
|
||||
|
||||
@@ -57,7 +59,7 @@ func Transport(t transport.Transport) Option {
|
||||
}
|
||||
|
||||
// Select is used to select a node to route a request to
|
||||
func Select(s Selector) Option {
|
||||
func Selector(s registry.Selector) Option {
|
||||
return func(o *options) {
|
||||
o.selector = s
|
||||
}
|
||||
@@ -69,3 +71,11 @@ func Wrap(w Wrapper) Option {
|
||||
o.wrappers = append(o.wrappers, w)
|
||||
}
|
||||
}
|
||||
|
||||
// Call Options
|
||||
|
||||
func WithSelectOption(so registry.SelectOption) CallOption {
|
||||
return func(o *callOptions) {
|
||||
o.selectOptions = append(o.selectOptions, so)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,9 @@ import (
|
||||
type rpcClient struct {
|
||||
once sync.Once
|
||||
opts options
|
||||
sel NodeSelector
|
||||
}
|
||||
|
||||
func newRpcClient(opt ...Option) Client {
|
||||
var sel NodeSelector
|
||||
var once sync.Once
|
||||
|
||||
opts := options{
|
||||
@@ -37,28 +35,27 @@ func newRpcClient(opt ...Option) Client {
|
||||
opts.contentType = defaultContentType
|
||||
}
|
||||
|
||||
if opts.transport == nil {
|
||||
opts.transport = transport.DefaultTransport
|
||||
if opts.broker == nil {
|
||||
opts.broker = broker.DefaultBroker
|
||||
}
|
||||
|
||||
if opts.registry == nil {
|
||||
opts.registry = registry.DefaultRegistry
|
||||
}
|
||||
|
||||
if opts.broker == nil {
|
||||
opts.broker = broker.DefaultBroker
|
||||
if opts.selector == nil {
|
||||
opts.selector = registry.NewRandomSelector(
|
||||
registry.SelectorRegistry(opts.registry),
|
||||
)
|
||||
}
|
||||
|
||||
if opts.selector != nil {
|
||||
sel = opts.selector(opts.registry)
|
||||
} else {
|
||||
sel = &nodeSelector{opts.registry}
|
||||
if opts.transport == nil {
|
||||
opts.transport = transport.DefaultTransport
|
||||
}
|
||||
|
||||
rc := &rpcClient{
|
||||
once: once,
|
||||
opts: opts,
|
||||
sel: sel,
|
||||
}
|
||||
|
||||
c := Client(rc)
|
||||
@@ -153,7 +150,17 @@ func (r *rpcClient) CallRemote(ctx context.Context, address string, request Requ
|
||||
}
|
||||
|
||||
func (r *rpcClient) Call(ctx context.Context, request Request, response interface{}, opts ...CallOption) error {
|
||||
node, err := r.sel.Select(ctx, request)
|
||||
var copts callOptions
|
||||
for _, opt := range opts {
|
||||
opt(&copts)
|
||||
}
|
||||
|
||||
next, err := r.opts.selector.Select(request.Service(), copts.selectOptions...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
node, err := next()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -164,7 +171,7 @@ func (r *rpcClient) Call(ctx context.Context, request Request, response interfac
|
||||
}
|
||||
|
||||
err = r.call(ctx, address, request, response)
|
||||
r.sel.Response(node, err)
|
||||
r.opts.selector.Mark(request.Service(), node, err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -173,7 +180,17 @@ func (r *rpcClient) StreamRemote(ctx context.Context, address string, request Re
|
||||
}
|
||||
|
||||
func (r *rpcClient) Stream(ctx context.Context, request Request, responseChan interface{}, opts ...CallOption) (Streamer, error) {
|
||||
node, err := r.sel.Select(ctx, request)
|
||||
var copts callOptions
|
||||
for _, opt := range opts {
|
||||
opt(&copts)
|
||||
}
|
||||
|
||||
next, err := r.opts.selector.Select(request.Service(), copts.selectOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
node, err := next()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -184,7 +201,7 @@ func (r *rpcClient) Stream(ctx context.Context, request Request, responseChan in
|
||||
}
|
||||
|
||||
stream, err := r.stream(ctx, address, request, responseChan)
|
||||
r.sel.Response(node, err)
|
||||
r.opts.selector.Mark(request.Service(), node, err)
|
||||
return stream, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user