-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawconn_outgoing.go
215 lines (195 loc) · 4.91 KB
/
rawconn_outgoing.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package copper
import (
"sync"
)
// rawConnOutgoing handles all outgoing data.
type rawConnOutgoing struct {
conn *rawConn
// This mutex protects all fields of this structure, but a very important
// invariant is that this mutex must be a leaf amongst other connection
// related mutexes. It must not be locked when calling any outside code.
mu sync.Mutex
err error // becomes non-nil when the connection fails
writeready bool // true if there exists something to write
writecond sync.Cond // signals when writeready becomes true
blocked int // blocks writing when >0, used during testing
unblocked sync.Cond // signals when blocked becomes 0
pingAcks []PingData // a list of outgoing ping replies
pingQueue []PingData // a list of outgoing ping requests
settingsAcks int // number of SETTINGS/ACK frames that need to be sent
ctrl rawStreamQueue // a queue of streams that need to write control frames
data rawStreamQueue // a queue of streams that need to write data
}
func (o *rawConnOutgoing) init(conn *rawConn) {
o.conn = conn
o.writecond.L = &o.mu
o.unblocked.L = &o.mu
}
// Schedules a send of err to the remote side. If the error is a normal close
// of the connection it is changed to ECONNSHUTDOWN. The connection will be
// closed after the error is sent, freeing resources.
func (o *rawConnOutgoing) fail(err error) {
o.mu.Lock()
if o.err == nil {
if err == ECONNCLOSED {
o.err = ECONNSHUTDOWN
} else {
o.err = err
}
o.wakeupLocked()
}
o.mu.Unlock()
}
// Schedules a ping reply on the connection.
func (o *rawConnOutgoing) addPingAck(value PingData) {
o.mu.Lock()
o.pingAcks = append(o.pingAcks, value)
o.wakeupLocked()
o.mu.Unlock()
}
// Schedules a ping request on the connection.
func (o *rawConnOutgoing) addPingQueue(value PingData) {
o.mu.Lock()
if o.err == nil {
o.pingQueue = append(o.pingQueue, value)
o.wakeupLocked()
}
o.mu.Unlock()
}
// Clears the ping request queue, used by other components to cancel pings.
func (o *rawConnOutgoing) clearPingQueue() {
o.mu.Lock()
o.pingQueue = nil
o.mu.Unlock()
}
// Schedules a SETTINGS frame with the ACK flag on the connection.
func (o *rawConnOutgoing) addSettingsAck() {
o.mu.Lock()
o.settingsAcks++
o.wakeupLocked()
o.mu.Unlock()
}
// Adds stream to the control frames queue
func (o *rawConnOutgoing) addCtrl(stream *rawStream) {
o.mu.Lock()
if !stream.inctrl {
o.ctrl.push(stream)
stream.inctrl = true
o.wakeupLocked()
}
o.mu.Unlock()
}
// Adds stream to the data queue
func (o *rawConnOutgoing) addData(stream *rawStream) {
o.mu.Lock()
if !stream.indata {
o.data.push(stream)
stream.indata = true
o.wakeupLocked()
}
o.mu.Unlock()
}
// Wakes up writeloop
func (o *rawConnOutgoing) wakeup() {
o.mu.Lock()
o.wakeupLocked()
o.mu.Unlock()
}
// Wakes up writeloop, mu must be locked
func (o *rawConnOutgoing) wakeupLocked() {
o.writeready = true
o.writecond.Broadcast()
}
// Writes outgoing frames until there is nothing more to send. Returns false on
// errors, or after sending an error to the remote, indicating that the
// connection should be closed.
func (o *rawConnOutgoing) writeFrames() (result bool) {
o.mu.Lock()
for !o.writeready {
o.writecond.Wait()
}
writeloop:
for {
for o.blocked > 0 {
// Allow tests to setup things properly
o.unblocked.Wait()
}
if o.err != nil {
err := o.err
o.mu.Unlock()
// Attempt to notify the other side that we have an error
// It's ok if any of this fails, the read side will stop
// when we close the connection.
o.conn.writer.WriteReset(0, 0, err)
return false
}
pingAcks := o.pingAcks
if len(pingAcks) > 0 {
o.pingAcks = nil
o.mu.Unlock()
for _, data := range pingAcks {
err := o.conn.writer.WritePing(FlagPingAck, data)
if err != nil {
o.conn.closeWithError(err)
return false
}
}
o.mu.Lock()
continue writeloop
}
pingQueue := o.pingQueue
if len(pingQueue) > 0 {
o.pingQueue = nil
o.mu.Unlock()
for _, data := range pingQueue {
err := o.conn.writer.WritePing(0, data)
if err != nil {
o.conn.closeWithError(err)
return false
}
}
o.mu.Lock()
continue writeloop
}
if o.settingsAcks > 0 {
o.settingsAcks--
o.mu.Unlock()
err := o.conn.writer.WriteSettingsAck()
if err != nil {
o.conn.closeWithError(err)
return false
}
o.mu.Lock()
continue writeloop
}
if o.ctrl.size > 0 {
stream := o.ctrl.take()
stream.inctrl = false
o.mu.Unlock()
err := stream.writeCtrl()
if err != nil {
o.conn.closeWithError(err)
return false
}
o.mu.Lock()
continue writeloop
}
if o.data.size > 0 {
stream := o.data.take()
stream.indata = false
o.mu.Unlock()
err := stream.writeData()
if err != nil {
o.conn.closeWithError(err)
return false
}
o.mu.Lock()
continue writeloop
}
break
}
// If we reach here there's nothing to send
o.writeready = false
o.mu.Unlock()
return true
}