-
Notifications
You must be signed in to change notification settings - Fork 75
/
buffer.go
65 lines (57 loc) · 1.68 KB
/
buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package counts
import (
"time"
"github.com/rancher/apiserver/pkg/types"
)
// 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)
debounceCounts(result, c)
}()
return result
}
// 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 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 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 currentCount != nil {
result <- toAPIEvent(*currentCount)
currentCount = nil
}
}
}
}
func toAPIEvent(count Count) types.APIEvent {
return types.APIEvent{
Name: "resource.change",
ResourceType: "counts",
Object: toAPIObject(count),
}
}