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
status-code-override
Casper-Mars 3 years ago committed by chenzhihui
parent d0dd3309db
commit 64c4417de3
  1. 7
      contrib/registry/consul/client.go
  2. 9
      contrib/registry/consul/registry.go

@ -27,6 +27,8 @@ type Client struct {
healthcheckInterval int
// heartbeat enable heartbeat
heartbeat bool
// deregisterCriticalServiceAfter time interval in seconds
deregisterCriticalServiceAfter int
}
// NewClient creates consul client
@ -36,6 +38,7 @@ func NewClient(cli *api.Client) *Client {
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),
})
}

@ -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

Loading…
Cancel
Save