mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-21 20:46:08 +03:00
[fanout] Optimize fanout
This commit is contained in:
parent
9c0c9709ff
commit
44115024e2
@ -22,21 +22,21 @@ type Pipeline struct {
|
||||
chain chan interface{}
|
||||
}
|
||||
|
||||
func (p *Pipeline) Start() {
|
||||
func (p *Pipeline) Start(ctx context.Context) {
|
||||
go func(pipe *Pipeline) {
|
||||
for {
|
||||
expectationWorkers := len(pipe.chain)
|
||||
if expectationWorkers > MaxWorkers {
|
||||
expectationWorkers = expectationWorkers % MaxWorkers
|
||||
expectationWorkers := len(pipe.chain) % MaxWorkers
|
||||
if expectationWorkers >= MaxWorkers {
|
||||
expectationWorkers = 0
|
||||
}
|
||||
for _, c := range pipe.workers {
|
||||
if expectationWorkers < int(c.index) {
|
||||
break
|
||||
}
|
||||
select {
|
||||
case val := <-pipe.chain:
|
||||
go c.stream(val)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case val, ok := <-pipe.chain:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
go pipe.workers[expectationWorkers].stream(val)
|
||||
}
|
||||
}
|
||||
}(p)
|
||||
|
Loading…
Reference in New Issue
Block a user