redis_lock

package module
v0.0.0-...-e6636ec Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

redis-lock

Go 实现的 Redis 分布式锁

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToPreemptLock = errors.New("redis-lock: failed to lock")
	ErrLockNotHold         = errors.New("redis-lock: lock not hold")
	ErrLockTimeout         = errors.New("redis-lock: lock timeout")
)

Functions

This section is empty.

Types

type Client

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

Client implements Redis distributed lock

func NewClient

func NewClient(client redis.Cmdable) *Client

NewClient creates a *Client

func (*Client) Lock

func (c *Client) Lock(ctx context.Context,
	key string,
	expiration time.Duration,
	timeout time.Duration, retry RetryStrategy) (*Lock, error)

Lock tries to acquire a lock with timeout and retry strategy

func (*Client) SingleflightLock

func (c *Client) SingleflightLock(ctx context.Context,
	key string,
	expiration time.Duration,
	timeout time.Duration, retry RetryStrategy) (*Lock, error)

SingleflightLock is an enhancement of Lock for high concurrency scenarios with singleflight restrictions for concurrency of a key

func (*Client) TryLock

func (c *Client) TryLock(ctx context.Context,
	key string,
	expiration time.Duration) (*Lock, error)

TryLock tries to acquire a lock

type FixedIntervalRetry

type FixedIntervalRetry struct {
	Interval time.Duration
	Max      int
	// contains filtered or unexported fields
}

func (*FixedIntervalRetry) Next

func (f *FixedIntervalRetry) Next() (time.Duration, bool)

type Lock

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

Lock is responsible for unlocking and refreshing

func (*Lock) AutoRefresh

func (l *Lock) AutoRefresh(interval time.Duration, timeout time.Duration) error

AutoRefresh refreshes automatically at interval, timeout specifies the refresh timeout

func (*Lock) AutoRefreshChan

func (l *Lock) AutoRefreshChan(interval time.Duration, timeout time.Duration) <-chan error

AutoRefreshChan refreshes automatically at interval, timeout specifies the refresh timeout, returns a chan with error

func (*Lock) Refresh

func (l *Lock) Refresh(ctx context.Context) error

Refresh refreshes the lock by expiration

func (*Lock) Unlock

func (l *Lock) Unlock(ctx context.Context) error

Unlock releases the lock

type RetryStrategy

type RetryStrategy interface {
	// Next determines the time interval for Lock
	// and whether Lock to retry
	Next() (time.Duration, bool)
}

RetryStrategy is the retry strategy of Lock

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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