Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMutexOwnershipConflict is an error returned when trying to unlock // a mutex that has been released or is held by another process. ErrMutexOwnershipConflict = errors.New("mutex already released or acquired by someone else") )
Functions ¶
This section is empty.
Types ¶
type KeyDBMutex ¶
type KeyDBMutex struct {
// contains filtered or unexported fields
}
KeyDBMutex provides a mutex mechanism using Redis.
func NewMutex ¶
func NewMutex(client *redis.Client, key string) *KeyDBMutex
NewMutex initializes a new Redis-based mutex. The mutex uses a random value to ensure that only the owner can unlock it.
func (*KeyDBMutex) Lock ¶
func (m *KeyDBMutex) Lock(ctx context.Context) error
Lock tries to obtain the lock. If it's already held, it will keep trying at intervals until the lock is acquired.
type KeyDBRWMutex ¶
type KeyDBRWMutex struct {
// contains filtered or unexported fields
}
func NewRWMutex ¶
func NewRWMutex(client *redis.Client, baseKey string, writeVal string) *KeyDBRWMutex
NewRWMutex initializes a new Redis-based reader-writer mutex. The mutex separates the keys for read and write locks for granular control. It uses a specific value for write locks to ensure that only the owner can unlock it, while read locks increment a counter to manage multiple readers.
func (*KeyDBRWMutex) Lock ¶
func (m *KeyDBRWMutex) Lock(ctx context.Context) error
Lock attempts to acquire a write lock, waiting if there are readers or another writer.
func (*KeyDBRWMutex) RLock ¶
func (m *KeyDBRWMutex) RLock(ctx context.Context) error
RLock attempts to acquire a read lock, waiting if a write lock is held.
func (*KeyDBRWMutex) RUnlock ¶
func (m *KeyDBRWMutex) RUnlock(ctx context.Context)
RUnlock releases a read lock by decrementing the read count.