globallock

package
v0.0.0-...-949612f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LockAndDo

func LockAndDo(ctx context.Context, key string, f func(context.Context) error) error

LockAndDo tries to acquire a lock for the given key and then calls the given function. It uses the default locker, and it will return an error if failed to acquire the lock.

func TryLockAndDo

func TryLockAndDo(ctx context.Context, key string, f func(context.Context) error) (bool, error)

TryLockAndDo tries to acquire a lock for the given key and then calls the given function. It uses the default locker, and it will return false if failed to acquire the lock.

Types

type Locker

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, redis 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 redis is down,
	// it will return false without any error.
	TryLock(ctx context.Context, key string) (bool, ReleaseFunc, error)
}

func DefaultLocker

func DefaultLocker() Locker

DefaultLocker returns the default locker.

func NewMemoryLocker

func NewMemoryLocker() Locker

func NewRedisLocker

func NewRedisLocker(connection string) Locker

type ReleaseFunc

type ReleaseFunc func()

ReleaseFunc is a function that releases a lock.

func Lock

func Lock(ctx context.Context, key string) (ReleaseFunc, error)

Lock tries to acquire a lock for the given key, it uses the default locker. Read the documentation of Locker.Lock for more information about the behavior.

func TryLock

func TryLock(ctx context.Context, key string) (bool, ReleaseFunc, error)

TryLock tries to acquire a lock for the given key, it uses the default locker. Read the documentation of Locker.TryLock for more information about the behavior.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL