redsync.go: github.com/hjr265/redsync.go/redsync Index | Examples | Files

package redsync

import "github.com/hjr265/redsync.go/redsync"

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

Package Files

mutex.go

Constants

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

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

type Locker Uses

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

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

type Mutex Uses

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.

Code:

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

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

func NewMutex Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

type Pool interface {
    Get() redis.Conn
}

Pool is a generic connection pool

type RedSync Uses

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

RedSync provides mutex handling via a multiple Redis connection pools.

func New Uses

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

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

func NewWithGenericPool Uses

func NewWithGenericPool(genericNodes []Pool) *RedSync

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

func (*RedSync) NewMutex Uses

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

NewMutex returns a new Mutex with the given name.

Package redsync imports 7 packages (graph) and is imported by 3 packages. Updated 2019-12-10. Refresh now. Tools for package owners.