From 65a90f5a219683c6bd19045cefc2064056ff77c3 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 21 Mar 2018 18:18:48 +0700 Subject: [PATCH] registry.Register: use local variable to get context value --- registry/consul_registry.go | 21 +++++++++------------ registry/options.go | 4 +--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/registry/consul_registry.go b/registry/consul_registry.go index dda716a4..514e29b1 100644 --- a/registry/consul_registry.go +++ b/registry/consul_registry.go @@ -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), } diff --git a/registry/options.go b/registry/options.go index cb687834..aa9c3f4f 100644 --- a/registry/options.go +++ b/registry/options.go @@ -18,9 +18,7 @@ type Options struct { } type RegisterOptions struct { - TCPCheck bool - TTL time.Duration - Interval time.Duration + TTL time.Duration // Other options for implementations of the interface // can be stored in a context Context context.Context