lock

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DistLock

type DistLock interface {
	TryLock(ctx context.Context, key string) (bool, error)
	TryLockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, error)
	Lock(ctx context.Context, key string) error
	Unlock(ctx context.Context, key string) error
}

type LocalLock

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

func NewLocalLock

func NewLocalLock() *LocalLock

func (*LocalLock) Lock

func (l *LocalLock) Lock(ctx context.Context, key string) error

func (*LocalLock) TryLock

func (l *LocalLock) TryLock(ctx context.Context, key string) (bool, error)

func (*LocalLock) TryLockWithTTL

func (l *LocalLock) TryLockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, error)

func (*LocalLock) Unlock

func (l *LocalLock) Unlock(ctx context.Context, key string) error

type RedisLock

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

func NewRedisLock

func NewRedisLock(addr, password string, db int) *RedisLock

func (*RedisLock) Client

func (l *RedisLock) Client() *redis.Client

func (*RedisLock) Close

func (l *RedisLock) Close() error

func (*RedisLock) ExtendLock

func (l *RedisLock) ExtendLock(ctx context.Context, key string, ttl time.Duration) error

func (*RedisLock) ExtendLockWithValue added in v1.5.5

func (l *RedisLock) ExtendLockWithValue(ctx context.Context, key, value string, ttl time.Duration) (bool, error)

ExtendLockWithValue 续期:仅当 value 匹配(自己持有)才延长 TTL,返回是否成功续期。 供 watchdog 在长任务期间周期性续期,避免锁过期被别人抢走。

func (*RedisLock) Lock

func (l *RedisLock) Lock(ctx context.Context, key string) error

func (*RedisLock) LockWithTTL

func (l *RedisLock) LockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, string, error)

func (*RedisLock) Ping

func (l *RedisLock) Ping(ctx context.Context) error

func (*RedisLock) TryLock

func (l *RedisLock) TryLock(ctx context.Context, key string) (bool, error)

func (*RedisLock) TryLockWithTTL

func (l *RedisLock) TryLockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, error)

func (*RedisLock) Unlock

func (l *RedisLock) Unlock(ctx context.Context, key string) error

func (*RedisLock) UnlockWithValue

func (l *RedisLock) UnlockWithValue(ctx context.Context, key, value string) error

type Semaphore

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

func NewSemaphore

func NewSemaphore(client *redis.Client, key string, max int, slotTTL time.Duration) *Semaphore

NewSemaphore 创建一个基于 redis ZSET 的分布式信号量。 slotTTL 为单个槽的存活时长:持有者需周期性 Renew,进程崩溃后槽在 slotTTL 后自动释放。

func (*Semaphore) Acquire

func (s *Semaphore) Acquire(ctx context.Context, identifier string) (bool, error)

func (*Semaphore) Cleanup

func (s *Semaphore) Cleanup(ctx context.Context, olderThan time.Duration) error

func (*Semaphore) Release

func (s *Semaphore) Release(ctx context.Context, identifier string) error

func (*Semaphore) Renew added in v1.5.5

func (s *Semaphore) Renew(ctx context.Context, identifier string) error

Renew 续期持有的槽(把 score 推到 now+slotTTL),供 watchdog 调用。

Jump to

Keyboard shortcuts

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