Accept a range of addresses

This commit is contained in:
Asim Aslam 2019-06-26 20:51:13 +01:00
parent 940ea94a96
commit 0da8256426
3 changed files with 24 additions and 17 deletions

View File

@ -59,14 +59,14 @@ func (g *grpcClient) next(request client.Request, opts client.CallOptions) (sele
// get proxy address // get proxy address
if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 { if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 {
opts.Address = prx opts.Address = []string{prx}
} }
// return remote address // return remote address
if len(opts.Address) > 0 { if len(opts.Address) > 0 {
return func() (*registry.Node, error) { return func() (*registry.Node, error) {
return &registry.Node{ return &registry.Node{
Address: opts.Address, Address: opts.Address[0],
}, nil }, nil
}, nil }, nil
} }

View File

@ -43,8 +43,8 @@ type Options struct {
type CallOptions struct { type CallOptions struct {
SelectOptions []selector.SelectOption SelectOptions []selector.SelectOption
// Address of remote host // Address of remote hosts
Address string Address []string
// Backoff func // Backoff func
Backoff BackoffFunc Backoff BackoffFunc
// Check if retriable func // 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 // WithAddress sets the remote addresses to use rather than using service discovery
func WithAddress(a string) CallOption { func WithAddress(a ...string) CallOption {
return func(o *CallOptions) { return func(o *CallOptions) {
o.Address = a o.Address = a
} }

View File

@ -283,29 +283,36 @@ func (r *rpcClient) next(request Request, opts CallOptions) (selector.Next, erro
// get proxy address // get proxy address
if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 { if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 {
opts.Address = prx opts.Address = []string{prx}
} }
// return remote address // return remote address
if len(opts.Address) > 0 { if len(opts.Address) > 0 {
address := opts.Address var nodes []*registry.Node
for _, addr := range opts.Address {
address := addr
port := 0 port := 0
host, sport, err := net.SplitHostPort(opts.Address) host, sport, err := net.SplitHostPort(addr)
if err == nil { if err == nil {
address = host address = host
port, _ = strconv.Atoi(sport) port, _ = strconv.Atoi(sport)
} }
return func() (*registry.Node, error) { nodes = append(nodes, &registry.Node{
return &registry.Node{
Address: address, Address: address,
Port: port, Port: port,
// Set the protocol // Set the protocol
Metadata: map[string]string{ Metadata: map[string]string{
"protocol": "mucp", "protocol": "mucp",
}, },
}, nil })
}
// crude return method
return func() (*registry.Node, error) {
return nodes[time.Now().Unix()%int64(len(nodes))], nil
}, nil }, nil
} }