Skip to content

Commit

Permalink
enhance: add log messages to WithDelayedCancelPropagation
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Oct 12, 2022
1 parent d6f801c commit af2b19d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion executor/linux/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ func (c *client) ExecBuild(ctx context.Context) error {
// runs StreamService or StreamStep in a goroutine.
func (c *client) StreamBuild(ctx context.Context) error {
// cancel streaming after a timeout once the build has finished
delayedCtx, cancelStreaming := context2.WithDelayedCancelPropagation(ctx, c.logStreamingTimeout)
delayedCtx, cancelStreaming := context2.
WithDelayedCancelPropagation(ctx, c.logStreamingTimeout, "streaming", c.Logger)
defer cancelStreaming()

// create an error group with the parent context
Expand Down
13 changes: 12 additions & 1 deletion internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package context
import (
"context"
"time"

"github.com/sirupsen/logrus"
)

func WithDelayedCancelPropagation(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
func WithDelayedCancelPropagation(parent context.Context, timeout time.Duration, name string, logger *logrus.Entry) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())

go func() {
Expand All @@ -18,18 +20,27 @@ func WithDelayedCancelPropagation(parent context.Context, timeout time.Duration)
// start the timer once the parent context is canceled
select {
case <-parent.Done():
logger.Tracef("%s timer for %s started now that parent context is done", name, timeout)
timer = time.NewTimer(timeout)

break
case <-ctx.Done():
logger.Tracef("%s finished before the parent context", name)

return
}

// wait for the timer to elapse or the context to naturally finish.
select {
case <-timer.C:
logger.Tracef("%s timed out, canceling %s", name, name)
cancel()

return
case <-ctx.Done():
logger.Tracef("%s finished, stopping timeout timer", name)
timer.Stop()

return
}
}()
Expand Down

0 comments on commit af2b19d

Please sign in to comment.