-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgedission.go
61 lines (51 loc) · 1.28 KB
/
gedission.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
package godisson
import (
"fmt"
"github.com/go-redis/redis/v8"
cmap "github.com/orcaman/concurrent-map"
uuid "github.com/satori/go.uuid"
"log"
"time"
)
type Godisson struct {
c *redis.Client
watchDogTimeout time.Duration
uuid string
// key uuid:key, value entry
RenewMap cmap.ConcurrentMap
}
var DefaultWatchDogTimeout = 30 * time.Second
func NewGodisson(redisClient *redis.Client, opts ...OptionFunc) *Godisson {
g := &Godisson{
c: redisClient,
uuid: uuid.NewV4().String(),
watchDogTimeout: DefaultWatchDogTimeout,
RenewMap: cmap.New(),
}
for _, opt := range opts {
opt(g)
}
return g
}
type OptionFunc func(g *Godisson)
func WithWatchDogTimeout(t time.Duration) OptionFunc {
return func(g *Godisson) {
if t.Seconds() < 30 {
t = DefaultWatchDogTimeout
log.Println("watchDogTimeout is too small, so config default ")
}
g.watchDogTimeout = t
}
}
func (g *Godisson) NewRLock(key string) *RLock {
return newRLock(key, g)
}
func (g *Godisson) NewMutex(key string) *Mutex {
return newMutex(key, g)
}
func (g *Godisson) getEntryName(key string) string {
return fmt.Sprintf("%s:%s", g.uuid, key)
}
func (g *Godisson) getChannelName(key string) string {
return fmt.Sprintf("{gedisson_lock__channel}:%s)", key)
}