7 type enqueuedPacket struct {
12 type sendQueueState struct {
13 enqueue chan enqueuedPacket
16 var globalQueue sendQueueState
19 globalQueue = sendQueueState{
20 enqueue: make(chan enqueuedPacket, 10),
22 go globalQueue.sendQueueDispatch()
26 throttleMinDuration = 2 * time.Second
27 throttleMaxDuration = 4 * time.Second
28 throttleIncFactor = 1.1
29 throttleDecFactor = 0.9
30 throttleDecInterval = 10 * time.Second
33 func sendPacket(p packet, c chan packet) chan bool {
34 p.sent = make(chan bool, 2)
35 globalQueue.enqueue <- enqueuedPacket{packet: p, queue: c}
39 func (gq *sendQueueState) sendQueueDispatch() {
40 pkt := (*enqueuedPacket)(nil)
41 queue := make([]enqueuedPacket, 0)
43 nextTimer := time.NewTimer(0)
44 decTimer := time.NewTimer(0)
46 currentThrottle := throttleMinDuration
49 if pkt == nil && len(queue) > 0 {
62 case p := <-gq.enqueue:
63 queue = append(queue, p)
65 pkt.queue <- pkt.packet
70 currentThrottle = time.Duration(float64(currentThrottle) * throttleIncFactor)
71 if currentThrottle > throttleMaxDuration {
72 currentThrottle = throttleMaxDuration
74 nextTimer.Reset(currentThrottle)
76 decTimer.Reset(throttleDecInterval)
78 currentThrottle = time.Duration(float64(currentThrottle) * throttleDecFactor)
79 if currentThrottle < throttleMinDuration {
80 currentThrottle = throttleMinDuration
82 decTimer.Reset(throttleDecInterval)