mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
refactor: refact FanIn func in channel.go
This commit is contained in:
@@ -97,32 +97,29 @@ func (c *Channel) Take(ctx context.Context, valueStream <-chan any, number int)
|
|||||||
|
|
||||||
// FanIn merge multiple channels into one channel
|
// FanIn merge multiple channels into one channel
|
||||||
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any {
|
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any {
|
||||||
var wg sync.WaitGroup
|
out := make(chan any)
|
||||||
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() {
|
go func() {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(len(channels))
|
||||||
|
|
||||||
|
for _, c := range channels {
|
||||||
|
go func(c <-chan any) {
|
||||||
|
defer wg.Done()
|
||||||
|
for v := range c {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case out <- v:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(c)
|
||||||
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(multiplexedStream)
|
close(out)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return multiplexedStream
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tee split one chanel into two channels
|
// Tee split one chanel into two channels
|
||||||
|
|||||||
Reference in New Issue
Block a user