Allow setting of timeout for registry
This commit is contained in:
parent
539b3d045c
commit
d7b3765c71
@ -12,6 +12,7 @@ import (
|
|||||||
type consulRegistry struct {
|
type consulRegistry struct {
|
||||||
Address string
|
Address string
|
||||||
Client *consul.Client
|
Client *consul.Client
|
||||||
|
Options Options
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeEndpoints(en []*Endpoint) []string {
|
func encodeEndpoints(en []*Endpoint) []string {
|
||||||
@ -69,7 +70,20 @@ func decodeMetadata(tags []string) map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newConsulRegistry(addrs []string, opts ...Option) Registry {
|
func newConsulRegistry(addrs []string, opts ...Option) Registry {
|
||||||
|
var opt Options
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// use default config
|
||||||
config := consul.DefaultConfig()
|
config := consul.DefaultConfig()
|
||||||
|
|
||||||
|
// set timeout
|
||||||
|
if opt.Timeout > 0 {
|
||||||
|
config.HttpClient.Timeout = opt.Timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if there are any addrs
|
||||||
if len(addrs) > 0 {
|
if len(addrs) > 0 {
|
||||||
addr, port, err := net.SplitHostPort(addrs[0])
|
addr, port, err := net.SplitHostPort(addrs[0])
|
||||||
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
|
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
|
||||||
@ -79,11 +93,14 @@ func newConsulRegistry(addrs []string, opts ...Option) Registry {
|
|||||||
config.Address = fmt.Sprintf("%s:%s", addr, port)
|
config.Address = fmt.Sprintf("%s:%s", addr, port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create the client
|
||||||
client, _ := consul.NewClient(config)
|
client, _ := consul.NewClient(config)
|
||||||
|
|
||||||
cr := &consulRegistry{
|
cr := &consulRegistry{
|
||||||
Address: config.Address,
|
Address: config.Address,
|
||||||
Client: client,
|
Client: client,
|
||||||
|
Options: opt,
|
||||||
}
|
}
|
||||||
|
|
||||||
return cr
|
return cr
|
||||||
@ -178,7 +195,7 @@ func (c *consulRegistry) GetService(name string) ([]*Service, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *consulRegistry) ListServices() ([]*Service, error) {
|
func (c *consulRegistry) ListServices() ([]*Service, error) {
|
||||||
rsp, _, err := c.Client.Catalog().Services(&consul.QueryOptions{})
|
rsp, _, err := c.Client.Catalog().Services(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
15
registry/options.go
Normal file
15
registry/options.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
Timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func Timeout(t time.Duration) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Timeout = t
|
||||||
|
}
|
||||||
|
}
|
@ -8,9 +8,7 @@ type Registry interface {
|
|||||||
Watch() (Watcher, error)
|
Watch() (Watcher, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type options struct{}
|
type Option func(*Options)
|
||||||
|
|
||||||
type Option func(*options)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultRegistry = newConsulRegistry([]string{})
|
DefaultRegistry = newConsulRegistry([]string{})
|
||||||
|
Loading…
Reference in New Issue
Block a user