Skip to content

Commit

Permalink
Move to a selector package
Browse files Browse the repository at this point in the history
  • Loading branch information
Asim committed Dec 9, 2015
1 parent 91405e5 commit eefb9c5
Show file tree
Hide file tree
Showing 18 changed files with 304 additions and 188 deletions.
9 changes: 5 additions & 4 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
"github.com/micro/go-micro/transport"
)

Expand All @@ -12,13 +13,13 @@ type options struct {
broker broker.Broker
codecs map[string]codec.NewCodec
registry registry.Registry
selector registry.Selector
selector selector.Selector
transport transport.Transport
wrappers []Wrapper
}

type callOptions struct {
selectOptions []registry.SelectOption
selectOptions []selector.SelectOption
}

type publishOptions struct{}
Expand Down Expand Up @@ -59,7 +60,7 @@ func Transport(t transport.Transport) Option {
}

// Select is used to select a node to route a request to
func Selector(s registry.Selector) Option {
func Selector(s selector.Selector) Option {
return func(o *options) {
o.selector = s
}
Expand All @@ -74,7 +75,7 @@ func Wrap(w Wrapper) Option {

// Call Options

func WithSelectOption(so registry.SelectOption) CallOption {
func WithSelectOption(so selector.SelectOption) CallOption {
return func(o *callOptions) {
o.selectOptions = append(o.selectOptions, so)
}
Expand Down
13 changes: 7 additions & 6 deletions client/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
c "github.com/micro/go-micro/context"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
"github.com/micro/go-micro/transport"

"golang.org/x/net/context"
Expand Down Expand Up @@ -44,8 +45,8 @@ func newRpcClient(opt ...Option) Client {
}

if opts.selector == nil {
opts.selector = registry.NewRandomSelector(
registry.SelectorRegistry(opts.registry),
opts.selector = selector.NewSelector(
selector.Registry(opts.registry),
)
}

Expand Down Expand Up @@ -156,14 +157,14 @@ func (r *rpcClient) Call(ctx context.Context, request Request, response interfac
}

next, err := r.opts.selector.Select(request.Service(), copts.selectOptions...)
if err != nil && err == registry.ErrNotFound {
if err != nil && err == selector.ErrNotFound {
return errors.NotFound("go.micro.client", err.Error())
} else if err != nil {
return errors.InternalServerError("go.micro.client", err.Error())
}

node, err := next()
if err != nil && err == registry.ErrNotFound {
if err != nil && err == selector.ErrNotFound {
return errors.NotFound("go.micro.client", err.Error())
} else if err != nil {
return errors.InternalServerError("go.micro.client", err.Error())
Expand All @@ -190,14 +191,14 @@ func (r *rpcClient) Stream(ctx context.Context, request Request, responseChan in
}

next, err := r.opts.selector.Select(request.Service(), copts.selectOptions...)
if err != nil && err == registry.ErrNotFound {
if err != nil && err == selector.ErrNotFound {
return nil, errors.NotFound("go.micro.client", err.Error())
} else if err != nil {
return nil, errors.InternalServerError("go.micro.client", err.Error())
}

node, err := next()
if err != nil && err == registry.ErrNotFound {
if err != nil && err == selector.ErrNotFound {
return nil, errors.NotFound("go.micro.client", err.Error())
} else if err != nil {
return nil, errors.InternalServerError("go.micro.client", err.Error())
Expand Down
8 changes: 6 additions & 2 deletions examples/client/dc_filter/dc_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd"
c "github.com/micro/go-micro/context"
example "github.com/micro/go-micro/examples/server/proto/example"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
"golang.org/x/net/context"

example "github.com/micro/go-micro/examples/server/proto/example"
)

func init() {
Expand Down Expand Up @@ -38,7 +40,9 @@ func (dc *dcWrapper) Call(ctx context.Context, req client.Request, rsp interface
return services
}

callOptions := append(opts, client.WithSelectOption(registry.SelectFilter(filter)))
callOptions := append(opts, client.WithSelectOption(
selector.Filter(filter),
))

fmt.Printf("[DC Wrapper] filtering for datacenter %s\n", md["datacenter"])
return dc.Client.Call(ctx, req, rsp, callOptions...)
Expand Down
16 changes: 9 additions & 7 deletions examples/client/dc_selector/dc_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (

"github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd"
example "github.com/micro/go-micro/examples/server/proto/example"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
"golang.org/x/net/context"

example "github.com/micro/go-micro/examples/server/proto/example"
)

// Built in random hashed node selector
type dcSelector struct {
opts registry.SelectorOptions
opts selector.Options
}

var (
Expand All @@ -26,14 +28,14 @@ func init() {
rand.Seed(time.Now().Unix())
}

func (n *dcSelector) Select(service string, opts ...registry.SelectOption) (registry.SelectNext, error) {
func (n *dcSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
services, err := n.opts.Registry.GetService(service)
if err != nil {
return nil, err
}

if len(services) == 0 {
return nil, registry.ErrNotFound
return nil, selector.ErrNotFound
}

var nodes []*registry.Node
Expand All @@ -48,7 +50,7 @@ func (n *dcSelector) Select(service string, opts ...registry.SelectOption) (regi
}

if len(nodes) == 0 {
return nil, registry.ErrNotFound
return nil, selector.ErrNotFound
}

var i int
Expand All @@ -75,8 +77,8 @@ func (n *dcSelector) Close() error {
}

// Return a new first node selector
func DCSelector(opts ...registry.SelectorOption) registry.Selector {
var sopts registry.SelectorOptions
func DCSelector(opts ...selector.Option) selector.Selector {
var sopts selector.Options
for _, opt := range opts {
opt(&sopts)
}
Expand Down
17 changes: 9 additions & 8 deletions examples/client/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/micro/go-micro/cmd"
example "github.com/micro/go-micro/examples/server/proto/example"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
"golang.org/x/net/context"
)

Expand All @@ -18,20 +19,20 @@ func init() {

// Built in random hashed node selector
type firstNodeSelector struct {
opts registry.SelectorOptions
opts selector.Options
}

func (n *firstNodeSelector) Select(service string, opts ...registry.SelectOption) (registry.SelectNext, error) {
func (n *firstNodeSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
services, err := n.opts.Registry.GetService(service)
if err != nil {
return nil, err
}

if len(services) == 0 {
return nil, registry.ErrNotFound
return nil, selector.ErrNotFound
}

var sopts registry.SelectOptions
var sopts selector.SelectOptions
for _, opt := range opts {
opt(&sopts)
}
Expand All @@ -41,11 +42,11 @@ func (n *firstNodeSelector) Select(service string, opts ...registry.SelectOption
}

if len(services) == 0 {
return nil, registry.ErrNotFound
return nil, selector.ErrNotFound
}

if len(services[0].Nodes) == 0 {
return nil, registry.ErrNotFound
return nil, selector.ErrNotFound
}

return func() (*registry.Node, error) {
Expand All @@ -66,8 +67,8 @@ func (n *firstNodeSelector) Close() error {
}

// Return a new first node selector
func FirstNodeSelector(opts ...registry.SelectorOption) registry.Selector {
var sopts registry.SelectorOptions
func FirstNodeSelector(opts ...selector.Option) selector.Selector {
var sopts selector.Options
for _, opt := range opts {
opt(&sopts)
}
Expand Down
70 changes: 70 additions & 0 deletions registry/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package mock

import (
"github.com/micro/go-micro/registry"
)

type MockRegistry struct{}

func (m *MockRegistry) GetService(service string) ([]*registry.Service, error) {
return []*registry.Service{
{
Name: "foo",
Version: "1.0.0",
Nodes: []*registry.Node{
{
Id: "foo-1.0.0-123",
Address: "localhost",
Port: 9999,
},
{
Id: "foo-1.0.0-321",
Address: "localhost",
Port: 9999,
},
},
},
{
Name: "foo",
Version: "1.0.1",
Nodes: []*registry.Node{
{
Id: "foo-1.0.1-321",
Address: "localhost",
Port: 6666,
},
},
},
{
Name: "foo",
Version: "1.0.3",
Nodes: []*registry.Node{
{
Id: "foo-1.0.3-345",
Address: "localhost",
Port: 8888,
},
},
},
}, nil
}

func (m *MockRegistry) ListServices() ([]*registry.Service, error) {
return []*registry.Service{}, nil
}

func (m *MockRegistry) Register(s *registry.Service) error {
return nil
}

func (m *MockRegistry) Deregister(s *registry.Service) error {
return nil
}

func (m *MockRegistry) Watch() (registry.Watcher, error) {
return nil, nil
}

func NewRegistry() *MockRegistry {
return &MockRegistry{}
}
62 changes: 0 additions & 62 deletions registry/mock_registry.go

This file was deleted.

7 changes: 0 additions & 7 deletions registry/registry.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package registry

import (
"errors"
)

type Registry interface {
Register(*Service) error
Deregister(*Service) error
Expand All @@ -18,9 +14,6 @@ type Option func(*options)

var (
DefaultRegistry = newConsulRegistry([]string{})

ErrNotFound = errors.New("not found")
ErrNoneAvailable = errors.New("none available")
)

func NewRegistry(addrs []string, opt ...Option) Registry {
Expand Down
Loading

0 comments on commit eefb9c5

Please sign in to comment.