-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathredisson.go
66 lines (54 loc) · 1.54 KB
/
redisson.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
package redisson
import (
"context"
"runtime"
"strings"
"sync"
"github.com/go-redis/redis/v8"
"github.com/segmentio/ksuid"
"github.com/MaricoHan/redisson/mutex"
"github.com/MaricoHan/redisson/pkg/utils"
"github.com/MaricoHan/redisson/pkg/utils/pubsub"
)
type Redisson struct {
root *mutex.Root
}
func New(ctx context.Context, client *redis.Client) *Redisson {
redisson := &Redisson{
root: &mutex.Root{
Client: client,
UUID: ksuid.New().String(),
RedisChannelName: utils.ChannelName("redisson_pubsub"),
},
}
// 一个实例只建立一个 pubsub 连接
// 额外开协程监听 redis 消息,转发给实例内部基于内存实现的 pubsub,再分配给对应的 subscriber。
pubSub := client.Subscribe(ctx, redisson.root.RedisChannelName)
gCtx, cancel := context.WithCancel(ctx)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
wg.Done()
defer func() { _ = pubSub.Close() }()
var ss []string
for {
select {
case <-gCtx.Done():
return
case msg := <-pubSub.Channel():
ss = strings.Split(msg.Payload, ":")
pubsub.Publish(ss[0], ss[1]) // 0:锁名 1:动作(unlock)
}
}
}()
wg.Wait() // 等待协程启动成功
// 释放资源
runtime.SetFinalizer(redisson, func(_ *Redisson) { cancel() })
return redisson
}
func (r Redisson) NewMutex(name string, options ...mutex.Option) *mutex.Mutex {
return mutex.NewMutex(r.root, name, options...)
}
func (r Redisson) NewRWMutex(name string, options ...mutex.Option) *mutex.RWMutex {
return mutex.NewRWMutex(r.root, name, options...)
}