Skip to content

Commit

Permalink
fix: heartbeat timeout (#1240)
Browse files Browse the repository at this point in the history
  • Loading branch information
brushknight authored Jun 28, 2022
1 parent 38e34ad commit 841750e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/integrations/v4/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func (r *runner) execute(ctx context.Context, matches *databind.Values, discover
var act contexts.Actuator
ctx, act = contexts.WithHeartBeat(ctx, def.Timeout, r.log)
r.setHeartBeat(act.HeartBeat)
defer act.HeartBeatStop()
}

// add hostID in the context to fetch and set in executor
Expand Down
14 changes: 12 additions & 2 deletions pkg/helpers/contexts/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ type Actuator struct {
// Cancel cancels the context
Cancel context.CancelFunc
// HeartBeat extends the context life time by the value the context was created with
HeartBeat func()
HeartBeat func()
HeartBeatStop func()
}

// WithHeartBeat with return a context that is automatically cancelled if the HeartBeat function
// from the returned Actuator is not invoked periodically before the passed timeout expires.
func WithHeartBeat(parent context.Context, timeout time.Duration, lg log.Entry) (context.Context, Actuator) {
ctx := heartBeatCtx{lifeTime: timeout}
actuator := Actuator{HeartBeat: ctx.heartBeat}
actuator := Actuator{
HeartBeat: ctx.heartBeat,
HeartBeatStop: ctx.heartBeatStop,
}
ctx.Context, actuator.Cancel = context.WithCancel(parent)
ctx.timer = time.AfterFunc(timeout, func() {
lg.Warnf("HeartBeat timeout exceeded after %f seconds", timeout.Seconds())
Expand All @@ -48,3 +52,9 @@ func (ctx *heartBeatCtx) heartBeat() {
}
ctx.timer.Reset(ctx.lifeTime)
}

func (ctx *heartBeatCtx) heartBeatStop() {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
ctx.timer.Stop()
}

0 comments on commit 841750e

Please sign in to comment.