registry.Register: use local variable to get context value

This commit is contained in:
Shulhan 2018-03-21 18:18:48 +07:00
parent 1eb4398b6c
commit 65a90f5a21
2 changed files with 10 additions and 15 deletions

View File

@ -14,10 +14,6 @@ import (
hash "github.com/mitchellh/hashstructure"
)
const (
ConsulRegisterTCPCheckKey = "consul_register_tcp_check"
)
type consulRegistry struct {
Address string
Client *consul.Client
@ -137,17 +133,18 @@ func (c *consulRegistry) Register(s *Service, opts ...RegisterOption) error {
return errors.New("Require at least one node")
}
var regTCPCheck bool
var regInterval time.Duration
var options RegisterOptions
for _, o := range opts {
o(&options)
}
if c.opts.Context != nil {
tcpCheckInterval, ok := c.opts.Context.
Value(ConsulRegisterTCPCheckKey).(time.Duration)
if ok {
options.TCPCheck = true
options.Interval = tcpCheckInterval
if tcpCheckInterval, ok := c.opts.Context.Value("consul_register_tcp_check").(time.Duration); ok {
regTCPCheck = true
regInterval = tcpCheckInterval
}
}
@ -181,12 +178,12 @@ func (c *consulRegistry) Register(s *Service, opts ...RegisterOption) error {
var check *consul.AgentServiceCheck
if options.TCPCheck {
deregTTL := getDeregisterTTL(options.Interval)
if regTCPCheck {
deregTTL := getDeregisterTTL(regInterval)
check = &consul.AgentServiceCheck{
TCP: fmt.Sprintf("%s:%d", node.Address, node.Port),
Interval: fmt.Sprintf("%v", options.Interval),
Interval: fmt.Sprintf("%v", regInterval),
DeregisterCriticalServiceAfter: fmt.Sprintf("%v", deregTTL),
}

View File

@ -18,9 +18,7 @@ type Options struct {
}
type RegisterOptions struct {
TCPCheck bool
TTL time.Duration
Interval time.Duration
// Other options for implementations of the interface
// can be stored in a context
Context context.Context