Documentation ¶
Overview ¶
Package gcslock acquires a forward-looking lock leveraging a file in a Google Cloud Storage bucket. Unlike a mutex, the lock is TTL-based instead of "held" like a traditional mutex.
Compared to other mutexes, this is intended to be a long-lived lock. The minimum granularity is "seconds" and most consumers will acquire a lock for "minutes" or "hours". Because of clock skew and network latency, granularity below 1s is not a supported use case.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Lock ¶ added in v0.1.2
type Lock struct {
// contains filtered or unexported fields
}
Lock represents a remote forward-looking lock in Google Cloud Storage.
func New ¶
New creates a new distributed locking handler on the specific object in Google Cloud. It does create the lock until Acquire is called.
func (*Lock) Acquire ¶ added in v0.1.2
Acquire attempts to acquire the lock. It returns [ErrLockHeld] if the lock is already held. Callers can cast the error type to get more specific information like the TTL expiration time:
if err := lock.Acquire(ctx, 5*time.Minute); err != nil { var lockErr *ErrLockHeld if errors.As(err, &lockErr) { log.Printf("lock is held until %s", lockErr.NotBefore()) } }
It automatically retries transient upstream API errors, but returns immediately for errors that are irrecoverable.
type LockHeldError ¶
type LockHeldError struct {
// contains filtered or unexported fields
}
LockHeldError is a specific error returned when a lock is alread held.
func (*LockHeldError) Error ¶
func (e *LockHeldError) Error() string
Error implements the error interface.
func (*LockHeldError) Is ¶
func (e *LockHeldError) Is(err error) bool
Is implements the error comparison interface.
func (*LockHeldError) NotBefore ¶
func (e *LockHeldError) NotBefore() time.Time
NotBefore returns the UTC Unix timestamp of when the lock expires.