-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
443 lines (397 loc) · 13 KB
/
service.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/abbot/go-http-auth"
"io/ioutil"
"log"
"math"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"sync"
"time"
)
const (
wdHub = "/wd/hub/"
statusPath = "/status"
queuePath = wdHub
badRequestPath = "/badRequest"
pingPath = "/ping"
badRequestMessage = "msg"
slash = "/"
)
var (
sessions = make(Sessions)
timeoutCancels = make(map[string]chan bool)
leases = make(map[string]Lease)
sessionLock sync.RWMutex
stateLock sync.Mutex
updateLock sync.Mutex
)
func badRequest(w http.ResponseWriter, r *http.Request) {
msg := r.URL.Query().Get(badRequestMessage)
if msg == "" {
msg = "bad request"
}
http.Error(w, msg, http.StatusBadRequest)
}
type requestInfo struct {
maxConnections int
browser BrowserId
browserState BrowserState
processName string
process *Process
command string
lease Lease
error error
}
func getRequestInfo(r *http.Request) *requestInfo {
quotaName, _, _ := r.BasicAuth()
stateLock.Lock()
defer stateLock.Unlock()
if _, ok := state[quotaName]; !ok {
state[quotaName] = &QuotaState{}
}
quotaState := *state[quotaName]
err, browserName, version, processName, priority, command := parsePath(r.URL)
if err != nil {
return &requestInfo{0, BrowserId{}, nil, "", nil, "", 0, err}
}
browserId := BrowserId{Name: browserName, Version: version}
if _, ok := quotaState[browserId]; !ok {
quotaState[browserId] = &BrowserState{}
}
browserState := *quotaState[browserId]
maxConnections := quota.MaxConnections(quotaName, browserName, version)
process := getProcess(browserState, processName, priority, maxConnections)
return &requestInfo{maxConnections, browserId, browserState, processName, process, command, 0, nil}
}
type transport struct {
http.RoundTripper
}
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
requestInfo := getRequestInfo(r)
ctx, _ := context.WithTimeout(r.Context(), requestTimeout)
r = r.WithContext(ctx)
err := requestInfo.error
if err != nil {
log.Printf("[INVALID_REQUEST] [%v]\n", err)
redirectToBadRequest(r, err.Error())
return t.RoundTripper.RoundTrip(r)
}
// Only new session requests should wait in queue
command := requestInfo.command
browserId := requestInfo.browser
processName := requestInfo.processName
process := requestInfo.process
isNewSessionRequest := isNewSessionRequest(r.Method, command)
if isNewSessionRequest {
log.Printf("[CREATING] [%s %s] [%s] [%d]\n", browserId.Name, browserId.Version, processName, process.Priority)
if process.CapacityQueue.Capacity() == 0 {
refreshCapacities(requestInfo.maxConnections, requestInfo.browserState)
if process.CapacityQueue.Capacity() == 0 {
log.Printf("[NOT_ENOUGH_SESSIONS] [%s %s] [%s]\n", browserId.Name, browserId.Version, processName)
redirectToBadRequest(r, "Not enough sessions for this process. Come back later.")
return t.RoundTripper.RoundTrip(r)
}
}
process.AwaitQueue <- struct{}{}
lease, disconnected := process.CapacityQueue.Push(r.Context())
<-process.AwaitQueue
if disconnected {
log.Printf("[CLIENT_DISCONNECTED_FROM_QUEUE] [%s %s] [%s] [%d]\n", browserId.Name, browserId.Version, processName, process.Priority)
return emptyResponse(), nil
}
requestInfo.lease = lease
}
//Here we change request url
r.URL.Scheme = "http"
r.URL.Host = destination
r.URL.Path = fmt.Sprintf("%s%s", wdHub, command)
resp, err := t.RoundTripper.RoundTrip(r)
select {
case <-r.Context().Done():
{
log.Printf("[CLIENT_DISCONNECTED] [%s %s] [%s] [%d]\n", browserId.Name, browserId.Version, requestInfo.processName, process.Priority)
cleanupQueue(isNewSessionRequest, requestInfo)
return emptyResponse(), nil
}
default:
{
if err != nil {
log.Printf("[REQUEST_ERROR] [%s %s] [%s] [%d] [%v]\n", browserId.Name, browserId.Version, requestInfo.processName, process.Priority, err)
cleanupQueue(isNewSessionRequest, requestInfo)
} else {
processResponse(isNewSessionRequest, requestInfo, r, resp)
}
}
}
if r.Body != nil {
r.Body.Close()
}
return resp, err
}
func emptyResponse() *http.Response {
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
StatusCode: http.StatusOK,
}
}
func processResponse(isNewSessionRequest bool, requestInfo *requestInfo, r *http.Request, resp *http.Response) {
browserId := requestInfo.browser
processName := requestInfo.processName
process := requestInfo.process
if isNewSessionRequest {
if resp.StatusCode == http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
var reply map[string]interface{}
if json.Unmarshal(body, &reply) != nil {
log.Printf("[JSON_ERROR] [%s %s] [%s] [%d]\n", browserId.Name, browserId.Version, processName, process.Priority)
cleanupQueue(isNewSessionRequest, requestInfo)
return
}
rawSessionId := reply["sessionId"]
switch rawSessionId.(type) {
case string:
{
sessionId := rawSessionId.(string)
cancelTimeout := make(chan bool)
sessionLock.Lock()
sessions[sessionId] = process
timeoutCancels[sessionId] = cancelTimeout
leases[sessionId] = requestInfo.lease
sessionLock.Unlock()
storage.AddSession(sessionId)
go func() {
select {
case <-time.After(requestTimeout):
{
deleteSessionWithTimeout(sessionId, requestInfo, true)
}
case <-cancelTimeout:
}
}()
storage.OnSessionDeleted(sessionId, func(id string) { deleteSession(id, requestInfo) })
resp.Body.Close()
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
log.Printf("[CREATED] [%s %s] [%s] [%d] [%s]\n", browserId.Name, browserId.Version, processName, process.Priority, sessionId)
return
}
}
}
log.Printf("[NOT_CREATED] [%s %s] [%s] [%d]\n", browserId.Name, browserId.Version, processName, process.Priority)
cleanupQueue(isNewSessionRequest, requestInfo)
}
if ok, sessionId := isDeleteSessionRequest(r.Method, requestInfo.command); ok {
deleteSession(sessionId, requestInfo)
}
}
func cleanupQueue(isNewSessionRequest bool, requestInfo *requestInfo) {
if isNewSessionRequest {
process := requestInfo.process
process.CapacityQueue.Pop(requestInfo.lease)
}
}
func deleteSession(sessionId string, requestInfo *requestInfo) {
deleteSessionWithTimeout(sessionId, requestInfo, false)
}
func deleteSessionWithTimeout(sessionId string, requestInfo *requestInfo, timedOut bool) {
browserId := requestInfo.browser
processName := requestInfo.processName
process := requestInfo.process
sessionLock.RLock()
process, ok := sessions[sessionId]
sessionLock.RUnlock()
if ok {
if timedOut {
log.Printf("[TIMED_OUT] [%s %s] [%s] [%d] [%s]\n", browserId.Name, browserId.Version, processName, process.Priority, sessionId)
}
log.Printf("[DELETING] [%s %s] [%s] [%d] [%s]\n", browserId.Name, browserId.Version, processName, process.Priority, sessionId)
sessionLock.Lock()
delete(sessions, sessionId)
if cancel, ok := timeoutCancels[sessionId]; ok {
delete(timeoutCancels, sessionId)
close(cancel)
}
lease := leases[sessionId]
delete(leases, sessionId)
sessionLock.Unlock()
process.CapacityQueue.Pop(lease)
log.Printf("[DELETED] [%s %s] [%s] [%d] [%s]\n", browserId.Name, browserId.Version, processName, process.Priority, sessionId)
}
storage.DeleteSession(sessionId)
}
func isNewSessionRequest(httpMethod string, command string) bool {
return httpMethod == http.MethodPost && command == "session"
}
func isDeleteSessionRequest(httpMethod string, command string) (bool, string) {
if httpMethod == http.MethodDelete && strings.HasPrefix(command, "session") {
pieces := strings.Split(command, slash)
if len(pieces) == 2 { //Against DELETE window url
return true, pieces[1]
}
}
return false, ""
}
func redirectToBadRequest(r *http.Request, msg string) {
r.URL.Scheme = "http"
r.URL.Host = listen
r.Method = "GET"
r.URL.Path = badRequestPath
values := r.URL.Query()
values.Set(badRequestMessage, msg)
r.URL.RawQuery = values.Encode()
}
func parsePath(url *url.URL) (error, string, string, string, int, string) {
p := strings.Split(strings.TrimPrefix(url.Path, wdHub), slash)
if len(p) < 5 {
err := errors.New(fmt.Sprintf("invalid url [%s]: should have format /browserName/version/processName/priority/command", url))
return err, "", "", "", 0, ""
}
priority, err := strconv.Atoi(p[3])
if err != nil {
priority = 1
}
return nil, p[0], p[1], p[2], priority, strings.Join(p[4:], slash)
}
func getProcess(browserState BrowserState, name string, priority int, maxConnections int) *Process {
updateLock.Lock()
defer updateLock.Unlock()
if _, ok := browserState[name]; !ok {
currentPriorities := getActiveProcessesPriorities(browserState)
currentPriorities[name] = priority
newCapacities := calculateCapacities(browserState, currentPriorities, maxConnections)
browserState[name] = createProcess(priority, newCapacities[name])
updateProcessCapacities(browserState, newCapacities)
}
process := browserState[name]
process.Priority = priority
process.LastActivity = time.Now()
return process
}
func createProcess(priority int, capacity int) *Process {
return &Process{
Priority: priority,
AwaitQueue: make(chan struct{}, math.MaxUint32),
CapacityQueue: CreateQueue(capacity),
LastActivity: time.Now(),
}
}
func getActiveProcessesPriorities(browserState BrowserState) ProcessMetrics {
currentPriorities := make(ProcessMetrics)
for name, process := range browserState {
if isProcessActive(process) {
currentPriorities[name] = process.Priority
}
}
return currentPriorities
}
func isProcessActive(process *Process) bool {
return len(process.AwaitQueue) > 0 || process.CapacityQueue.Size() > 0 || time.Now().Sub(process.LastActivity) < updateRate
}
func calculateCapacities(browserState BrowserState, activeProcessesPriorities ProcessMetrics, maxConnections int) ProcessMetrics {
sumOfPriorities := 0
membersCount := storage.MembersCount()
for _, priority := range activeProcessesPriorities {
sumOfPriorities += priority
}
ret := ProcessMetrics{}
for processName, priority := range activeProcessesPriorities {
ret[processName] = round(float64(priority) / float64(sumOfPriorities) * float64(maxConnections) / float64(membersCount))
}
for processName := range browserState {
if _, ok := activeProcessesPriorities[processName]; !ok {
ret[processName] = 0
}
}
return ret
}
func round(num float64) int {
i, frac := math.Modf(num)
if frac < 0.5 {
return int(i)
} else {
return int(i + 1)
}
}
func updateProcessCapacities(browserState BrowserState, newCapacities ProcessMetrics) {
for processName, newCapacity := range newCapacities {
process := browserState[processName]
process.CapacityQueue.SetCapacity(newCapacity)
}
}
func refreshCapacities(maxConnections int, browserState BrowserState) {
updateLock.Lock()
defer updateLock.Unlock()
currentPriorities := getActiveProcessesPriorities(browserState)
newCapacities := calculateCapacities(browserState, currentPriorities, maxConnections)
updateProcessCapacities(browserState, newCapacities)
}
func status(w http.ResponseWriter, r *http.Request) {
quotaName, _, _ := r.BasicAuth()
status := []BrowserStatus{}
if _, ok := state[quotaName]; ok {
quotaState := state[quotaName]
for browserId, browserState := range *quotaState {
processes := make(map[string]ProcessStatus)
for processName, process := range *browserState {
processes[processName] = ProcessStatus{
Priority: process.Priority,
Queued: len(process.AwaitQueue),
Processing: process.CapacityQueue.Size(),
Max: process.CapacityQueue.Capacity(),
LastActivity: process.LastActivity.Format(time.UnixDate),
}
}
status = append(status, BrowserStatus{
Name: browserId.String(),
Processes: processes,
})
}
}
json.NewEncoder(w).Encode(&status)
}
func ping(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK\n"))
}
func requireBasicAuth(authenticator *auth.BasicAuth, handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return authenticator.Wrap(func(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
handler(w, &r.Request)
})
}
func withCloseNotifier(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(r.Context())
go func() {
handler(w, r.WithContext(ctx))
cancel()
}()
select {
case <-w.(http.CloseNotifier).CloseNotify():
cancel()
case <-ctx.Done():
}
}
}
func mux() http.Handler {
mux := http.NewServeMux()
authenticator := auth.NewBasicAuthenticator(
"Selenium Load Balancer",
auth.HtpasswdFileProvider(usersFile),
)
proxyFunc := (&httputil.ReverseProxy{
Director: func(*http.Request) {},
Transport: &transport{http.DefaultTransport},
}).ServeHTTP
mux.HandleFunc(queuePath, requireBasicAuth(authenticator, withCloseNotifier(proxyFunc)))
mux.HandleFunc(statusPath, requireBasicAuth(authenticator, status))
mux.HandleFunc(badRequestPath, badRequest)
mux.HandleFunc(pingPath, ping)
return mux
}