Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2285 from maxsmythe/dynamic-informer-cache
馃尡 Proposal for dynamic informer cache
- Loading branch information
Showing
9 changed files
with
329 additions
and
6 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
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
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,38 @@ | ||
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) | ||
}) | ||
<-out | ||
} | ||
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 | ||
} |
Oops, something went wrong.