techdebt: [CODE-3218]: use context.WithoutCancel in contextutil (#3457)

jobatzil/login/xforwardedfor
Marko Gaćeša 2025-02-20 19:20:48 +00:00 committed by Harness
parent e39ae83e78
commit 53142b18a8
7 changed files with 10 additions and 36 deletions

View File

@ -280,10 +280,7 @@ func (c *Controller) CommentApplySuggestions(
// TODO: This is a small change to reduce likelihood of dirty state (e.g. git work done but db canceled).
// We still require a proper solution to handle an application crash or very slow execution times
const timeout = 1 * time.Minute
ctx, cancel := context.WithTimeout(
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
ctx, cancel := contextutil.WithNewTimeout(ctx, timeout)
defer cancel()
// Create internal write params. Note: This will skip the pre-commit protection rules check.

View File

@ -237,10 +237,7 @@ func (c *Controller) Merge(
// TODO: This is a small change to reduce likelihood of dirty state.
// We still require a proper solution to handle an application crash or very slow execution times
// (which could cause an unlocking pre operation completion).
ctx, cancel := context.WithTimeout(
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
ctx, cancel := contextutil.WithNewTimeout(ctx, timeout)
defer cancel()
//nolint:nestif

View File

@ -72,10 +72,7 @@ func (c *Controller) UpdateDefaultBranch(
// create new, time-restricted context to guarantee update completion, even if request is canceled.
// TODO: a proper error handling solution required.
ctx, cancel := context.WithTimeout(
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
ctx, cancel := contextutil.WithNewTimeout(ctx, timeout)
defer cancel()
repoFull, err := c.repoStore.Find(ctx, repo.ID)

View File

@ -61,10 +61,7 @@ func (c *Controller) PurgeNoAuth(
// the max time we give a purge space to succeed
const timeout = 15 * time.Minute
// create new, time-restricted context to guarantee space purge completion, even if request is canceled.
ctx, cancel := context.WithTimeout(
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
ctx, cancel := contextutil.WithNewTimeout(ctx, timeout)
defer cancel()
var toBeDeletedRepos []*types.Repository

View File

@ -360,7 +360,7 @@ func (r *Repository) Handle(ctx context.Context, data string, _ job.ProgressRepo
repo.GitUID = gitUID // make sure to delete the correct directory
if errDel := r.deleteGitRepository(context.Background(), &systemPrincipal, repo); errDel != nil {
if errDel := r.deleteGitRepository(context.WithoutCancel(ctx), &systemPrincipal, repo); errDel != nil {
log.Warn().Err(errDel).
Msg("failed to delete git repository after failed import")
}

View File

@ -74,10 +74,7 @@ func (l Locker) lock(
unlockFn := func() {
// always unlock independent of whether source context got canceled or not
ctx, cancel := context.WithTimeout(
contextutil.WithNewValues(context.Background(), ctx),
30*time.Second,
)
ctx, cancel := contextutil.WithNewTimeout(ctx, 30*time.Second)
defer cancel()
err := mutext.Unlock(ctx)

View File

@ -16,21 +16,10 @@ package contextutil
import (
"context"
"time"
)
// WithNewValues creates a new context derived from originalCtx with values from valuesCtx.
func WithNewValues(originalCtx context.Context, valuesCtx context.Context) context.Context {
return &combinedContext{
Context: originalCtx,
valuesCtx: valuesCtx,
}
}
type combinedContext struct {
context.Context
valuesCtx context.Context
}
func (c *combinedContext) Value(key any) any {
return c.valuesCtx.Value(key)
// WithNewTimeout creates a new context derived from original context, but without canceling and with the new timeout.
func WithNewTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.WithoutCancel(ctx), timeout)
}