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
pull/1929/head
Casper-Mars 3 years ago committed by GitHub
parent 4f21094a56
commit 19637d9b87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      contrib/registry/consul/client.go
  2. 9
      contrib/registry/consul/registry.go

@ -27,15 +27,18 @@ type Client struct {
healthcheckInterval int healthcheckInterval int
// heartbeat enable heartbeat // heartbeat enable heartbeat
heartbeat bool heartbeat bool
// deregisterCriticalServiceAfter time interval in seconds
deregisterCriticalServiceAfter int
} }
// NewClient creates consul client // NewClient creates consul client
func NewClient(cli *api.Client) *Client { func NewClient(cli *api.Client) *Client {
c := &Client{ c := &Client{
cli: cli, cli: cli,
resolver: defaultResolver, resolver: defaultResolver,
healthcheckInterval: 10, healthcheckInterval: 10,
heartbeat: true, heartbeat: true,
deregisterCriticalServiceAfter: 600,
} }
c.ctx, c.cancel = context.WithCancel(context.Background()) c.ctx, c.cancel = context.WithCancel(context.Background())
return c return c
@ -123,7 +126,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{ asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
TCP: address, TCP: address,
Interval: fmt.Sprintf("%ds", c.healthcheckInterval), Interval: fmt.Sprintf("%ds", c.healthcheckInterval),
DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.healthcheckInterval*60), DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.deregisterCriticalServiceAfter),
Timeout: "5s", Timeout: "5s",
}) })
} }
@ -132,7 +135,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{ asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
CheckID: "service:" + svc.ID, CheckID: "service:" + svc.ID,
TTL: fmt.Sprintf("%ds", c.healthcheckInterval*2), 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 // Config is consul registry config
type Config struct { type Config struct {
*api.Config *api.Config

Loading…
Cancel
Save