/
dsync.go
87 lines (71 loc) · 2.06 KB
/
dsync.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
package redis_dsync
import (
"git.golaxy.org/core/service"
"git.golaxy.org/core/util/option"
"git.golaxy.org/framework/plugins/dsync"
"git.golaxy.org/framework/plugins/log"
"github.com/go-redsync/redsync/v4"
"github.com/go-redsync/redsync/v4/redis/goredis/v9"
"github.com/redis/go-redis/v9"
)
func newDSync(settings ...option.Setting[DSyncOptions]) dsync.IDistSync {
return &_DistSync{
options: option.Make(With.Default(), settings...),
}
}
type _DistSync struct {
options DSyncOptions
servCtx service.Context
client *redis.Client
redSync *redsync.Redsync
}
// InitSP 初始化服务插件
func (s *_DistSync) InitSP(ctx service.Context) {
log.Infof(ctx, "init plugin %q", self.Name)
s.servCtx = ctx
if s.options.RedisClient == nil {
s.client = redis.NewClient(s.configure())
} else {
s.client = s.options.RedisClient
}
_, err := s.client.Ping(ctx).Result()
if err != nil {
log.Panicf(ctx, "ping redis %q failed, %v", s.client, err)
}
s.redSync = redsync.New(goredis.NewPool(s.client))
}
// ShutSP 关闭服务插件
func (s *_DistSync) ShutSP(ctx service.Context) {
log.Infof(ctx, "shut plugin %q", self.Name)
if s.options.RedisClient == nil {
if s.client != nil {
s.client.Close()
}
}
}
// NewMutex returns a new distributed mutex with given name.
func (s *_DistSync) NewMutex(name string, settings ...option.Setting[dsync.DistMutexOptions]) dsync.IDistMutex {
return s.newMutex(name, option.Make(dsync.With.Default(), settings...))
}
// GetSeparator return name path separator.
func (s *_DistSync) GetSeparator() string {
return ":"
}
func (s *_DistSync) configure() *redis.Options {
if s.options.RedisConfig != nil {
return s.options.RedisConfig
}
if s.options.RedisURL != "" {
conf, err := redis.ParseURL(s.options.RedisURL)
if err != nil {
log.Panicf(s.servCtx, "parse redis url %q failed, %s", s.options.RedisURL, err)
}
return conf
}
conf := &redis.Options{}
conf.Username = s.options.CustomUsername
conf.Password = s.options.CustomPassword
conf.Addr = s.options.CustomAddress
conf.DB = s.options.CustomDB
return conf
}