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

This commit is contained in:
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). // 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 // We still require a proper solution to handle an application crash or very slow execution times
const timeout = 1 * time.Minute const timeout = 1 * time.Minute
ctx, cancel := context.WithTimeout( ctx, cancel := contextutil.WithNewTimeout(ctx, timeout)
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
defer cancel() defer cancel()
// Create internal write params. Note: This will skip the pre-commit protection rules check. // 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. // 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 // We still require a proper solution to handle an application crash or very slow execution times
// (which could cause an unlocking pre operation completion). // (which could cause an unlocking pre operation completion).
ctx, cancel := context.WithTimeout( ctx, cancel := contextutil.WithNewTimeout(ctx, timeout)
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
defer cancel() defer cancel()
//nolint:nestif //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. // create new, time-restricted context to guarantee update completion, even if request is canceled.
// TODO: a proper error handling solution required. // TODO: a proper error handling solution required.
ctx, cancel := context.WithTimeout( ctx, cancel := contextutil.WithNewTimeout(ctx, timeout)
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
defer cancel() defer cancel()
repoFull, err := c.repoStore.Find(ctx, repo.ID) 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 // the max time we give a purge space to succeed
const timeout = 15 * time.Minute const timeout = 15 * time.Minute
// create new, time-restricted context to guarantee space purge completion, even if request is canceled. // create new, time-restricted context to guarantee space purge completion, even if request is canceled.
ctx, cancel := context.WithTimeout( ctx, cancel := contextutil.WithNewTimeout(ctx, timeout)
contextutil.WithNewValues(context.Background(), ctx),
timeout,
)
defer cancel() defer cancel()
var toBeDeletedRepos []*types.Repository 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 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). log.Warn().Err(errDel).
Msg("failed to delete git repository after failed import") Msg("failed to delete git repository after failed import")
} }

View File

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

View File

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