mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
40 lines
1.6 KiB
Go
40 lines
1.6 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package globallock
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type Locker interface {
|
|
// Lock tries to acquire a lock for the given key, it blocks until the lock is acquired or the context is canceled.
|
|
//
|
|
// Lock returns a ReleaseFunc to release the lock, it cannot be nil.
|
|
// It's always safe to call this function even if it fails to acquire the lock, and it will do nothing in that case.
|
|
// And it's also safe to call it multiple times, but it will only release the lock once.
|
|
// That's why it's called ReleaseFunc, not UnlockFunc.
|
|
// But be aware that it's not safe to not call it at all; it could lead to a memory leak.
|
|
// So a recommended pattern is to use defer to call it:
|
|
// release, err := locker.Lock(ctx, "key")
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// defer release()
|
|
//
|
|
// Lock returns an error if failed to acquire the lock.
|
|
// Be aware that even the context is not canceled, it's still possible to fail to acquire the lock.
|
|
// For example, the KV server is down, or it reached the maximum number of tries.
|
|
Lock(ctx context.Context, key string) (ReleaseFunc, error)
|
|
|
|
// TryLock tries to acquire a lock for the given key, it returns immediately.
|
|
// It follows the same pattern as Lock, but it doesn't block.
|
|
// And if it fails to acquire the lock because it's already locked, not other reasons like the KV server being down,
|
|
// it will return false without any error.
|
|
TryLock(ctx context.Context, key string) (bool, ReleaseFunc, error)
|
|
}
|
|
|
|
// ReleaseFunc is a function that releases a lock.
|
|
type ReleaseFunc func()
|