|
|
@ -23,6 +23,8 @@ type Client struct { |
|
|
|
resolver ServiceResolver |
|
|
|
resolver ServiceResolver |
|
|
|
// healthcheck time interval in seconds
|
|
|
|
// healthcheck time interval in seconds
|
|
|
|
healthcheckInterval int |
|
|
|
healthcheckInterval int |
|
|
|
|
|
|
|
// heartbeat enable heartbeat
|
|
|
|
|
|
|
|
heartbeat bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewClient creates consul client
|
|
|
|
// NewClient creates consul client
|
|
|
@ -31,6 +33,7 @@ func NewClient(cli *api.Client) *Client { |
|
|
|
cli: cli, |
|
|
|
cli: cli, |
|
|
|
resolver: defaultResolver, |
|
|
|
resolver: defaultResolver, |
|
|
|
healthcheckInterval: 10, |
|
|
|
healthcheckInterval: 10, |
|
|
|
|
|
|
|
heartbeat: true, |
|
|
|
} |
|
|
|
} |
|
|
|
c.ctx, c.cancel = context.WithCancel(context.Background()) |
|
|
|
c.ctx, c.cancel = context.WithCancel(context.Background()) |
|
|
|
return c |
|
|
|
return c |
|
|
@ -123,18 +126,20 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass") |
|
|
|
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass") |
|
|
|
go func() { |
|
|
|
if c.heartbeat { |
|
|
|
ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval)) |
|
|
|
go func() { |
|
|
|
defer ticker.Stop() |
|
|
|
ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval)) |
|
|
|
for { |
|
|
|
defer ticker.Stop() |
|
|
|
select { |
|
|
|
for { |
|
|
|
case <-ticker.C: |
|
|
|
select { |
|
|
|
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass") |
|
|
|
case <-ticker.C: |
|
|
|
case <-c.ctx.Done(): |
|
|
|
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass") |
|
|
|
return |
|
|
|
case <-c.ctx.Done(): |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
}() |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|