From 0da8256426e865ebd4e31088ebcb535c18b47ece Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 26 Jun 2019 20:51:13 +0100 Subject: [PATCH] Accept a range of addresses --- client/grpc/grpc.go | 4 ++-- client/options.go | 8 ++++---- client/rpc_client.go | 29 ++++++++++++++++++----------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/client/grpc/grpc.go b/client/grpc/grpc.go index 3430fbfc..df4163c7 100644 --- a/client/grpc/grpc.go +++ b/client/grpc/grpc.go @@ -59,14 +59,14 @@ func (g *grpcClient) next(request client.Request, opts client.CallOptions) (sele // get proxy address if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 { - opts.Address = prx + opts.Address = []string{prx} } // return remote address if len(opts.Address) > 0 { return func() (*registry.Node, error) { return ®istry.Node{ - Address: opts.Address, + Address: opts.Address[0], }, nil }, nil } diff --git a/client/options.go b/client/options.go index 5a0f4100..9f363d74 100644 --- a/client/options.go +++ b/client/options.go @@ -43,8 +43,8 @@ type Options struct { type CallOptions struct { SelectOptions []selector.SelectOption - // Address of remote host - Address string + // Address of remote hosts + Address []string // Backoff func Backoff BackoffFunc // Check if retriable func @@ -245,8 +245,8 @@ func WithExchange(e string) PublishOption { } } -// WithAddress sets the remote address to use rather than using service discovery -func WithAddress(a string) CallOption { +// WithAddress sets the remote addresses to use rather than using service discovery +func WithAddress(a ...string) CallOption { return func(o *CallOptions) { o.Address = a } diff --git a/client/rpc_client.go b/client/rpc_client.go index 36b3dcf7..c724435a 100644 --- a/client/rpc_client.go +++ b/client/rpc_client.go @@ -283,29 +283,36 @@ func (r *rpcClient) next(request Request, opts CallOptions) (selector.Next, erro // get proxy address if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 { - opts.Address = prx + opts.Address = []string{prx} } // return remote address if len(opts.Address) > 0 { - address := opts.Address - port := 0 + var nodes []*registry.Node - host, sport, err := net.SplitHostPort(opts.Address) - if err == nil { - address = host - port, _ = strconv.Atoi(sport) - } + for _, addr := range opts.Address { + address := addr + port := 0 - return func() (*registry.Node, error) { - return ®istry.Node{ + host, sport, err := net.SplitHostPort(addr) + if err == nil { + address = host + port, _ = strconv.Atoi(sport) + } + + nodes = append(nodes, ®istry.Node{ Address: address, Port: port, // Set the protocol Metadata: map[string]string{ "protocol": "mucp", }, - }, nil + }) + } + + // crude return method + return func() (*registry.Node, error) { + return nodes[time.Now().Unix()%int64(len(nodes))], nil }, nil }