/
config.go
215 lines (198 loc) · 5.99 KB
/
config.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package redis
import (
"github.com/go-redis/redis"
"github.com/xqk/ox/pkg/conf"
"github.com/xqk/ox/pkg/olog"
"github.com/xqk/ox/pkg/util/otime"
"time"
)
const (
//ClusterMode using clusterClient
ClusterMode string = "cluster"
//StubMode using reidsClient
StubMode string = "stub"
)
// Config for redis, contains RedisStubConfig and RedisClusterConfig
type Config struct {
// Addrs 实例配置地址
Addrs []string `json:"addrs"`
// Addr stubConfig 实例配置地址
Addr string `json:"addr"`
// Mode Redis模式 cluster|stub
Mode string `json:"mode"`
// Password 密码
Password string `json:"password"`
// DB,默认为0, 一般应用不推荐使用DB分片
DB int `json:"db"`
// PoolSize 集群内每个节点的最大连接池限制 默认每个CPU10个连接
PoolSize int `json:"poolSize"`
// MaxRetries 网络相关的错误最大重试次数 默认8次
MaxRetries int `json:"maxRetries"`
// MinIdleConns 最小空闲连接数
MinIdleConns int `json:"minIdleConns"`
// DialTimeout 拨超时时间
DialTimeout time.Duration `json:"dialTimeout"`
// ReadTimeout 读超时 默认3s
ReadTimeout time.Duration `json:"readTimeout"`
// WriteTimeout 读超时 默认3s
WriteTimeout time.Duration `json:"writeTimeout"`
// IdleTimeout 连接最大空闲时间,默认60s, 超过该时间,连接会被主动关闭
IdleTimeout time.Duration `json:"idleTimeout"`
// Debug开关
Debug bool `json:"debug"`
// ReadOnly 集群模式 在从属节点上启用读模式
ReadOnly bool `json:"readOnly"`
// 是否开启链路追踪,开启以后。使用DoCotext的请求会被trace
EnableTrace bool `json:"enableTrace"`
// 慢日志门限值,超过该门限值的请求,将被记录到慢日志中
SlowThreshold time.Duration `json:"slowThreshold"`
// OnDialError panic|error
OnDialError string `json:"level"`
logger *olog.Logger
}
// DefaultRedisConfig default config ...
func DefaultRedisConfig() Config {
return Config{
DB: 0,
PoolSize: 10,
MaxRetries: 3,
MinIdleConns: 100,
DialTimeout: otime.Duration("1s"),
ReadTimeout: otime.Duration("1s"),
WriteTimeout: otime.Duration("1s"),
IdleTimeout: otime.Duration("60s"),
ReadOnly: false,
Debug: false,
EnableTrace: false,
SlowThreshold: otime.Duration("250ms"),
OnDialError: "panic",
logger: olog.OxLogger,
}
}
// StdRedisConfig ...
func StdRedisConfig(name string) Config {
return RawRedisConfig("ox.redis." + name)
}
// RawRedisConfig ...
func RawRedisConfig(key string) Config {
var config = DefaultRedisConfig()
if err := conf.UnmarshalKey(key, &config); err != nil {
olog.Panic("unmarshal redisConfig",
olog.String("key", key),
olog.Any("redisConfig", config),
olog.String("error", err.Error()))
}
return config
}
// Build ...
func (config Config) Build() *Redis {
count := len(config.Addrs)
if count < 1 {
config.logger.Panic("no address in redis config", olog.Any("config", config))
}
if len(config.Mode) == 0 {
config.Mode = StubMode
if count > 1 {
config.Mode = ClusterMode
}
}
var client redis.Cmdable
switch config.Mode {
case ClusterMode:
if count == 1 {
config.logger.Warn("redis config has only 1 address but with cluster mode")
}
client = config.buildCluster()
case StubMode:
if count > 1 {
config.logger.Warn("redis config has more than 1 address but with stub mode")
}
client = config.buildStub()
default:
config.logger.Panic("redis mode must be one of (stub, cluster)")
}
return &Redis{
Config: &config,
Client: client,
}
}
func (config Config) buildStub() *redis.Client {
stubClient := redis.NewClient(&redis.Options{
Addr: config.Addrs[0],
Password: config.Password,
DB: config.DB,
MaxRetries: config.MaxRetries,
DialTimeout: config.DialTimeout,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
PoolSize: config.PoolSize,
MinIdleConns: config.MinIdleConns,
IdleTimeout: config.IdleTimeout,
})
if err := stubClient.Ping().Err(); err != nil {
switch config.OnDialError {
case "panic":
config.logger.Panic("dial redis fail", olog.Any("err", err), olog.Any("config", config))
default:
config.logger.Error("dial redis fail", olog.Any("err", err), olog.Any("config", config))
}
}
return stubClient
}
func (config Config) buildCluster() *redis.ClusterClient {
clusterClient := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: config.Addrs,
MaxRedirects: config.MaxRetries,
ReadOnly: config.ReadOnly,
Password: config.Password,
MaxRetries: config.MaxRetries,
DialTimeout: config.DialTimeout,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
PoolSize: config.PoolSize,
MinIdleConns: config.MinIdleConns,
IdleTimeout: config.IdleTimeout,
})
if err := clusterClient.Ping().Err(); err != nil {
switch config.OnDialError {
case "panic":
config.logger.Panic("start cluster redis", olog.Any("err", err))
default:
config.logger.Error("start cluster redis", olog.Any("err", err))
}
}
return clusterClient
}
// StdRedisStubConfig ...
func StdRedisStubConfig(name string) Config {
return RawRedisStubConfig("ox.redis." + name + ".stub")
}
// RawRedisStubConfig ...
func RawRedisStubConfig(key string) Config {
var config = DefaultRedisConfig()
if err := conf.UnmarshalKey(key, &config); err != nil {
config.logger.Panic("unmarshal config",
olog.String("key", key),
olog.Any("config", config),
olog.Any("error", err))
}
config.Addrs = []string{config.Addr}
config.Mode = StubMode
return config
}
// StdRedisClusterConfig ...
func StdRedisClusterConfig(name string) Config {
return RawRedisClusterConfig("ox.redis." + name + ".cluster")
}
// RawRedisClusterConfig ...
func RawRedisClusterConfig(key string) Config {
var config = DefaultRedisConfig()
if err := conf.UnmarshalKey(key, &config); err != nil {
config.logger.Panic("unmarshal config",
olog.String("key", key),
olog.Any("config", config),
olog.Any("error", err))
}
config.Mode = ClusterMode
return config
}