forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_queue.go
49 lines (44 loc) · 1.04 KB
/
send_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
package quic
type sendQueue struct {
queue chan *packetBuffer
closeCalled chan struct{} // runStopped when Close() is called
runStopped chan struct{} // runStopped when the run loop returns
conn connection
}
func newSendQueue(conn connection) *sendQueue {
s := &sendQueue{
conn: conn,
runStopped: make(chan struct{}),
closeCalled: make(chan struct{}),
queue: make(chan *packetBuffer, 1),
}
return s
}
func (h *sendQueue) Send(p *packetBuffer) {
h.queue <- p
}
func (h *sendQueue) Run() error {
defer close(h.runStopped)
var shouldClose bool
for {
if shouldClose && len(h.queue) == 0 {
return nil
}
select {
case <-h.closeCalled:
h.closeCalled = nil // prevent this case from being selected again
// make sure that all queued packets are actually sent out
shouldClose = true
case p := <-h.queue:
if err := h.conn.Write(p.Data); err != nil {
return err
}
p.Release()
}
}
}
func (h *sendQueue) Close() {
close(h.closeCalled)
// wait until the run loop returned
<-h.runStopped
}