Skip to content

Commit

Permalink
Use a context with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
samanbarghi committed May 15, 2023
1 parent 1240df2 commit a1af8e8
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions service/history/workflow/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package cache

import (
"context"
"fmt"
"sync/atomic"
"time"
"unicode/utf8"
Expand All @@ -38,6 +39,7 @@ import (

"go.temporal.io/server/common/cache"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
Expand Down Expand Up @@ -198,12 +200,25 @@ func (c *CacheImpl) getOrCreateWorkflowExecutionInternal(
// Consider revisiting this if it causes too much GC activity
releaseFunc := c.makeReleaseFunc(key, workflowCtx, forceClearContext, lockPriority)

if err := workflowCtx.Lock(ctx, lockPriority); err != nil {
var timeout time.Time
if deadline, ok := ctx.Deadline(); ok {
// TODO: what is our tail time?
timeout = deadline
}

if headers.GetCallerInfo(ctx).CallerType == headers.CallerTypeBackground {
timeout = time.Now().Add(500 * time.Millisecond)
}
ctxWithDeadline, cancel := context.WithDeadline(ctx, timeout)
defer cancel()

if err := workflowCtx.Lock(ctxWithDeadline, lockPriority); err != nil {
// ctx is done before lock can be acquired
c.Release(key)
handler.Counter(metrics.CacheFailures.GetMetricName()).Record(1)
handler.Counter(metrics.AcquireLockFailedCounter.GetMetricName()).Record(1)
return nil, nil, err
// TODO: what error return here?
return nil, nil, fmt.Errorf("Workflow is busy")
}
return workflowCtx, releaseFunc, nil
}
Expand Down

0 comments on commit a1af8e8

Please sign in to comment.