Skip to content

Commit

Permalink
Use time.After again (before, I had just put back the use of sleep).
Browse files Browse the repository at this point in the history
  • Loading branch information
scr-oath committed Dec 3, 2024
1 parent da58603 commit 47f9583
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions hooks/hookexecution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ func executeHook[H any, P any](
startTime := time.Now()
hookId := HookID{ModuleCode: hw.Module, HookImplCode: hw.Code}

ctx, cancel := context.WithTimeout(executionCtx.ctx, timeout)
defer cancel()

// Only execute the hook if it's not already canceled
ctx := executionCtx.ctx
if ctx.Err() == nil {
var cancel context.CancelCauseFunc
ctx, cancel = context.WithCancelCause(ctx)
defer cancel(context.Canceled)

// Execute the hook in the background
go func() {
defer func() {
Expand Down Expand Up @@ -140,16 +142,17 @@ func executeHook[H any, P any](
res.ExecutionTime = time.Since(startTime)
resp <- res
return
case <-ctx.Done():
// fall through to the error handler
case <-time.After(timeout):
cancel(context.DeadlineExceeded)
break
case <-rejected:
return
}
}

// Handle the context error case - either immediately, or after timeout.
theResp := hookResponse[P]{
Err: ctx.Err(),
Err: context.Cause(ctx),
ExecutionTime: time.Since(startTime),
HookID: hookId,
Result: hookstage.HookResult[P]{},
Expand Down

0 comments on commit 47f9583

Please sign in to comment.