mirror of https://github.com/harness/drone.git
techdebt: [CODE-3218]: use context.WithoutCancel in contextutil (#3457)
parent
e39ae83e78
commit
53142b18a8
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue