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
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 &registry.Node{
Address: opts.Address,
Address: opts.Address[0],
}, nil
}, nil
}

View File

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

View File

@ -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 &registry.Node{
host, sport, err := net.SplitHostPort(addr)
if err == nil {
address = host
port, _ = strconv.Atoi(sport)
}
nodes = append(nodes, &registry.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
}