1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-16 15:29:04 +00:00

Only return changed counts, and send initial count

Three main changes:
- When a count changes, only send that count. This makes the
count socket significantly more usable
- The count buffer will now send a count right away. This allows
a message to launch wihtout an inital buffered wait period
- The count buffer now sends messages every 5 seconds instead of 1
This commit is contained in:
Michael Bolot
2022-10-03 16:12:11 -05:00
parent 647cba2be7
commit 2eb41a9db7
2 changed files with 49 additions and 26 deletions

View File

@@ -6,35 +6,60 @@ import (
"github.com/rancher/apiserver/pkg/types"
)
func buffer(c chan types.APIEvent) chan types.APIEvent {
// debounceDuration determines how long events will be held before they are sent to the consumer
var debounceDuration = 5 * time.Second
// countsBuffer creates an APIEvent channel with a buffered response time (i.e. replies are only sent once every second)
func countsBuffer(c chan Count) chan types.APIEvent {
result := make(chan types.APIEvent)
go func() {
defer close(result)
debounce(result, c)
debounceCounts(result, c)
}()
return result
}
func debounce(result, input chan types.APIEvent) {
t := time.NewTicker(time.Second)
// debounceCounts converts counts from an input channel into an APIEvent, and updates the result channel at a reduced pace
func debounceCounts(result chan types.APIEvent, input chan Count) {
// counts aren't a critical value. To avoid excess UI processing, only send updates after debounceDuration has elapsed
t := time.NewTicker(debounceDuration)
defer t.Stop()
var (
lastEvent *types.APIEvent
)
var currentCount *Count
firstCount, fOk := <-input
if fOk {
// send a count immediately or we will have to wait a second for the first update
result <- toAPIEvent(firstCount)
}
for {
select {
case event, ok := <-input:
if ok {
lastEvent = &event
} else {
case count, ok := <-input:
if !ok {
return
}
if currentCount == nil {
currentCount = &count
} else {
itemCounts := count.Counts
for id, itemCount := range itemCounts {
// our current count will be outdated in comparison with anything in the new events
currentCount.Counts[id] = itemCount
}
}
case <-t.C:
if lastEvent != nil {
result <- *lastEvent
lastEvent = nil
if currentCount != nil {
result <- toAPIEvent(*currentCount)
currentCount = nil
}
}
}
}
func toAPIEvent(count Count) types.APIEvent {
return types.APIEvent{
Name: "resource.change",
ResourceType: "counts",
Object: toAPIObject(count),
}
}