From b4f0c1ad6a93b0eeeae254d6676a17a8934a4bea Mon Sep 17 00:00:00 2001 From: Travis Ralston <travpc@gmail.com> Date: Sun, 28 May 2023 20:36:53 -0600 Subject: [PATCH] Retry upload lock in case it's exclusively held --- pipline/_steps/upload/lock.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/pipline/_steps/upload/lock.go b/pipline/_steps/upload/lock.go index 9aaa7529..6a9f12d2 100644 --- a/pipline/_steps/upload/lock.go +++ b/pipline/_steps/upload/lock.go @@ -8,11 +8,28 @@ import ( "github.com/turt2live/matrix-media-repo/redislib" ) +const maxLockAttemptTime = 30 * time.Second + func LockForUpload(ctx rcontext.RequestContext, hash string) (func() error, error) { - mutex := redislib.GetMutex(hash, 5*time.Minute) + mutex := redislib.GetMutex(hash, 1*time.Minute) if mutex != nil { - if err := mutex.LockContext(ctx.Context); err != nil { - return nil, errors.New("failed to acquire upload lock: " + err.Error()) + attemptDoneAt := time.Now().Add(maxLockAttemptTime) + acquired := false + for !acquired { + if chErr := ctx.Context.Err(); chErr != nil { + return nil, chErr + } + if err := mutex.LockContext(ctx.Context); err != nil { + ctx.Log.Warn("failed to acquire upload lock") + if time.Now().After(attemptDoneAt) { + return nil, errors.New("failed to acquire upload lock: " + err.Error()) + } + } else { + acquired = true + } + } + if !acquired { + return nil, errors.New("failed to acquire upload lock: timeout") } return func() error { b, err := mutex.UnlockContext(ctx.Context) -- GitLab