Skip to content

Commit 4cebe43

Browse files
authored
Retry service refresh when consul is restarted (#15)
1 parent b1791c2 commit 4cebe43

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

consulService.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (cp *ConsulClient) RegisterService(serviceInfo ServiceInfo) error {
116116

117117
// Run refresh in background
118118
stopChan := make(chan struct{})
119-
go cp.client.Session().RenewPeriodic(sessCfg.TTL, sessionID, nil, stopChan)
119+
go cp.renewService(keyName, sessCfg.TTL, sessionID, stopChan)
120120

121121
// Store it in DB
122122
cp.serviceDb[keyName] = &consulServiceState{
@@ -240,6 +240,8 @@ func (cp *ConsulClient) DeregisterService(serviceInfo ServiceInfo) error {
240240
return errors.New("Service not found")
241241
}
242242

243+
log.Infof("Deregistering service key: %s, value: %+v", keyName, serviceInfo)
244+
243245
// stop the refresh thread and delete service
244246
close(srvState.stopChan)
245247
delete(cp.serviceDb, keyName)
@@ -255,6 +257,16 @@ func (cp *ConsulClient) DeregisterService(serviceInfo ServiceInfo) error {
255257
}
256258

257259
//--------------------- Internal funcitons -------------------
260+
func (cp *ConsulClient) renewService(keyName, ttl, sessionID string, stopChan chan struct{}) {
261+
for {
262+
err := cp.client.Session().RenewPeriodic(ttl, sessionID, nil, stopChan)
263+
if err == nil {
264+
log.Infof("Stoping renew on %s", keyName)
265+
return
266+
}
267+
log.Infof("RenewPeriodic for session %s exited with error: %v. Retrying..", keyName, err)
268+
}
269+
}
258270

259271
// getServiceInstances gets the current list of service instances
260272
func (cp *ConsulClient) getServiceInstances(key string, waitIdx uint64) ([]ServiceInfo, uint64, error) {

0 commit comments

Comments
 (0)