-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
container.go
205 lines (184 loc) · 5.67 KB
/
container.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
package eredis
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/elog"
)
type Option func(c *Container)
type Container struct {
config *config
name string
logger *elog.Component
}
// DefaultContainer 定义了默认Container配置
func DefaultContainer() *Container {
return &Container{
config: DefaultConfig(),
logger: elog.EgoLogger.With(elog.FieldComponent(PackageName)),
}
}
// Load 载入配置,初始化Container
func Load(key string) *Container {
c := DefaultContainer()
if err := econf.UnmarshalKey(key, &c.config); err != nil {
c.logger.Panic("parse config error", elog.FieldErr(err), elog.FieldKey(key))
return c
}
c.logger = c.logger.With(elog.FieldComponentName(key))
c.name = key
return c
}
// Build 构建Component
func (c *Container) Build(options ...Option) *Component {
options = append(options, withInterceptor(fixedInterceptor(c.name, c.config, c.logger)))
if c.config.Debug {
options = append(options, withInterceptor(debugInterceptor(c.name, c.config, c.logger)))
}
if c.config.EnableMetricInterceptor {
options = append(options, withInterceptor(metricInterceptor(c.name, c.config, c.logger)))
}
if c.config.EnableAccessInterceptor {
options = append(options, withInterceptor(accessInterceptor(c.name, c.config, c.logger)))
}
if c.config.EnableTraceInterceptor {
options = append(options, withInterceptor(traceInterceptor(c.name, c.config, c.logger)))
}
for _, option := range options {
option(c)
}
redis.SetLogger(c)
var client redis.Cmdable
switch c.config.Mode {
case ClusterMode:
if len(c.config.Addrs) == 0 {
c.logger.Panic(`invalid "addrs" config, "addrs" has none addresses but with cluster mode"`)
}
obj := c.buildCluster()
client = obj
// store db
instances.Store(c.name, &storeRedis{
ClientCluster: obj,
})
case StubMode:
if c.config.Addr == "" {
c.logger.Panic(`invalid "addr" config, "addr" is empty but with stub mode"`)
}
obj := c.buildStub()
client = obj
// store db
instances.Store(c.name, &storeRedis{
ClientStub: obj,
})
case SentinelMode:
if len(c.config.Addrs) == 0 {
c.logger.Panic(`invalid "addrs" config, "addrs" has none addresses but with sentinel mode"`)
}
if c.config.MasterName == "" {
c.logger.Panic(`invalid "masterName" config, "masterName" is empty but with sentinel mode"`)
}
obj := c.buildSentinel()
client = obj
// store db
instances.Store(c.name, &storeRedis{
ClientStub: obj,
})
default:
c.logger.Panic(`redis mode must be one of ("stub", "cluster", "sentinel")`)
}
c.logger = c.logger.With(elog.FieldAddr(fmt.Sprintf("%s", c.config.Addrs)))
return &Component{
config: c.config,
client: client,
lockClient: &lockClient{client: client},
logger: c.logger,
}
}
func (c *Container) buildCluster() *redis.ClusterClient {
clusterClient := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: c.config.Addrs,
MaxRedirects: c.config.MaxRetries,
ReadOnly: c.config.ReadOnly,
Password: c.config.Password,
MaxRetries: c.config.MaxRetries,
DialTimeout: c.config.DialTimeout,
ReadTimeout: c.config.ReadTimeout,
WriteTimeout: c.config.WriteTimeout,
PoolSize: c.config.PoolSize,
MinIdleConns: c.config.MinIdleConns,
IdleTimeout: c.config.IdleTimeout,
TLSConfig: c.config.Authentication.TLSConfig(),
})
for _, incpt := range c.config.interceptors {
clusterClient.AddHook(incpt)
}
if err := clusterClient.Ping(context.Background()).Err(); err != nil {
switch c.config.OnFail {
case "panic":
c.logger.Panic("start cluster redis", elog.FieldErr(err))
default:
c.logger.Error("start cluster redis", elog.FieldErr(err))
}
}
return clusterClient
}
func (c *Container) buildSentinel() *redis.Client {
sentinelClient := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: c.config.MasterName,
SentinelAddrs: c.config.Addrs,
SentinelPassword: c.config.SentinelPassword,
Password: c.config.Password,
DB: c.config.DB,
MaxRetries: c.config.MaxRetries,
DialTimeout: c.config.DialTimeout,
ReadTimeout: c.config.ReadTimeout,
WriteTimeout: c.config.WriteTimeout,
PoolSize: c.config.PoolSize,
MinIdleConns: c.config.MinIdleConns,
IdleTimeout: c.config.IdleTimeout,
TLSConfig: c.config.Authentication.TLSConfig(),
})
for _, incpt := range c.config.interceptors {
sentinelClient.AddHook(incpt)
}
if err := sentinelClient.Ping(context.Background()).Err(); err != nil {
switch c.config.OnFail {
case "panic":
c.logger.Panic("start sentinel redis", elog.FieldErr(err))
default:
c.logger.Error("start sentinel redis", elog.FieldErr(err))
}
}
return sentinelClient
}
func (c *Container) buildStub() *redis.Client {
stubClient := redis.NewClient(&redis.Options{
Addr: c.config.Addr,
Password: c.config.Password,
DB: c.config.DB,
MaxRetries: c.config.MaxRetries,
DialTimeout: c.config.DialTimeout,
ReadTimeout: c.config.ReadTimeout,
WriteTimeout: c.config.WriteTimeout,
PoolSize: c.config.PoolSize,
MinIdleConns: c.config.MinIdleConns,
IdleTimeout: c.config.IdleTimeout,
TLSConfig: c.config.Authentication.TLSConfig(),
})
for _, incpt := range c.config.interceptors {
stubClient.AddHook(incpt)
}
if err := stubClient.Ping(context.Background()).Err(); err != nil {
switch c.config.OnFail {
case "panic":
c.logger.Panic("start stub redis", elog.FieldErr(err))
default:
c.logger.Error("start stub redis", elog.FieldErr(err))
}
}
return stubClient
}
func (c *Container) Printf(ctx context.Context, format string, v ...interface{}) {
c.logger.Infof(format, v...)
}