concurrency

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorLockNotAcquired is returned when a lock cannot be acquired. This means the lock is already held.
	ErrorLockNotAcquired = errors.New("go-generics: concurrency: lock not acquired")
	// ErrorLockNotRefreshed is returned when a lock cannot be refreshed. This means the lock is not held or has expired.
	ErrorLockNotRefreshed = errors.New("go-generics: concurrency: lock not refreshed")
	// ErrorLockNotReleased is returned when trying to release an inactive lock.
	ErrorLockNotReleased = errors.New("go-generics: concurrency: lock not held")
)

Functions

func WrapError

func WrapError(err error, message string) error

Types

type ConcurrencyLimiter

type ConcurrencyLimiter struct {
	// contains filtered or unexported fields
}

ConcurrencyLimiter will allow you to manage a maximum number of concurrently executing goroutines

func NewConcurrencyLimiter

func NewConcurrencyLimiter(maxConcurrency int) *ConcurrencyLimiter

func (*ConcurrencyLimiter) Run

func (rp *ConcurrencyLimiter) Run(routine func(), opts ...ConcurrencyLimiterRunOption)

Run will schedule the current function as a go routine if concurrency is available. If no concurrency is available, this function will block until other scheduled goroutines exit. If concurrency is available, the routine will be started and the Run() will immediately return.

type ConcurrencyLimiterRunConfig

type ConcurrencyLimiterRunConfig struct {
	// contains filtered or unexported fields
}

ConcurrencyLimiterRunConfig allows you to configure the behavior of the ConcurrencyLimiter.Run() function

type ConcurrencyLimiterRunOption

type ConcurrencyLimiterRunOption func(cfg ConcurrencyLimiterRunConfig) ConcurrencyLimiterRunConfig

func WithOnCompleteCallback

func WithOnCompleteCallback(onComplete func()) ConcurrencyLimiterRunOption

WithOnCompleteCallback - This option will allow you to specify a callback function that will be called after the goroutine exits

type Lock

type Lock interface {
	Release(ctx context.Context) error
	Refresh(ctx context.Context) error
}

Lock defines the interface for a lock.

type LockBackend

type LockBackend interface {
	ObtainLock(ctx context.Context, name string, ttl time.Duration) (Lock, error)
}

LockBackend defines the interface for lock storage backends

type LockError

type LockError struct {
	Message string
}

LockError represents an error that occurred during locking operations

func (LockError) Error

func (le LockError) Error() string

type LockManager

type LockManager struct {
	// contains filtered or unexported fields
}

func NewLockManager

func NewLockManager(backend LockBackend) *LockManager

NewLockManager creates a new LockManager with the provided backend

func (*LockManager) Acquire

func (lm *LockManager) Acquire(ctx context.Context, key string, ttl time.Duration) (Lock, bool, error)

func (*LockManager) ExecuteWithLock

func (lm *LockManager) ExecuteWithLock(ctx context.Context, key string, lockTTL, timeout time.Duration, fn func() error) error

ExecuteWithLock attempts to acquire a lock for the given key and executes the provided function. If the lock cannot be acquired immediately, it will retry with exponential backoff until the timeout is reached. If the lock is acquired, it will ensure that the lock is released after the function execution. If the function returns an error, it will be propagated back to the caller.

type LockTimeoutError

type LockTimeoutError struct {
	Key string
}

LockTimeoutError represents an error when a lock operation times out

func (LockTimeoutError) Error

func (lte LockTimeoutError) Error() string

type MemoryLockBackend

type MemoryLockBackend struct {
	// contains filtered or unexported fields
}

func NewMemoryLockBackend

func NewMemoryLockBackend() *MemoryLockBackend

func (*MemoryLockBackend) ObtainLock

func (mlb *MemoryLockBackend) ObtainLock(ctx context.Context, name string, ttl time.Duration) (Lock, error)

type RedisLockBackend

type RedisLockBackend struct {
	// contains filtered or unexported fields
}

func NewRedisLockBackend

func NewRedisLockBackend(pools ...redis.Pool) *RedisLockBackend

func (*RedisLockBackend) ObtainLock

func (r *RedisLockBackend) ObtainLock(ctx context.Context, name string, ttl time.Duration) (Lock, error)

Jump to

Keyboard shortcuts

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