|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package globallock |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | +) |
| 10 | + |
| 11 | +type Locker interface { |
| 12 | + // Lock tries to acquire a lock for the given key, it blocks until the lock is acquired or the context is canceled. |
| 13 | + // |
| 14 | + // Lock returns a new context which should be used in the following code. |
| 15 | + // The new context will be canceled when the lock is released or lost - yes, it's possible to lose a lock. |
| 16 | + // For example, it lost the connection to the redis server while holding the lock. |
| 17 | + // If it fails to acquire the lock, the returned context will be the same as the input context. |
| 18 | + // |
| 19 | + // Lock returns a ReleaseFunc to release the lock, it cannot be nil. |
| 20 | + // It's always safe to call this function even if it fails to acquire the lock, and it will do nothing in that case. |
| 21 | + // And it's also safe to call it multiple times, but it will only release the lock once. |
| 22 | + // That's why it's called ReleaseFunc, not UnlockFunc. |
| 23 | + // But be aware that it's not safe to not call it at all; it could lead to a memory leak. |
| 24 | + // So a recommended pattern is to use defer to call it: |
| 25 | + // ctx, release, err := locker.Lock(ctx, "key") |
| 26 | + // if err != nil { |
| 27 | + // return err |
| 28 | + // } |
| 29 | + // defer release() |
| 30 | + // The ReleaseFunc will return the original context which was used to acquire the lock. |
| 31 | + // It's useful when you want to continue to do something after releasing the lock. |
| 32 | + // At that time, the ctx will be canceled, and you can use the returned context by the ReleaseFunc to continue: |
| 33 | + // ctx, release, err := locker.Lock(ctx, "key") |
| 34 | + // if err != nil { |
| 35 | + // return err |
| 36 | + // } |
| 37 | + // doSomething(ctx) |
| 38 | + // ctx = release() |
| 39 | + // doSomethingElse(ctx) |
| 40 | + // Please ignore it and use `defer release()` instead if you don't need this, to avoid forgetting to release the lock. |
| 41 | + // |
| 42 | + // Lock returns an error if failed to acquire the lock. |
| 43 | + // Be aware that even the context is not canceled, it's still possible to fail to acquire the lock. |
| 44 | + // For example, redis is down, or it reached the maximum number of tries. |
| 45 | + Lock(ctx context.Context, key string) (context.Context, ReleaseFunc, error) |
| 46 | + |
| 47 | + // TryLock tries to acquire a lock for the given key, it returns immediately. |
| 48 | + // It follows the same pattern as Lock, but it doesn't block. |
| 49 | + // And if it fails to acquire the lock because it's already locked, not other reasons like redis is down, |
| 50 | + // it will return false without any error. |
| 51 | + TryLock(ctx context.Context, key string) (bool, context.Context, ReleaseFunc, error) |
| 52 | +} |
| 53 | + |
| 54 | +// ReleaseFunc is a function that releases a lock. |
| 55 | +// It returns the original context which was used to acquire the lock. |
| 56 | +type ReleaseFunc func() context.Context |
| 57 | + |
| 58 | +// ErrLockReleased is used as context cause when a lock is released |
| 59 | +var ErrLockReleased = fmt.Errorf("lock released") |
0 commit comments