redsync

package
v0.0.0-...-c1546bb Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: BSD-3-Clause Imports: 7 Imported by: 10

Documentation

Overview

Package redsync provides a Redis-based distributed mutual exclusion lock implementation as described in the blog post http://antirez.com/news/77.

Values containing the types defined in this package should not be copied.

Index

Examples

Constants

View Source
const (
	// DefaultExpiry is used when Mutex Duration is 0
	DefaultExpiry = 8 * time.Second
	// DefaultTries is used when Mutex Duration is 0
	DefaultTries = 16
	// DefaultDelay is used when Mutex Delay is 0
	DefaultDelay = 512 * time.Millisecond
	// DefaultFactor is used when Mutex Factor is 0
	DefaultFactor = 0.01
)

Variables

View Source
var (
	// ErrFailed is returned when lock cannot be acquired
	ErrFailed = errors.New("failed to acquire lock")
)

Functions

This section is empty.

Types

type Locker

type Locker interface {
	Lock() error
	Touch() bool
	Unlock() bool
}

Locker interface with Lock returning an error when lock cannot be aquired

type Mutex

type Mutex struct {
	Name   string        // Resouce name
	Expiry time.Duration // Duration for which the lock is valid, DefaultExpiry if 0

	Tries int           // Number of attempts to acquire lock before admitting failure, DefaultTries if 0
	Delay time.Duration // Delay between two attempts to acquire lock, DefaultDelay if 0

	Factor float64 // Drift factor, DefaultFactor if 0

	Quorum int // Quorum for the lock, set to len(addrs)/2+1 by NewMutex()
	// contains filtered or unexported fields
}

A Mutex is a mutual exclusion lock.

Fields of a Mutex must not be changed after first use.

Example
m, err := redsync.NewMutexWithGenericPool("FlyingSquirrels", pools)
if err != nil {
	panic(err)
}

err = m.Lock()
if err != nil {
	panic(err)
}
defer m.Unlock()
Output:

func NewMutex

func NewMutex(name string, addrs []net.Addr) (*Mutex, error)

NewMutex returns a new Mutex on a named resource connected to the Redis instances at given addresses.

func NewMutexWithGenericPool

func NewMutexWithGenericPool(name string, genericNodes []Pool) (*Mutex, error)

NewMutexWithGenericPool returns a new Mutex on a named resource connected to the Redis instances at given generic Pools. different from NewMutexWithPool to maintain backwards compatibility

func NewMutexWithPool

func NewMutexWithPool(name string, nodes []*redis.Pool) (*Mutex, error)

NewMutexWithPool returns a new Mutex on a named resource connected to the Redis instances at given redis Pools.

func (*Mutex) Lock

func (m *Mutex) Lock() error

Lock locks m. In case it returns an error on failure, you may retry to acquire the lock by calling this method again.

func (*Mutex) Touch

func (m *Mutex) Touch() bool

Touch resets m's expiry to the expiry value. It is a run-time error if m is not locked on entry to Touch. It returns the status of the touch

func (*Mutex) Unlock

func (m *Mutex) Unlock() bool

Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock. It returns the status of the unlock

type Pool

type Pool interface {
	Get() redis.Conn
}

Pool is a generic connection pool

type RedSync

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

RedSync provides mutex handling via a multiple Redis connection pools.

func New

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

New creates and returns a new RedSync instance from given network addresses.

func NewWithGenericPool

func NewWithGenericPool(genericNodes []Pool) *RedSync

NewWithGenericPool creates and returns a new RedSync instance from given generic Pools.

func (*RedSync) NewMutex

func (r *RedSync) NewMutex(name string) *Mutex

NewMutex returns a new Mutex with the given name.

Jump to

Keyboard shortcuts

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