forked from go-redsync/redsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redsync.go
84 lines (72 loc) · 1.95 KB
/
redsync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package redsync
import (
"github.com/go-redis/redis"
"time"
)
// RedSync provides a simple method for creating distributed mutexes using multiple Redis connection pools.
type RedSync struct {
redisClient *redis.Client
}
// New creates and returns a new RedSync instance from given Redis connection pools.
func New(redisClient *redis.Client) *RedSync {
return &RedSync{
redisClient: redisClient,
}
}
// NewMutex returns a new distributed mutex with given name.
func (r *RedSync) NewMutex(name string, options ...Option) *Mutex {
m := &Mutex{
name: name,
expiry: 8 * time.Second,
tries: 32,
delayFunc: func(_ int) time.Duration { return 500 * time.Millisecond },
factor: 0.01,
redisClient: r.redisClient,
}
for _, o := range options {
o.Apply(m)
}
return m
}
// An Option configures a mutex.
type Option interface {
Apply(*Mutex)
}
// OptionFunc is a function that configures a mutex.
type OptionFunc func(*Mutex)
// Apply calls f(mutex)
func (f OptionFunc) Apply(mutex *Mutex) {
f(mutex)
}
// SetExpiry can be used to set the expiry of a mutex to the given value.
func SetExpiry(expiry time.Duration) Option {
return OptionFunc(func(m *Mutex) {
m.expiry = expiry
})
}
// SetTries can be used to set the number of times lock acquire is attempted.
func SetTries(tries int) Option {
return OptionFunc(func(m *Mutex) {
m.tries = tries
})
}
// SetRetryDelay can be used to set the amount of time to wait between retries.
func SetRetryDelay(delay time.Duration) Option {
return OptionFunc(func(m *Mutex) {
m.delayFunc = func(_ int) time.Duration {
return delay
}
})
}
// SetRetryDelayFunc can be used to override default delay behavior.
func SetRetryDelayFunc(delayFunc DelayFunc) Option {
return OptionFunc(func(m *Mutex) {
m.delayFunc = delayFunc
})
}
// SetDriftFactor can be used to set the clock drift factor.
func SetDriftFactor(factor float64) Option {
return OptionFunc(func(m *Mutex) {
m.factor = factor
})
}