-
Notifications
You must be signed in to change notification settings - Fork 3
/
mutex.go
89 lines (72 loc) · 1.83 KB
/
mutex.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
85
86
87
88
89
// distlock provides simple distributed locks using redis, mysql, postgresql,
// mongodb, etc.
package distlock
import (
"strconv"
"time"
)
type mutex struct {
ns string // namespace, as prefix for the lock name
id string
owner string
lifetime time.Duration
provider Provider
}
func (m *mutex) lockId() string {
return m.ns + ":" + m.id
}
func (m *mutex) GetId() string {
return m.lockId()
}
func (m *mutex) GetOwner() string {
return m.owner
}
func (m *mutex) GetLifetime() time.Duration {
return m.lifetime
}
// Option is an option for tweaking Mutex.
type Option interface {
Apply(*mutex)
}
type optionFunc func(*mutex)
// Apply implements Option interface.
func (f optionFunc) Apply(m *mutex) { f(m) }
// WithNamespace returns an option that configures Mutex.ns.
func WithNamespace(ns string) Option {
return optionFunc(func(m *mutex) {
m.ns = ns
})
}
// WithLockLifetime returns an option that configures Mutex.lifetime.
func WithLockLifetime(lifetime time.Duration) Option {
return optionFunc(func(m *mutex) {
m.lifetime = lifetime
})
}
// NewMutex creates a new Mutex instance.
func newMutex(provider Provider, id string, opts ...Option) Mutex {
m := &mutex{
id: id,
owner: strconv.FormatInt(time.Now().UnixNano(), 10),
provider: provider,
}
WithNamespace("default").Apply(m)
for _, opt := range opts {
opt.Apply(m)
}
return m
}
// String implements print interface.
func (m *mutex) String() string {
return "Mutex(" + m.provider.Name() + ":" + m.GetId() + ")"
}
// Lock locks the named resourc
func (m *mutex) Lock() error {
return m.provider.Lock(m)
}
// Unlock unlocks the named resource.
// Attempts to remove the key so long as the value matches.
// When the key not found or the value was incorrect, unlock will fail.
func (m *mutex) Unlock() error {
return m.provider.Unlock(m)
}