This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslides.slide
167 lines (127 loc) · 3.85 KB
/
slides.slide
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
Go away
28 Jun 2017
Tags: go, http, overload, shedding
Robert Vollmert
* When are you overloaded?
- when you get more requests per time unit than you can handle in that time
* When are you overloaded? Graph!
- when you get more requests per time unit than you can handle in that time
.image graphite.png _ 500
- not that easy to detect reliably automatically
- traffic spikes vs broad traffic patterns
* What to do when you're overloaded?
- just keep going
- but tell the client it's going to take a while?
- scale up (but what if you can't?)
- shedding
* An example
+package main
+
+import (
+ "io"
+ "log"
+ "net/http"
+)
+
+func main() {
+ http.HandleFunc("/echo", EchoHandler)
+ log.Fatal(http.ListenAndServe(":8765", nil))
+}
+
+func EchoHandler(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ w.Header().Set("Content-Type", "text/plain")
+ io.Copy(w, r.Body)
+}
* Ok, that was boring
_import (
"io"
"log"
"net/http"
+ "time"
)
func main() {
http.HandleFunc("/echo", EchoHandler)
+ http.HandleFunc("/work", WorkHandler)
log.Fatal(http.ListenAndServe(":8765", nil))
}
+func work() {
+ time.Sleep(50*time.Millisecond)
+}
+
+func WorkHandler(w http.ResponseWriter, r *http.Request) {
+ work()
+ EchoHandler(w, r)
+}
* Life of a (bid) request
- read and parse request
- look up user
- make a bid
- format and send response
* And this is where things start to break down
_func main() {
http.HandleFunc("/echo", EchoHandler)
http.HandleFunc("/work", WorkHandler)
+ http.HandleFunc("/worklimit", WorkLimitHandler)
log.Fatal(http.ListenAndServe(":8765", nil))
}
+var workers = make(chan struct{}, numWorkers)
+
+func WorkLimitHandler(w http.ResponseWriter, r *http.Request) {
+ workers <- struct{}{}
+ WorkHandler(w, r)
+ <-workers
+}
* On call centers
- queue
- tell?
- stack!
* On queueing
- net/http is an implicit queue, watch the goroutine count go up as you overload
- converting to a stack is not straightforward, but fun
* What is load shedding?
short-circuit requests when you're overloaded
- 503
- static / cached copy
- no bid
* Approaches to load shedding
- limit number of concurrent requests
* Limit number of concurrent requests
_func main() {
http.HandleFunc("/echo", EchoHandler)
http.HandleFunc("/work", WorkHandler)
http.HandleFunc("/worklimit", WorkLimitHandler)
+ http.HandleFunc("/worklimitshed", ShedLimit(WorkLimitHandler, 4))
log.Fatal(http.ListenAndServe(":8765", nil))
}
+func ShedLimit(handle http.HandlerFunc, maxConcurrent int) http.HandlerFunc {
+ reqs := make(chan struct{}, maxConcurrent)
+ return func(w http.ResponseWriter, r *http.Request) {
+ select {
+ case reqs <- struct{}{}:
+ handle(w, r)
+ <-reqs
+ default:
+ http.Error(w, "overloaded", 503)
+ }
+ }
+}
* Approaches to load shedding (2)
- limit number of concurrent requests
- queue requests briefly
_select {
case reqs <- struct{}{}:
handle(w, r)
<-reqs
-default:
+case <-time.After(5 * time.Millisecond):
http.Error(w, "overloaded", 503)
}
- stacks!
* Closing
- source code and slides at https://github.com/dcmn-com/goaway
- wrk doesn't work, find/write a better tool next time
- I can't get a useful graph out of prometheus
* Audience input
- vegeta (https://github.com/tsenart/vegeta) as an alternative to wrk
- Google's SRE book as a recommended complementary read, e.g.: https://landing.google.com/sre/book/chapters/handling-overload.html