From a2c862dd210bdc4799898eec5d5374886ad8c6b2 Mon Sep 17 00:00:00 2001 From: Asim Date: Wed, 9 Dec 2015 12:44:38 +0000 Subject: [PATCH] Add service random --- registry/random_selector.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/registry/random_selector.go b/registry/random_selector.go index a5b012d7..7f4d60b1 100644 --- a/registry/random_selector.go +++ b/registry/random_selector.go @@ -48,7 +48,15 @@ func (r *randomSelector) Select(service string, opts ...SelectOption) (SelectNex } return func() (*Node, error) { - return nodes[rand.Int()%len(nodes)], nil + i := rand.Int() + j := i % len(services) + + if len(services[j].Nodes) == 0 { + return nil, ErrNotFound + } + + k := i % len(services[j].Nodes) + return services[j].Nodes[k], nil }, nil }