1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-13 22:09:31 +00:00

Only send counts every second if there is a change

This commit is contained in:
Darren Shepherd
2020-07-20 09:21:03 -07:00
parent ece0f7bce5
commit e27f384795
2 changed files with 49 additions and 9 deletions

View File

@@ -0,0 +1,40 @@
package counts
import (
"time"
"github.com/rancher/apiserver/pkg/types"
)
func buffer(c chan types.APIEvent) chan types.APIEvent {
result := make(chan types.APIEvent)
go func() {
defer close(result)
debounce(result, c)
}()
return result
}
func debounce(result, input chan types.APIEvent) {
t := time.NewTicker(time.Second)
defer t.Stop()
var (
lastEvent *types.APIEvent
)
for {
select {
case event, ok := <-input:
if ok {
lastEvent = &event
} else {
return
}
case <-t.C:
if lastEvent != nil {
result <- *lastEvent
lastEvent = nil
}
}
}
}