2019-04-23 09:05:07 +03:00
|
|
|
package messaging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-04-23 09:40:50 +03:00
|
|
|
"go.uber.org/zap"
|
2019-04-23 09:05:07 +03:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MaxWorkers = 32
|
|
|
|
MaxQueueSize = 128
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-04-23 09:40:50 +03:00
|
|
|
log, _ = zap.NewDevelopment()
|
2019-04-23 09:05:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Pipeline struct {
|
|
|
|
workers []*worker
|
|
|
|
chain chan interface{}
|
2019-05-01 14:14:30 +03:00
|
|
|
running *uint32
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pipeline) Start() {
|
|
|
|
distributeToChannels := func(ch chan interface{}, cs []*worker) {
|
|
|
|
writer := cs[0] //first worker must stream as default
|
|
|
|
for {
|
|
|
|
for _, c := range cs {
|
|
|
|
expectationWorkers := uint32(len(ch)/(MaxQueueSize/MaxWorkers)) + 1
|
2019-05-01 14:14:30 +03:00
|
|
|
runningWorker := atomic.LoadUint32(p.running)
|
2019-04-23 11:24:12 +03:00
|
|
|
if c.index > runningWorker+1 || c.index > expectationWorkers {
|
|
|
|
break
|
|
|
|
}
|
2019-04-23 09:05:07 +03:00
|
|
|
select {
|
|
|
|
case val := <-ch:
|
2019-04-23 11:24:12 +03:00
|
|
|
writer = c
|
2019-04-23 09:05:07 +03:00
|
|
|
if c.debug {
|
2019-04-23 09:40:50 +03:00
|
|
|
log.Info("Worker receiving", zap.Any("index", writer.index), zap.Any("running", runningWorker), zap.Any("no# workers", expectationWorkers))
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
go writer.stream(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 14:14:30 +03:00
|
|
|
}
|
2019-04-23 09:05:07 +03:00
|
|
|
|
2019-05-01 14:14:30 +03:00
|
|
|
go distributeToChannels(p.chain, p.workers)
|
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-04-23 09:05:07 +03:00
|
|
|
wk := make([]*worker, 0, MaxWorkers)
|
2019-04-23 09:40:50 +03:00
|
|
|
ch := make(chan interface{}, 4096)
|
2019-05-01 14:14:30 +03:00
|
|
|
pipe := &Pipeline{chain: ch, running: new(uint32)}
|
2019-04-23 09:05:07 +03:00
|
|
|
for i := 0; i < MaxWorkers; i++ {
|
|
|
|
wk = append(wk,
|
|
|
|
&worker{
|
|
|
|
index: uint32(i + 1),
|
|
|
|
chain: make(chan interface{}, MaxQueueSize),
|
|
|
|
mutex: new(sync.Mutex),
|
|
|
|
debug: debug,
|
|
|
|
idle: idle,
|
|
|
|
Dispatcher: d(),
|
2019-05-01 14:14:30 +03:00
|
|
|
broker: pipe.running,
|
2019-04-23 09:05:07 +03:00
|
|
|
})
|
|
|
|
}
|
2019-05-01 14:14:30 +03:00
|
|
|
pipe.workers = wk
|
|
|
|
return pipe
|
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
|
2019-05-01 14:14:30 +03:00
|
|
|
broker *uint32
|
2019-04-23 09:05:07 +03:00
|
|
|
Dispatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *worker) stream(val interface{}) {
|
|
|
|
c.chain <- val
|
|
|
|
if !c.running {
|
|
|
|
c.mutex.Lock()
|
|
|
|
c.running = true
|
2019-05-01 14:14:30 +03:00
|
|
|
atomic.AddUint32(c.broker, 1)
|
|
|
|
defer atomic.AddUint32(c.broker, ^uint32(1 - 1))
|
2019-04-23 09:05:07 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
err := c.Before(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-23 09:40:50 +03:00
|
|
|
log.Error("can not start worker", zap.Error(err))
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
atomic.AddUint32(&idle, 1)
|
|
|
|
if i := atomic.LoadUint32(&idle); i > 0 {
|
|
|
|
if i > c.idle {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.debug {
|
2019-04-23 09:40:50 +03:00
|
|
|
log.Info("Idle", zap.Any("worker index", c.index), zap.Any("idle", idle))
|
2019-04-23 09:05:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|