Skip to content

Commit 95e8faf

Browse files
committed
Fix CPU hogging bug in Sleep activity.
Heartbeating was incorrectly structured to fire at nanosecond intervals rather than seconds. Unfortunately this timer would also fire even when heartbeating was meant ot be disabled. h/t to @Quinn-With-Two-Ns for spotting the issue.
1 parent 6d63403 commit 95e8faf

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

activities/activity.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,31 @@ type SleepActivityInput struct {
1414
func SleepActivity(ctx context.Context, input SleepActivityInput) error {
1515
sleepTimer := time.After(time.Duration(input.SleepTimeInSeconds) * time.Second)
1616
heartbeatTimeout := activity.GetInfo(ctx).HeartbeatTimeout
17+
18+
// If no heartbeat timeout is set, we don't need heartbeating
1719
if heartbeatTimeout == 0 {
18-
// If no heartbeat timeout is set, assume we don't want heartbeating.
19-
// Set the heartbeat time longer than our sleep time so that we never send a heartbeat.
20-
heartbeatTimeout = time.Duration(input.SleepTimeInSeconds) * 2
20+
for {
21+
select {
22+
case <-sleepTimer:
23+
return nil
24+
case <-ctx.Done():
25+
return ctx.Err()
26+
}
27+
}
2128
}
29+
30+
// Create ticker only when heartbeating is needed
2231
heartbeatTick := time.Duration(0.8 * float64(heartbeatTimeout))
23-
t := time.NewTicker(heartbeatTick)
32+
ticker := time.NewTicker(heartbeatTick)
33+
defer ticker.Stop()
2434

2535
for {
2636
select {
2737
case <-sleepTimer:
2838
return nil
2939
case <-ctx.Done():
3040
return ctx.Err()
31-
case <-t.C:
41+
case <-ticker.C:
3242
activity.RecordHeartbeat(ctx)
3343
}
3444
}

0 commit comments

Comments
 (0)