-
Notifications
You must be signed in to change notification settings - Fork 0
/
ack_queue.go
53 lines (47 loc) · 876 Bytes
/
ack_queue.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
package spectral
import (
"slices"
"sync"
"time"
)
type ackQueue struct {
sequenceID uint32
sort bool
lastAck time.Time
list []uint32
mu sync.Mutex
}
func newAckQueue() *ackQueue {
return &ackQueue{}
}
func (a *ackQueue) add(sequenceID uint32) {
a.mu.Lock()
a.sequenceID++
if !a.sort && sequenceID != a.sequenceID {
a.sort = true
}
a.lastAck = time.Now()
a.list = append(a.list, sequenceID)
a.mu.Unlock()
}
func (a *ackQueue) addDuplicate(sequenceID uint32) {
a.mu.Lock()
a.sort = true
a.list = append(a.list, sequenceID)
a.mu.Unlock()
}
func (a *ackQueue) flush() (delay int64, list []uint32) {
a.mu.Lock()
if len(a.list) > 0 {
delay = time.Since(a.lastAck).Nanoseconds()
list = a.list
if a.sort {
slices.Sort(list)
}
a.sort = false
a.lastAck = time.Time{}
a.list = a.list[:0]
}
a.mu.Unlock()
return
}