-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency.go
278 lines (249 loc) · 7.33 KB
/
dependency.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package queue
import (
"context"
"fmt"
"runtime"
"time"
"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/events"
"github.com/DoNewsCode/core/otredis"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/kit/metrics"
"github.com/oklog/run"
"github.com/pkg/errors"
)
/*
Providers returns a set JobFrom dependencies related to queue. It includes the
DispatcherMaker, the JobDispatcher and the exported configs.
Depends On:
contract.ConfigAccessor
contract.Dispatcher
Driver `optional:"true"`
otredis.Maker `optional:"true"`
log.Logger
contract.AppName
contract.Env
Gauge `optional:"true"`
Provides:
DispatcherMaker
DispatcherFactory
JobDispatcher
*Queue
*/
func Providers(optionFunc ...ProvidersOptionFunc) di.Deps {
option := &providersOption{}
for _, f := range optionFunc {
f(option)
}
return []interface{}{
provideDispatcherFactory(option),
provideConfig,
provideDispatcher,
di.Bind(new(DispatcherFactory), new(DispatcherMaker)),
}
}
// Gauge is an alias used for dependency injection
type Gauge metrics.Gauge
// ConsumableDispatcher is the key of *Queue in the dependencies graph. Used as a type hint for injection.
type ConsumableDispatcher interface {
JobDispatcher
Consume(ctx context.Context) error
}
// configuration is the struct for queue configs.
type configuration struct {
RedisName string `yaml:"redisName" json:"redisName"`
Parallelism int `yaml:"parallelism" json:"parallelism"`
CheckQueueLengthIntervalSecond int `yaml:"checkQueueLengthIntervalSecond" json:"checkQueueLengthIntervalSecond"`
}
// makerIn is the injection parameters for provideDispatcherFactory
type makerIn struct {
di.In
Conf contract.ConfigAccessor
JobDispatcher JobDispatcher `optional:"true"`
EventDispatcher contract.Dispatcher `optional:"true"`
Logger log.Logger
Gauge Gauge `optional:"true"`
Populator contract.DIPopulator `optional:"true"`
}
// makerOut is the di output JobFrom provideDispatcherFactory
type makerOut struct {
di.Out
DispatcherFactory DispatcherFactory
}
func (d makerOut) ModuleSentinel() {}
func (m makerOut) Module() interface{} { return m }
// provideDispatcherFactory is a provider for *DispatcherFactory and *Queue.
// It also provides an interface for each.
func provideDispatcherFactory(option *providersOption) func(p makerIn) (makerOut, error) {
if option.driverConstructor == nil {
option.driverConstructor = newDefaultDriver
}
return func(p makerIn) (makerOut, error) {
var (
err error
queueConfs map[string]configuration
)
err = p.Conf.Unmarshal("queue", &queueConfs)
if err != nil {
level.Warn(p.Logger).Log("err", err)
}
factory := di.NewFactory(func(name string) (di.Pair, error) {
var (
ok bool
conf configuration
)
p := p
if conf, ok = queueConfs[name]; !ok {
if name != "default" {
return di.Pair{}, fmt.Errorf("queue configuration %s not found", name)
}
conf = configuration{
Parallelism: runtime.NumCPU(),
CheckQueueLengthIntervalSecond: 0,
}
}
if p.JobDispatcher == nil {
p.JobDispatcher = &SyncDispatcher{}
}
if p.EventDispatcher == nil {
p.EventDispatcher = &events.SyncDispatcher{}
}
if p.Gauge != nil {
p.Gauge = p.Gauge.With("queue", name)
}
var driver = option.driver
if option.driver == nil {
driver, err = option.driverConstructor(
DriverArgs{
Name: name,
Populator: p.Populator,
},
)
if err != nil {
return di.Pair{}, err
}
}
queuedDispatcher := NewQueue(
driver,
UseLogger(p.Logger),
UseParallelism(conf.Parallelism),
UseGauge(p.Gauge, time.Duration(conf.CheckQueueLengthIntervalSecond)*time.Second),
UseJobDispatcher(p.JobDispatcher),
UseEventDispatcher(p.EventDispatcher),
)
return di.Pair{
Closer: nil,
Conn: queuedDispatcher,
}, nil
})
// Queue must be created eagerly, so that the consumer goroutines can start on boot up.
for name := range queueConfs {
factory.Make(name)
}
dispatcherFactory := DispatcherFactory{Factory: factory}
return makerOut{
DispatcherFactory: dispatcherFactory,
}, nil
}
}
// ProvideRunGroup implements container.RunProvider.
func (d makerOut) ProvideRunGroup(group *run.Group) {
for name := range d.DispatcherFactory.List() {
queueName := name
ctx, cancel := context.WithCancel(context.Background())
group.Add(func() error {
consumer, err := d.DispatcherFactory.Make(queueName)
if err != nil {
return err
}
return consumer.Consume(ctx)
}, func(err error) {
cancel()
})
}
}
func newDefaultDriver(args DriverArgs) (Driver, error) {
var injected struct {
di.In
contract.AppName
contract.Env
contract.Logger
otredis.Maker
contract.ConfigUnmarshaler
}
if args.Populator == nil {
return nil, errors.New("the default driver requires setting the populator in DI container")
}
if err := args.Populator.Populate(&injected); err != nil {
return nil, fmt.Errorf("missing dependency for the default queue driver: %w", err)
}
driver, err := driverFromDI(args.Populator)
if err != nil {
return nil, fmt.Errorf("error fetching default driver from DI: %w", err)
}
if driver != nil {
return driver, nil
}
var redisName string
if err := injected.ConfigUnmarshaler.Unmarshal(fmt.Sprintf("queue.%s.redisName", injected.AppName), &redisName); err != nil {
return nil, fmt.Errorf("bad configuration: %w", err)
}
client, err := injected.Maker.Make(redisName)
if err != nil {
return nil, fmt.Errorf("the default driver requires the redis client called %s: %w", redisName, err)
}
return &RedisDriver{
Logger: injected.Logger,
RedisClient: client,
ChannelConfig: ChannelConfig{
Delayed: fmt.Sprintf("{%s:%s:%s}:delayed", injected.AppName.String(), injected.Env.String(), args.Name),
Failed: fmt.Sprintf("{%s:%s:%s}:failed", injected.AppName.String(), injected.Env.String(), args.Name),
Reserved: fmt.Sprintf("{%s:%s:%s}:reserved", injected.AppName.String(), injected.Env.String(), args.Name),
Waiting: fmt.Sprintf("{%s:%s:%s}:waiting", injected.AppName.String(), injected.Env.String(), args.Name),
Timeout: fmt.Sprintf("{%s:%s:%s}:timeout", injected.AppName.String(), injected.Env.String(), args.Name),
},
}, nil
}
type dispatcherOut struct {
di.Out
QueueableDispatcher *Queue
}
func provideDispatcher(maker DispatcherMaker) (dispatcherOut, error) {
dispatcher, err := maker.Make("default")
return dispatcherOut{
QueueableDispatcher: dispatcher,
}, err
}
type configOut struct {
di.Out
Config []config.ExportedConfig `group:"config,flatten"`
}
func provideConfig() configOut {
configs := []config.ExportedConfig{{
Owner: "queue",
Data: map[string]interface{}{
"queue": map[string]configuration{
"default": {
RedisName: "default",
Parallelism: runtime.NumCPU(),
CheckQueueLengthIntervalSecond: 15,
},
},
},
}}
return configOut{Config: configs}
}
func driverFromDI(populator contract.DIPopulator) (Driver, error) {
var injected struct {
di.In
Driver `optional:"true"`
}
err := populator.Populate(&injected)
if err != nil {
return nil, err
}
return injected.Driver, nil
}