-
-
Save leonardo5621/9dffff4c9cee30efe9524cc15eb29eda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fanIn(ctx context.Context, fetchers ...<-chan interface{}) <-chan interface{} { | |
combinedFetcher := make(chan interface{}) | |
// 1 | |
var wg sync.WaitGroup | |
wg.Add(len(fetchers)) | |
// 2 | |
for _, f := range fetchers { | |
f := f | |
go func() { | |
// 3 | |
defer wg.Done() | |
for{ | |
select{ | |
case res := <-f: | |
combinedFetcher <- res | |
case <-ctx.Done(): | |
return | |
} | |
} | |
}() | |
} | |
// 4 | |
// Channel cleanup | |
go func() { | |
wg.Wait() | |
close(combinedFetcher) | |
} () | |
return combinedFetcher | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment