redislock

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

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

Go to latest
Published: Sep 21, 2021 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

redislock

This package is originally by https://github.com/dineshgowda24
Thank you to Dinesh for creating this.

License

Simplified distributed locking implementation using Redis. For more information, please see examples.

This intro is written by Dinesh.

Motivation

I came across a concurrency issue when multiple clients were accessing single redis instance. So I wanted a primitive locking solution, but redis did not have one implemented. So started looking for open source libraries and found redislock very well written and effective library. But it still did not solve my problem as I was using redigo client but the package used go-redis. Although redigo had redsync, I wanted a much more simpler one and so with redislock.

Features

  • Simple and easy to use interface.
  • Plug in any redis client of your choice by implementing the RedisClient interface.
  • Simple but effective locking for single redis instance.

Examples

Check out examples in for redigo and go-redis clients.

Documentation

Full documentation is available on GoDoc

Contribution

Feel free to send a PR.

Documentation

Index

Constants

View Source
const (
	LuaRefreshScript = `if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("pexpire", KEYS[1], ARGV[2]) else return 0 end`
	LuaReleaseScript = `if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end`
	LuaPTTLScript    = `if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("pttl", KEYS[1]) else return -3 end`
)

lua scripts which should be loaded to redis client when implementing RedisClient interface

Variables

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

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

Functions

This section is empty.

Types

type Client

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

func New

func New(redisClient RedisClient) *Client

// New creates a new Client instance with a custom namespace.

func (*Client) Obtain

func (c *Client) Obtain(key string, ttl time.Duration, opt *Options) (*Lock, error)

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

type Lock

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

func Obtain

func Obtain(redisClient RedisClient, 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 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(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() error

Release manually releases the lock. May return ErrLockNotHeld.

func (*Lock) TTL

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

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

	// Optional context for Obtain timeout and cancellation control.
	Context context.Context
}

Options describe the options for the lock

type RedisClient

type RedisClient interface {
	SetNX(key, value string, ttl time.Duration) (bool, error)
	Refresh(key, value string, ttl string) error
	Release(key, value string) error
	TTL(key, value string) (int64, error)
}

Implement the interface with which every redis client you wish to use

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.

Directories

Path Synopsis
examples
goredis command
redigo command

Jump to

Keyboard shortcuts

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