Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 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
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) 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 ¶
type RedisLockBackend ¶
type RedisLockBackend struct {
// contains filtered or unexported fields
}
func NewRedisLockBackend ¶
func NewRedisLockBackend(pools ...redis.Pool) *RedisLockBackend