Skip to content

Commit

Permalink
fix(registry/consul): fix can not find service in 20s
Browse files Browse the repository at this point in the history
  • Loading branch information
letian0805 committed Dec 29, 2021
1 parent 11a6120 commit 4f6326c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 9 additions & 3 deletions contrib/registry/consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ type Client struct {

// resolve service entry endpoints
resolver ServiceResolver
// healthcheck time interval in seconds
healthcheckInterval int
}

// NewClient creates consul client
func NewClient(cli *api.Client) *Client {
c := &Client{cli: cli, resolver: defaultResolver}
c := &Client{
cli: cli,
resolver: defaultResolver,
healthcheckInterval: 10,
}
c.ctx, c.cancel = context.WithCancel(context.Background())
return c
}
Expand Down Expand Up @@ -107,7 +113,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
for _, address := range checkAddresses {
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
TCP: address,
Interval: "20s",
Interval: fmt.Sprintf("%ds", c.healthcheckInterval),
DeregisterCriticalServiceAfter: "70s",
})
}
Expand All @@ -117,7 +123,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
return err
}
go func() {
ticker := time.NewTicker(time.Second * 20)
ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval))
defer ticker.Stop()
for {
select {
Expand Down
9 changes: 9 additions & 0 deletions contrib/registry/consul/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func WithServiceResolver(fn ServiceResolver) Option {
}
}

// WithHealthcheckInterval with healthcheck interval in seconds.
func WithHealthcheckInterval(interval int) Option {
return func(o *Registry) {
if o.cli != nil {
o.cli.healthcheckInterval = interval
}
}
}

// Config is consul registry config
type Config struct {
*api.Config
Expand Down

0 comments on commit 4f6326c

Please sign in to comment.