1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-23 13:52:26 +08:00

feat: add FanIn for channel

This commit is contained in:
dudaodong
2022-04-19 15:34:26 +08:00
parent 763aa8e10d
commit 980ff2c363
2 changed files with 57 additions and 6 deletions

View File

@@ -4,7 +4,10 @@
// Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel, async.
package concurrency
import "context"
import (
"context"
"sync"
)
// Channel is a logic object which can generate or manipulate go channel
// all methods of Channel are in the book tilted《Concurrency in Go》
@@ -91,3 +94,33 @@ func (c *Channel) Take(ctx context.Context, valueStream <-chan any, number int)
return takeStream
}
// FanIn merge multiple channels into one channel
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any {
var wg sync.WaitGroup
multiplexedStream := make(chan any)
multiplex := func(c <-chan any) {
defer wg.Done()
for i := range c {
select {
case <-ctx.Done():
return
case multiplexedStream <- i:
}
}
}
wg.Add(len(channels))
for _, c := range channels {
go multiplex(c)
}
go func() {
wg.Wait()
close(multiplexedStream)
}()
return multiplexedStream
}