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

View File

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