-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdistlock.go
63 lines (56 loc) · 1.64 KB
/
distlock.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
package distlock
import (
"errors"
"fmt"
"sync"
"github.com/go-locks/distlock/driver"
"github.com/go-locks/distlock/mutex"
)
type Distlock struct {
sync.Mutex
prefix string
driver driver.IDriver
mtxMap map[string]*mutex.Mutex
rwMtxMap map[string]*mutex.RWMutex
}
func New(driver driver.IDriver, optFuncs ...OptFunc) *Distlock {
dl := Distlock{
driver: driver,
prefix: "distlock-",
mtxMap: make(map[string]*mutex.Mutex),
rwMtxMap: make(map[string]*mutex.RWMutex),
}
for _, optFunc := range optFuncs {
optFunc(&dl)
}
return &dl
}
func (dl *Distlock) buildName(name string) string {
return dl.prefix + name
}
func (dl *Distlock) NewMutex(name string, optFuncs ...mutex.OptFunc) (*mutex.Mutex, error) {
dl.Lock()
defer dl.Unlock()
if _, ok := dl.mtxMap[name]; ok {
return dl.mtxMap[name], nil
} else if _, ok := dl.rwMtxMap[name]; ok {
return nil, fmt.Errorf("a rw mutex named '%s' already exist, instead of mutex", name)
}
dl.mtxMap[name] = mutex.NewMutex(dl.buildName(name), dl.driver, optFuncs...)
return dl.mtxMap[name], nil
}
func (dl *Distlock) NewRWMutex(name string, optFuncs ...mutex.OptFunc) (*mutex.RWMutex, error) {
dl.Lock()
defer dl.Unlock()
rwDriver, ok := dl.driver.(driver.IRWDriver)
if !ok {
return nil, errors.New("the driver is not a rw driver, so you can not create rw mutex")
}
if _, ok := dl.rwMtxMap[name]; ok {
return dl.rwMtxMap[name], nil
} else if _, ok := dl.mtxMap[name]; ok {
return nil, fmt.Errorf("a mutex named '%s' already exist, instead of rw mutex", name)
}
dl.rwMtxMap[name] = mutex.NewRWMutex(dl.buildName(name), rwDriver, optFuncs...)
return dl.rwMtxMap[name], nil
}