2019-04-23 09:05:07 +03:00
|
|
|
package messaging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2020-05-07 07:18:03 +03:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2019-04-23 09:05:07 +03:00
|
|
|
)
|
|
|
|
|
2019-05-02 13:27:28 +03:00
|
|
|
var (
|
|
|
|
log, _ = zap.NewDevelopment()
|
2019-04-23 09:05:07 +03:00
|
|
|
)
|
|
|
|
|
2019-05-02 13:27:28 +03:00
|
|
|
const (
|
|
|
|
MaxWorkers = 16
|
|
|
|
MaxQueueSize = 512
|
|
|
|
MasterQueueSize = MaxQueueSize * MaxWorkers
|
2019-04-23 09:05:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Pipeline struct {
|
2019-05-02 13:27:28 +03:00
|
|
|
workers map[int]*worker
|
2019-04-23 09:05:07 +03:00
|
|
|
chain chan interface{}
|
|
|
|
}
|
|
|
|
|
2019-05-06 17:43:42 +03:00
|
|
|
func (p *Pipeline) Start(ctx context.Context) {
|
2019-05-02 13:27:28 +03:00
|
|
|
go func(pipe *Pipeline) {
|
2019-04-23 09:05:07 +03:00
|
|
|
for {
|
2019-05-06 17:43:42 +03:00
|
|
|
expectationWorkers := len(pipe.chain) % MaxWorkers
|
|
|
|
if expectationWorkers >= MaxWorkers {
|
|
|
|
expectationWorkers = 0
|
2019-05-02 13:27:28 +03:00
|
|
|
}
|
2019-05-06 17:43:42 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case val, ok := <-pipe.chain:
|
|
|
|
if !ok {
|
|
|
|
return
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
2019-05-06 17:43:42 +03:00
|
|
|
go pipe.workers[expectationWorkers].stream(val)
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-02 13:27:28 +03:00
|
|
|
}(p)
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pipeline) Dispatch(msg interface{}) {
|
|
|
|
p.chain <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
type DispatcherBuilder func() Dispatcher
|
|
|
|
|
2019-04-23 09:40:50 +03:00
|
|
|
func NewPipeline(d DispatcherBuilder, idle uint32, debug bool) *Pipeline {
|
2019-05-02 13:27:28 +03:00
|
|
|
ch := make(chan interface{}, MasterQueueSize)
|
|
|
|
wk := make(map[int]*worker)
|
2019-04-23 09:05:07 +03:00
|
|
|
for i := 0; i < MaxWorkers; i++ {
|
2019-05-02 13:27:28 +03:00
|
|
|
wk[i] = &worker{
|
|
|
|
index: uint32(i + 1),
|
|
|
|
chain: make(chan interface{}, MaxQueueSize),
|
|
|
|
mutex: new(sync.Mutex),
|
|
|
|
debug: debug,
|
|
|
|
idle: idle,
|
|
|
|
Dispatcher: d(),
|
|
|
|
}
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
2019-05-02 13:27:28 +03:00
|
|
|
return &Pipeline{workers: wk, chain: ch}
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Dispatcher interface {
|
|
|
|
Before(context.Context) error
|
|
|
|
After() error
|
|
|
|
Process(interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type worker struct {
|
|
|
|
index uint32
|
|
|
|
mutex *sync.Mutex
|
|
|
|
running bool
|
|
|
|
chain chan interface{}
|
|
|
|
debug bool
|
|
|
|
idle uint32
|
|
|
|
Dispatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *worker) stream(val interface{}) {
|
|
|
|
c.chain <- val
|
|
|
|
if !c.running {
|
|
|
|
c.mutex.Lock()
|
|
|
|
c.running = true
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer func(w *worker, cancel context.CancelFunc) {
|
|
|
|
if w.debug {
|
2019-04-23 09:40:50 +03:00
|
|
|
log.Info("Worker leaving", zap.Any("index", w.index), zap.Any("idle", w.idle))
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
err := c.After()
|
|
|
|
if err != nil {
|
2019-04-23 09:40:50 +03:00
|
|
|
log.Error("can not finish track issue", zap.Error(err))
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
w.mutex.Unlock()
|
|
|
|
w.running = false
|
|
|
|
}(c, cancel)
|
2019-05-02 13:27:28 +03:00
|
|
|
err := c.Before(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("can not start worker", zap.Error(err))
|
|
|
|
}
|
2019-04-23 09:05:07 +03:00
|
|
|
var idle uint32 = 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-c.chain:
|
|
|
|
atomic.StoreUint32(&idle, 0)
|
|
|
|
if msg != nil {
|
|
|
|
err := c.Process(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("can not process message",
|
2019-04-23 09:40:50 +03:00
|
|
|
zap.Any("msg", &msg),
|
|
|
|
zap.Error(err),
|
2019-04-23 09:05:07 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
atomic.AddUint32(&idle, 1)
|
|
|
|
if i := atomic.LoadUint32(&idle); i > 0 {
|
|
|
|
if i > c.idle {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|