Move to a selector package

This commit is contained in:
Asim
2015-12-09 19:23:16 +00:00
parent 91405e5993
commit eefb9c53d4
18 changed files with 304 additions and 188 deletions

View File

@@ -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() {
@@ -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...)

View File

@@ -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 (
@@ -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
@@ -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
@@ -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)
}

View File

@@ -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"
)
@@ -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)
}
@@ -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) {
@@ -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)
}