fix: close of closed channel
This commit is contained in:
parent
2d67e2e0c6
commit
8adf35d480
@ -16,6 +16,7 @@ package wait
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
@ -182,13 +183,15 @@ func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
|||||||
|
|
||||||
func MergeAndCloseOnAnyStopChannel[T any](upstreams ...<-chan T) <-chan T {
|
func MergeAndCloseOnAnyStopChannel[T any](upstreams ...<-chan T) <-chan T {
|
||||||
out := make(chan T)
|
out := make(chan T)
|
||||||
|
closeOnce := sync.Once{}
|
||||||
for _, upstream := range upstreams {
|
for _, upstream := range upstreams {
|
||||||
ch := upstream
|
ch := upstream
|
||||||
go lo.Try0(func() {
|
go lo.Try0(func() {
|
||||||
select {
|
select {
|
||||||
case <-ch:
|
case <-ch:
|
||||||
|
closeOnce.Do(func() {
|
||||||
close(out)
|
close(out)
|
||||||
|
})
|
||||||
case <-out:
|
case <-out:
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user