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 ephemeral
import (
"sync"
"sync/atomic"
"time"
"github.com/spiral/errors"
"github.com/spiral/roadrunner/v2/pkg/events"
priorityqueue "github.com/spiral/roadrunner/v2/pkg/priority_queue"
"github.com/spiral/roadrunner/v2/plugins/config"
"github.com/spiral/roadrunner/v2/plugins/jobs/job"
"github.com/spiral/roadrunner/v2/plugins/jobs/pipeline"
"github.com/spiral/roadrunner/v2/plugins/logger"
)
const (
prefetch string = "prefetch"
)
type Config struct {
Prefetch uint64 `mapstructure:"prefetch"`
}
type JobBroker struct {
cfg *Config
log logger.Logger
eh events.Handler
pipeline sync.Map
pq priorityqueue.Queue
localPrefetch chan *Item
goroutinesMaxNum uint64
stopCh chan struct{}
}
func NewJobBroker(configKey string, log logger.Logger, cfg config.Configurer, eh events.Handler, pq priorityqueue.Queue) (*JobBroker, error) {
const op = errors.Op("new_ephemeral_pipeline")
jb := &JobBroker{
log: log,
pq: pq,
eh: eh,
goroutinesMaxNum: 1000,
stopCh: make(chan struct{}, 1),
}
err := cfg.UnmarshalKey(configKey, &jb.cfg)
if err != nil {
return nil, errors.E(op, err)
}
if jb.cfg.Prefetch == 0 {
jb.cfg.Prefetch = 100_000
}
// initialize a local queue
jb.localPrefetch = make(chan *Item, jb.cfg.Prefetch)
// consume from the queue
go jb.consume()
return jb, nil
}
func FromPipeline(pipeline *pipeline.Pipeline, log logger.Logger, eh events.Handler, pq priorityqueue.Queue) (*JobBroker, error) {
jb := &JobBroker{
log: log,
pq: pq,
eh: eh,
goroutinesMaxNum: 1000,
stopCh: make(chan struct{}, 1),
}
// initialize a local queue
jb.localPrefetch = make(chan *Item, pipeline.Int(prefetch, 100_000))
// consume from the queue
go jb.consume()
return jb, nil
}
func (j *JobBroker) Push(jb *job.Job) error {
const op = errors.Op("ephemeral_push")
// check if the pipeline registered
if b, ok := j.pipeline.Load(jb.Options.Pipeline); ok {
if !b.(bool) {
return errors.E(op, errors.Errorf("pipeline disabled: %s", jb.Options.Pipeline))
}
msg := fromJob(jb)
// handle timeouts
// theoretically, some bad user may send a millions requests with a delay and produce a billion (for example)
// goroutines here. We should limit goroutines here.
if msg.Options.Delay > 0 {
// if we have 1000 goroutines waiting on the delay - reject 1001
if atomic.LoadUint64(&j.goroutinesMaxNum) >= 1000 {
return errors.E(op, errors.Str("max concurrency number reached"))
}
go func(jj *job.Job) {
atomic.AddUint64(&j.goroutinesMaxNum, 1)
time.Sleep(jj.Options.DelayDuration())
// send the item after timeout expired
j.localPrefetch <- msg
atomic.AddUint64(&j.goroutinesMaxNum, ^uint64(0))
}(jb)
return nil
}
// insert to the local, limited pipeline
select {
case j.localPrefetch <- msg:
default:
return errors.E(op, errors.Errorf("local pipeline is full, consider to increase prefetch number, current limit: %d", j.cfg.Prefetch))
}
return nil
}
return errors.E(op, errors.Errorf("no such pipeline: %s", jb.Options.Pipeline))
}
func (j *JobBroker) consume() {
// redirect
for {
select {
case item := <-j.localPrefetch:
j.pq.Insert(item)
case <-j.stopCh:
return
}
}
}
func (j *JobBroker) Register(pipeline *pipeline.Pipeline) error {
const op = errors.Op("ephemeral_register")
if _, ok := j.pipeline.Load(pipeline.Name()); ok {
return errors.E(op, errors.Errorf("queue %s has already been registered", pipeline))
}
j.pipeline.Store(pipeline.Name(), true)
return nil
}
func (j *JobBroker) Pause(pipeline string) {
if q, ok := j.pipeline.Load(pipeline); ok {
if q == true {
// mark pipeline as turned off
j.pipeline.Store(pipeline, false)
}
}
j.eh.Push(events.JobEvent{
Event: events.EventPipeStopped,
Pipeline: pipeline,
Start: time.Now(),
Elapsed: 0,
})
}
func (j *JobBroker) Resume(pipeline string) {
if q, ok := j.pipeline.Load(pipeline); ok {
if q == false {
// mark pipeline as turned on
j.pipeline.Store(pipeline, true)
}
}
j.eh.Push(events.JobEvent{
Event: events.EventPipeActive,
Pipeline: pipeline,
Start: time.Now(),
Elapsed: 0,
})
}
// Run is no-op for the ephemeral
func (j *JobBroker) Run(pipe *pipeline.Pipeline) error {
j.eh.Push(events.JobEvent{
Event: events.EventPipeRun,
Driver: pipe.Driver(),
Pipeline: pipe.Name(),
Start: time.Now(),
})
return nil
}
func (j *JobBroker) Stop() error {
var pipe string
j.pipeline.Range(func(key, _ interface{}) bool {
pipe = key.(string)
j.pipeline.Delete(key)
return true
})
// return from the consumer
j.stopCh <- struct{}{}
j.eh.Push(events.JobEvent{
Event: events.EventPipeStopped,
Pipeline: pipe,
Start: time.Now(),
Elapsed: 0,
})
return nil
}
|