dislock

package module
v0.0.0-...-49dc3de Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: MIT Imports: 12 Imported by: 0

README

disLock redis分布式锁

实现了一个基于redigo的分布式锁(distribute lock)支持自动续期

使用方式:

import(
    "github.com/danliznh/dislock"
)

查看examples.go

支持红锁redlock

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotObtained is returned when a lock cannot be obtained.
	ErrNotObtained = errors.New("dislock: not obtained")

	// ErrLockNotHeld is returned when trying to release an inactive lock.
	ErrLockNotHeld = errors.New("dislock: lock not held")
)

Functions

This section is empty.

Types

type DisLockClient

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

func New

func New(conn redis.Conn) *DisLockClient

func (*DisLockClient) Obtain

func (c *DisLockClient) Obtain(ctx context.Context, key string, ttl time.Duration, opt *Options) (*Lock, error)

Obtain tries to obtain a new lock using a unit_key with the given TTL. May return ErrNotObtained if not successful.

func (*DisLockClient) Refresh

func (c *DisLockClient) Refresh(ctx context.Context, key, value string, ttl time.Duration) error

func (*DisLockClient) Release

func (c *DisLockClient) Release(ctx context.Context, key, value string) error

type Lock

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

Lock represents an obtained, distributed lock.

func Obtain

func Obtain(ctx context.Context, conn redis.Conn, key string, ttl time.Duration, opt *Options) (*Lock, error)

Obtain is a short-cut for New(...).Obtain(...).

func (*Lock) Key

func (l *Lock) Key() string

Key returns the redis unit_key used by the lock.

func (*Lock) Metadata

func (l *Lock) Metadata() string

Metadata returns the metadata of the lock.

func (*Lock) Refresh

func (l *Lock) Refresh(ctx context.Context, ttl time.Duration, opt *Options) error

Refresh extends the lock with a new TTL. May return ErrNotObtained if refresh is unsuccessful.

func (*Lock) Release

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

Release manually releases the lock. May return ErrLockNotHeld.

func (*Lock) TTL

func (l *Lock) TTL(ctx context.Context) (time.Duration, error)

TTL returns the remaining time-to-live. Returns 0 if the lock has expired.

func (*Lock) Token

func (l *Lock) Token() string

Token returns the token value set by the lock.

type Options

type Options struct {
	// RetryStrategy allows to customise the lock retry strategy.
	// Default: do not retry
	RetryStrategy RetryStrategy

	// Metadata string is appended to the lock token.
	Metadata string
}

Options describe the options for the lock

type RedLock

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

func (*RedLock) Lock

func (lock *RedLock) Lock(ctx context.Context, key string, ttl time.Duration, obtainTtl time.Duration, opt *Options) error

obtainTtl is extremely smaller than ttl, 50ms-100ms is recommended

func (*RedLock) Refresh

func (lock *RedLock) Refresh(ctx context.Context, ttl time.Duration)

func (*RedLock) Release

func (lock *RedLock) Release(ctx context.Context)

type RedSync

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

func NewRedSync

func NewRedSync(addrs []net.Addr) *RedSync

func (*RedSync) NewRedLock

func (red *RedSync) NewRedLock() *RedLock

type RetryStrategy

type RetryStrategy interface {
	// NextBackoff returns the next backoff duration.
	NextBackoff() time.Duration
}

RetryStrategy allows to customise the lock retry strategy.

func ExponentialBackoff

func ExponentialBackoff(min, max time.Duration) RetryStrategy

ExponentialBackoff strategy is an optimization strategy with a retry time of 2**n milliseconds (n means number of times). You can set a minimum and maximum value, the recommended minimum value is not less than 16ms.

func LimitRetry

func LimitRetry(s RetryStrategy, max int) RetryStrategy

LimitRetry limits the number of retries to max attempts.

func LinearBackoff

func LinearBackoff(backoff time.Duration) RetryStrategy

LinearBackoff allows retries regularly with customized intervals

func NoRetry

func NoRetry() RetryStrategy

NoRetry acquire the lock only once.

Jump to

Keyboard shortcuts

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