gitea源码

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package globallock
  4. import (
  5. "context"
  6. )
  7. type Locker interface {
  8. // Lock tries to acquire a lock for the given key, it blocks until the lock is acquired or the context is canceled.
  9. //
  10. // Lock returns a ReleaseFunc to release the lock, it cannot be nil.
  11. // It's always safe to call this function even if it fails to acquire the lock, and it will do nothing in that case.
  12. // And it's also safe to call it multiple times, but it will only release the lock once.
  13. // That's why it's called ReleaseFunc, not UnlockFunc.
  14. // But be aware that it's not safe to not call it at all; it could lead to a memory leak.
  15. // So a recommended pattern is to use defer to call it:
  16. // release, err := locker.Lock(ctx, "key")
  17. // if err != nil {
  18. // return err
  19. // }
  20. // defer release()
  21. //
  22. // Lock returns an error if failed to acquire the lock.
  23. // Be aware that even the context is not canceled, it's still possible to fail to acquire the lock.
  24. // For example, redis is down, or it reached the maximum number of tries.
  25. Lock(ctx context.Context, key string) (ReleaseFunc, error)
  26. // TryLock tries to acquire a lock for the given key, it returns immediately.
  27. // It follows the same pattern as Lock, but it doesn't block.
  28. // And if it fails to acquire the lock because it's already locked, not other reasons like redis is down,
  29. // it will return false without any error.
  30. TryLock(ctx context.Context, key string) (bool, ReleaseFunc, error)
  31. }
  32. // ReleaseFunc is a function that releases a lock.
  33. type ReleaseFunc func()