From 19637d9b87cb457f05b08cfce8ed7d32f65098d8 Mon Sep 17 00:00:00 2001 From: Casper-Mars <50834595+Casper-Mars@users.noreply.github.com> Date: Sun, 10 Apr 2022 16:14:33 +0800 Subject: [PATCH] feat(registry): consul client add DeregisterCriticalServiceAfter option (#1917) * feat(registry/consul):add DeregisterCriticalServiceAfter option * fix(cmd/protoc-gen-go-errors):fix lint problem (#1919) use cases.Title instead of strings.Title --- contrib/registry/consul/client.go | 15 +++++++++------ contrib/registry/consul/registry.go | 9 +++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/contrib/registry/consul/client.go b/contrib/registry/consul/client.go index edbc6944d..80ea383a6 100644 --- a/contrib/registry/consul/client.go +++ b/contrib/registry/consul/client.go @@ -27,15 +27,18 @@ type Client struct { healthcheckInterval int // heartbeat enable heartbeat heartbeat bool + // deregisterCriticalServiceAfter time interval in seconds + deregisterCriticalServiceAfter int } // NewClient creates consul client func NewClient(cli *api.Client) *Client { c := &Client{ - cli: cli, - resolver: defaultResolver, - healthcheckInterval: 10, - heartbeat: true, + cli: cli, + resolver: defaultResolver, + healthcheckInterval: 10, + heartbeat: true, + deregisterCriticalServiceAfter: 600, } c.ctx, c.cancel = context.WithCancel(context.Background()) return c @@ -123,7 +126,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab asr.Checks = append(asr.Checks, &api.AgentServiceCheck{ TCP: address, Interval: fmt.Sprintf("%ds", c.healthcheckInterval), - DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.healthcheckInterval*60), + DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.deregisterCriticalServiceAfter), Timeout: "5s", }) } @@ -132,7 +135,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab asr.Checks = append(asr.Checks, &api.AgentServiceCheck{ CheckID: "service:" + svc.ID, TTL: fmt.Sprintf("%ds", c.healthcheckInterval*2), - DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.healthcheckInterval*60), + DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.deregisterCriticalServiceAfter), }) } diff --git a/contrib/registry/consul/registry.go b/contrib/registry/consul/registry.go index c3bc25466..7e454ea24 100644 --- a/contrib/registry/consul/registry.go +++ b/contrib/registry/consul/registry.go @@ -54,6 +54,15 @@ func WithHealthCheckInterval(interval int) Option { } } +// WithDeregisterCriticalServiceAfter with deregister-critical-service-after in seconds. +func WithDeregisterCriticalServiceAfter(interval int) Option { + return func(o *Registry) { + if o.cli != nil { + o.cli.deregisterCriticalServiceAfter = interval + } + } +} + // Config is consul registry config type Config struct { *api.Config