fixed potential race condition

This commit is contained in:
marcobaobao
2023-10-28 12:10:50 +02:00
parent c320058af3
commit 252d2f2845
+11 -3
View File
@@ -6,12 +6,20 @@ import "time"
//
// Debounce emits the most recently emitted value from the source
// withing the timespan set by the span time.Duration
func Sample[T any](span time.Duration, source chan T, done chan struct{}, fn func(e T)) {
ticker := time.NewTicker(span)
func Sample(span time.Duration, source chan []byte, done chan struct{}, fn func(e []byte)) {
var (
item []byte
ticker = time.NewTicker(span)
)
for {
select {
case <-ticker.C:
fn(<-source)
if item != nil {
fn(item)
}
case <-source:
item = <-source
case <-done:
ticker.Stop()
return