|
|
@ -21,11 +21,17 @@ type Client struct { |
|
|
|
|
|
|
|
|
|
|
|
// resolve service entry endpoints
|
|
|
|
// resolve service entry endpoints
|
|
|
|
resolver ServiceResolver |
|
|
|
resolver ServiceResolver |
|
|
|
|
|
|
|
// healthcheck time interval in seconds
|
|
|
|
|
|
|
|
healthcheckInterval int |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewClient creates consul client
|
|
|
|
// NewClient creates consul client
|
|
|
|
func NewClient(cli *api.Client) *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()) |
|
|
|
c.ctx, c.cancel = context.WithCancel(context.Background()) |
|
|
|
return c |
|
|
|
return c |
|
|
|
} |
|
|
|
} |
|
|
@ -107,7 +113,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab |
|
|
|
for _, address := range checkAddresses { |
|
|
|
for _, address := range checkAddresses { |
|
|
|
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{ |
|
|
|
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{ |
|
|
|
TCP: address, |
|
|
|
TCP: address, |
|
|
|
Interval: "20s", |
|
|
|
Interval: fmt.Sprintf("%ds", c.healthcheckInterval), |
|
|
|
DeregisterCriticalServiceAfter: "70s", |
|
|
|
DeregisterCriticalServiceAfter: "70s", |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -116,8 +122,9 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass") |
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
ticker := time.NewTicker(time.Second * 20) |
|
|
|
ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval)) |
|
|
|
defer ticker.Stop() |
|
|
|
defer ticker.Stop() |
|
|
|
for { |
|
|
|
for { |
|
|
|
select { |
|
|
|
select { |
|
|
|