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 ¶
const ( // DefaultExpiry is used when Mutex Duration is 0 // lock 默认过期时间 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") )
Functions ¶
This section is empty.
Types ¶
type Mutex ¶
type Mutex struct { Name string // Resouce name Expiry time.Duration // Dura锁tion 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 // 获取锁的法定master个数 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 ¶
NewMutex returns a new Mutex on a named resource connected to the Redis instances at given addresses. 基于name资源,返回一个新的锁,该锁通过连接指定地址的redis实例获得
func NewMutexWithGenericPool ¶
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 ¶
NewMutexWithPool returns a new Mutex on a named resource connected to the Redis instances at given redis Pools.
func (*Mutex) Lock ¶
Lock locks m. In case it returns an error on failure, you may retry to acquire the lock by calling this method again. 获取锁,如果返回失败,则可以重新尝试获取
type RedSync ¶
type RedSync struct {
// contains filtered or unexported fields
}
RedSync provides mutex handling via a multiple Redis connection pools.
func NewWithGenericPool ¶
NewWithGenericPool creates and returns a new RedSync instance from given generic Pools.