-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
307 lines (285 loc) · 7.3 KB
/
session.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright (c) 2014 SameGoal LLC. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wc
import (
"errors"
"fmt"
"net/http"
"strconv"
"time"
)
func flushPending(sw *sessionWrapper) error {
msgs, err := sw.BackChannelPeek()
if err != nil {
return err
}
for len(msgs) > 0 {
if msgs[0].ID > sw.si.BackChannelAID {
break
}
msgs = msgs[1:]
}
if len(msgs) == 0 {
return nil
}
for _, msg := range msgs {
debug("wc: %s writing back channel message %d %s", sw.SID(), msg.ID,
msg.Body)
}
sw.si.BackChannelAID = msgs[len(msgs)-1].ID
err = sw.p.chunkMessages(msgs)
if err != nil {
return err
}
if sw.bc.r.FormValue("CI") == "1" {
// TODO(hochhaus): Do not write messages in chunked mode when the back
// channel is a buffered proxy. Just reply to the entire request at once.
debug("wc: %s closing buffered-proxy back channel to deliver messages",
sw.SID())
sw.p.end()
sw.BackChannelClose()
close(sw.bc.done)
sw.bc = nil
sw.p = nil
sw.backChannelCloseNotifier = nil
sw.noopTimer.Stop()
sw.longBackChannelTimer.Stop()
}
return nil
}
func noop(sw *sessionWrapper) {
if sw.bc == nil {
debug("wc: %s noop skipped", sw.SID())
return
}
// if a non-buffered, active backchannel w/o pending data add noop
debug("wc: %s noop", sw.SID())
sw.noopTimer.Reset(30 * time.Second)
if err := sw.BackChannelAdd([]byte("[\"noop\"]")); err != nil {
sm.Error(sw.bc.r, err)
return
}
sw.backChannelBytes += 8
if err := flushPending(sw); err != nil {
sm.Error(sw.bc.r, err)
}
}
func longBackChannel(sw *sessionWrapper) {
if sw.bc != nil {
debug("wc: %s closing long-lived back channel", sw.SID())
sw.p.end()
sw.BackChannelClose()
close(sw.bc.done)
}
sw.bc = nil
sw.p = nil
sw.backChannelCloseNotifier = nil
sw.noopTimer.Stop()
sw.longBackChannelTimer.Stop()
}
func backChannelClose(sw *sessionWrapper) {
if sw.bc != nil {
debug("wc: %s back channel closed", sw.SID())
sw.BackChannelClose()
close(sw.bc.done)
}
sw.bc = nil
sw.p = nil
sw.backChannelCloseNotifier = nil
sw.noopTimer.Stop()
sw.longBackChannelTimer.Stop()
}
func backChannel(sw *sessionWrapper, reqRequest *reqRegister) {
debug("wc: %s new back channel", sw.SID())
if !maybeACKBackChannel(sw, reqRequest.w, reqRequest.r, false) {
close(sw.bc.done)
return
}
if sw.bc != nil {
sw.BackChannelClose()
sm.Error(reqRequest.r, errors.New("Duplicate backchannel."))
close(sw.bc.done)
}
sw.bc = reqRequest
sw.p = newPadder(reqRequest.w, reqRequest.r)
cn, ok := reqRequest.w.(http.CloseNotifier)
if !ok {
panic("webserver doesn't support close notification")
}
sw.backChannelCloseNotifier = cn.CloseNotify()
sw.noopTimer.Reset(30 * time.Second)
sw.longBackChannelTimer.Reset(4 * 60 * time.Second)
sw.BackChannelOpen()
if err := flushPending(sw); err != nil {
sm.Error(sw.bc.r, err)
}
}
func clientTerminate(sw *sessionWrapper, reqRequest *reqRegister) {
debug("wc: %s client terminate session", sw.SID())
defer func() {
reqRequest.done <- struct{}{}
}()
err := sm.TerminatedSession(sw.Session, ClientTerminateRequest)
if err != nil {
sm.Error(reqRequest.r, err)
http.Error(reqRequest.w, "Unable to terminate",
http.StatusInternalServerError)
return
}
if sw.bc != nil {
sw.BackChannelClose()
close(sw.bc.done)
sw.bc = nil
sw.p = nil
sw.backChannelCloseNotifier = nil
sw.noopTimer.Stop()
sw.longBackChannelTimer.Stop()
}
mutex.Lock()
delete(sessionWrapperMap, sw.SID())
mutex.Unlock()
reqRequest.w.Write([]byte("Terminated"))
}
func launchSession(sw *sessionWrapper) {
activityNotifier := make(chan int)
go sessionWorker(sw, activityNotifier)
go activityProxyWorker(sw, activityNotifier)
}
func maybeACKBackChannel(
sw *sessionWrapper,
w http.ResponseWriter,
r *http.Request,
forwardChannel bool,
) bool {
aid, err := strconv.Atoi(r.FormValue("AID"))
if err != nil || aid < 0 {
sm.Error(r, err)
http.Error(w, "Unable to parse AID", 400)
return false
}
bcMsgs, err := sw.BackChannelPeek()
if err != nil {
sm.Error(r, err)
http.Error(w, "Unable to get messages", http.StatusInternalServerError)
return false
}
remainingBytes := 0
ackedBytes := 0
messagesToACK := false
for _, bcMsg := range bcMsgs {
if bcMsg.ID > aid {
remainingBytes += len(bcMsg.Body)
} else {
messagesToACK = true
ackedBytes += len(bcMsg.Body)
}
}
if !messagesToACK {
if !forwardChannel {
// Retransmit any un-ACKed messages on the new back channel.
sw.si.BackChannelAID = aid
sw.backChannelBytes = remainingBytes
}
return true
}
err = sw.BackChannelACKThrough(aid)
if err != nil {
sm.Error(r, err)
http.Error(w, "Unable to ACK back channel up to AID", 400)
return false
}
if forwardChannel {
// Do not trigger retransmit on the current back channel
sw.backChannelBytes -= ackedBytes
} else {
// Retransmit any un-ACKed messages on the new back channel.
sw.si.BackChannelAID = aid
sw.backChannelBytes = remainingBytes
}
debug("wc: %s ACKed back channel %d bytes up to AID %d", sw.SID(),
ackedBytes, aid)
return true
}
func activityProxyWorker(sw *sessionWrapper, activityNotifier chan int) {
var an chan int
var proxiedByteCount int
for {
// TODO(hochhaus): shutdown activityProxyWorker when the corresponding
// session has been terminated.
select {
case i := <-sw.DataNotifier():
debug("wc: %s new back channel data %d bytes (proxied)", sw.SID(), i)
proxiedByteCount += i
if proxiedByteCount > 0 {
an = activityNotifier
}
case an <- proxiedByteCount:
debug("wc: %s new back channel data %d bytes (non-proxied)", sw.SID(),
proxiedByteCount)
proxiedByteCount = 0
an = nil
}
}
}
func sessionWorker(sw *sessionWrapper, activityNotifier chan int) {
for {
select {
case <-sw.noopTimer.C:
noop(sw)
case <-sw.longBackChannelTimer.C:
longBackChannel(sw)
case <-sw.backChannelCloseNotifier:
backChannelClose(sw)
case reqRequest := <-sw.reqNotifier:
switch {
case reqRequest.r.FormValue("TYPE") == "xmlhttp" ||
reqRequest.r.FormValue("TYPE") == "html":
backChannel(sw, reqRequest)
case reqRequest.r.FormValue("TYPE") == "terminate":
clientTerminate(sw, reqRequest)
case reqRequest.r.FormValue("SID") == "":
newSessionHandler(sw, reqRequest)
default:
fcHandler(sw, reqRequest)
}
case sa := <-sw.Notifier():
switch {
case sa == ServerTerminate:
debug("wc: %s server terminate session", sw.SID(), sa)
err := sm.TerminatedSession(sw.Session, ServerTerminateRequest)
if err != nil {
sm.Error(sw.bc.r, err)
}
if sw.bc != nil {
// TODO(hochhaus): persist the session termination until the client
// ACKs it?
msgs := []*Message{
&Message{0, []byte(jsonArray([]interface{}{"stop"}))},
}
sw.p.chunkMessages(msgs)
sw.p.end()
sw.BackChannelClose()
close(sw.bc.done)
sw.bc = nil
sw.p = nil
sw.backChannelCloseNotifier = nil
sw.noopTimer.Stop()
sw.longBackChannelTimer.Stop()
}
break
default:
panic(fmt.Sprintf("Unsupported SessionActivity: %d", sa))
}
case sa := <-activityNotifier:
debug("wc: %s new back channel data %d bytes", sw.SID(), sa)
// BackChannelActivity
sw.backChannelBytes += sa
if sw.bc != nil {
if err := flushPending(sw); err != nil {
sm.Error(sw.bc.r, err)
}
}
}
}
}