-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Max Smythe <smythe@google.com>
- Loading branch information
Showing
4 changed files
with
179 additions
and
79 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package syncs | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
// MergeChans returns a channel that is closed when any of the input channels are signaled. | ||
// The caller must call the returned CancelFunc to ensure no resources are leaked. | ||
func MergeChans[T any](chans ...<-chan T) (<-chan T, context.CancelFunc) { | ||
var once sync.Once | ||
out := make(chan T) | ||
cancel := make(chan T) | ||
cancelFunc := func() { | ||
once.Do(func() { | ||
close(cancel) | ||
}) | ||
} | ||
cases := make([]reflect.SelectCase, len(chans)+1) | ||
for i := range chans { | ||
cases[i] = reflect.SelectCase{ | ||
Dir: reflect.SelectRecv, | ||
Chan: reflect.ValueOf(chans[i]), | ||
} | ||
} | ||
cases[len(cases)-1] = reflect.SelectCase{ | ||
Dir: reflect.SelectRecv, | ||
Chan: reflect.ValueOf(cancel), | ||
} | ||
go func() { | ||
defer close(out) | ||
_, _, _ = reflect.Select(cases) | ||
}() | ||
|
||
return out, cancelFunc | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package syncs | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestMergeChans(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
count int | ||
signal int | ||
}{ | ||
{ | ||
name: "single channel", | ||
count: 1, | ||
signal: 0, | ||
}, | ||
{ | ||
name: "double channel", | ||
count: 2, | ||
signal: 0, | ||
}, | ||
{ | ||
name: "five channel, close 0", | ||
count: 5, | ||
signal: 0, | ||
}, | ||
{ | ||
name: "five channel, close 1", | ||
count: 5, | ||
signal: 1, | ||
}, | ||
{ | ||
name: "five channel, close 2", | ||
count: 5, | ||
signal: 2, | ||
}, | ||
{ | ||
name: "five channel, close 3", | ||
count: 5, | ||
signal: 3, | ||
}, | ||
{ | ||
name: "five channel, close 4", | ||
count: 5, | ||
signal: 4, | ||
}, | ||
{ | ||
name: "single channel, cancel", | ||
count: 1, | ||
signal: -1, | ||
}, | ||
{ | ||
name: "double channel, cancel", | ||
count: 2, | ||
signal: -1, | ||
}, | ||
{ | ||
name: "five channel, cancel", | ||
count: 5, | ||
signal: -1, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if callAndClose(test.count, test.signal, 1) { | ||
t.Error("timeout before merged channel closed") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func callAndClose(numChans, signalChan, timeoutSeconds int) bool { | ||
chans := make([]chan struct{}, numChans) | ||
readOnlyChans := make([]<-chan struct{}, numChans) | ||
for i := range chans { | ||
chans[i] = make(chan struct{}) | ||
readOnlyChans[i] = chans[i] | ||
} | ||
defer func() { | ||
for i := range chans { | ||
close(chans[i]) | ||
} | ||
}() | ||
|
||
merged, cancel := MergeChans(readOnlyChans...) | ||
defer cancel() | ||
|
||
timer := time.NewTimer(time.Duration(timeoutSeconds) * time.Second) | ||
|
||
if signalChan >= 0 { | ||
chans[signalChan] <- struct{}{} | ||
} else { | ||
cancel() | ||
} | ||
select { | ||
case <-merged: | ||
return false | ||
case <-timer.C: | ||
return true | ||
} | ||
} |