From 897115ed3160b5b6580fc0da7153cace3b7a2b9f Mon Sep 17 00:00:00 2001 From: Asim Date: Mon, 4 Jan 2016 23:07:56 +0000 Subject: [PATCH] Fix some issues with initialisation --- cmd/cmd.go | 4 ++++ examples/client/dc_selector/dc_selector.go | 11 +++++++++++ examples/client/selector/selector.go | 11 +++++++++++ selector/blacklist/black_list_selector.go | 11 +++++++++++ selector/random_selector.go | 11 +++++++++++ selector/roundrobin/round_robin_selector.go | 11 +++++++++++ selector/selector.go | 2 ++ 7 files changed, 61 insertions(+) diff --git a/cmd/cmd.go b/cmd/cmd.go index e99657e7..f5afff6f 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -238,6 +238,7 @@ func (c *cmd) Before(ctx *cli.Context) error { serverOpts = append(serverOpts, server.Broker(*c.opts.Broker)) clientOpts = append(clientOpts, client.Broker(*c.opts.Broker)) + } // Set the registry @@ -251,6 +252,9 @@ func (c *cmd) Before(ctx *cli.Context) error { serverOpts = append(serverOpts, server.Registry(*c.opts.Registry)) clientOpts = append(clientOpts, client.Registry(*c.opts.Registry)) + + (*c.opts.Selector).Init(selector.Registry(*c.opts.Registry)) + clientOpts = append(clientOpts, client.Selector(*c.opts.Selector)) } // Set the selector diff --git a/examples/client/dc_selector/dc_selector.go b/examples/client/dc_selector/dc_selector.go index 60669bd8..00a0cf2c 100644 --- a/examples/client/dc_selector/dc_selector.go +++ b/examples/client/dc_selector/dc_selector.go @@ -28,6 +28,17 @@ func init() { rand.Seed(time.Now().Unix()) } +func (n *dcSelector) Init(opts ...selector.Option) error { + for _, o := range opts { + o(&n.opts) + } + return nil +} + +func (n *dcSelector) Options() selector.Options { + return n.opts +} + func (n *dcSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) { services, err := n.opts.Registry.GetService(service) if err != nil { diff --git a/examples/client/selector/selector.go b/examples/client/selector/selector.go index b5b3074f..60888d54 100644 --- a/examples/client/selector/selector.go +++ b/examples/client/selector/selector.go @@ -22,6 +22,17 @@ type firstNodeSelector struct { opts selector.Options } +func (n *firstNodeSelector) Init(opts ...selector.Option) error { + for _, o := range opts { + o(&n.opts) + } + return nil +} + +func (n *firstNodeSelector) Options() selector.Options { + return n.opts +} + func (n *firstNodeSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) { services, err := n.opts.Registry.GetService(service) if err != nil { diff --git a/selector/blacklist/black_list_selector.go b/selector/blacklist/black_list_selector.go index 689710f6..66d06859 100644 --- a/selector/blacklist/black_list_selector.go +++ b/selector/blacklist/black_list_selector.go @@ -56,6 +56,17 @@ func (r *blackListSelector) run() { } } +func (r *blackListSelector) Init(opts ...selector.Option) error { + for _, o := range opts { + o(&r.so) + } + return nil +} + +func (r *blackListSelector) Options() selector.Options { + return r.so +} + func (r *blackListSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) { var sopts selector.SelectOptions for _, opt := range opts { diff --git a/selector/random_selector.go b/selector/random_selector.go index fd9f6f1d..dd3babca 100644 --- a/selector/random_selector.go +++ b/selector/random_selector.go @@ -15,6 +15,17 @@ func init() { rand.Seed(time.Now().Unix()) } +func (r *randomSelector) Init(opts ...Option) error { + for _, o := range opts { + o(&r.so) + } + return nil +} + +func (r *randomSelector) Options() Options { + return r.so +} + func (r *randomSelector) Select(service string, opts ...SelectOption) (Next, error) { var sopts SelectOptions for _, opt := range opts { diff --git a/selector/roundrobin/round_robin_selector.go b/selector/roundrobin/round_robin_selector.go index 7ee019dd..54309023 100644 --- a/selector/roundrobin/round_robin_selector.go +++ b/selector/roundrobin/round_robin_selector.go @@ -16,6 +16,17 @@ func init() { cmd.DefaultSelectors["roundrobin"] = NewSelector } +func (r *roundRobinSelector) Init(opts ...selector.Option) error { + for _, o := range opts { + o(&r.so) + } + return nil +} + +func (r *roundRobinSelector) Options() selector.Options { + return r.so +} + func (r *roundRobinSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) { var sopts selector.SelectOptions for _, opt := range opts { diff --git a/selector/selector.go b/selector/selector.go index 8f729941..551b0a41 100644 --- a/selector/selector.go +++ b/selector/selector.go @@ -64,6 +64,8 @@ import ( // and mark their status. This allows host pools and other things // to be built using various algorithms. type Selector interface { + Init(opts ...Option) error + Options() Options // Select returns a function which should return the next node Select(service string, opts ...SelectOption) (Next, error) // Mark sets the success/error against a node