-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
572 lines (540 loc) · 14.9 KB
/
main.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright (c) 2024 Jay R. Wren
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"slices"
"strconv"
"strings"
"syscall"
"time"
"github.com/NYTimes/gziphandler"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
xnws "golang.org/x/net/websocket"
)
var (
conns []*Connection
)
func main() {
err := os.WriteFile("/run/app.pid", []byte(strconv.Itoa(os.Getpid())), os.ModePerm)
if err != nil {
log.Println("could not write /run/app.pid")
}
var httpPort, httpsPort int
var certfile, initconns string
var gosocket bool
flag.BoolVar(&gosocket, "gosocket", false, "serve websocket with golang.org/x/net/websocket insetad of ")
flag.IntVar(&httpPort, "httpPort", 8080, "http listen port")
flag.IntVar(&httpsPort, "httpsPort", 8443, "https listen port")
flag.StringVar(&certfile, "certfile", "", "certificate file for https (combined with key)")
// e.g. -initconns tcpbin.com:4242_35s_"ping\n"
// or www.example.net:80_25s_"GET / HTTP/1.1\r\nHost: %s\r\n\r\n"
flag.StringVar(&initconns, "initconns", "", "initial remote connections - comma separated host:port_delay_payload pairs")
flag.Parse()
doinitconns(initconns)
log.Print("initialized ", len(conns), " connections")
r := http.NewServeMux()
// TODO: convert all of these handlers to InstrumentHandlerInFlight,
// InstrumentHandlerDuration, InstrumentHandlerCounter,
// InstrumentHandlerResponseSize chain
r.Handle("/metrics", promhttp.Handler())
r.Handle("/livez", http.HandlerFunc(livezreadyz))
r.Handle("/readyz", http.HandlerFunc(livezreadyz))
r.Handle("/", gziphandler.GzipHandler(http.HandlerFunc(root)))
r.Handle("/slow", gziphandler.GzipHandler(http.HandlerFunc(slow)))
r.Handle("/slam", gziphandler.GzipHandler(http.HandlerFunc(slam)))
r.Handle("/slam/headers", gziphandler.GzipHandler(http.HandlerFunc(headerSlam)))
r.Handle("/slam/body", gziphandler.GzipHandler(http.HandlerFunc(bodySlam)))
r.Handle("/connections", gziphandler.GzipHandler(http.HandlerFunc(connections)))
r.Handle("/headers", gziphandler.GzipHandler(http.HandlerFunc(headers)))
r.Handle("/gs-echo", xnws.Handler(echoServerXNWS))
r.Handle("/gs-pinger", xnws.Handler(pingerXNWS))
r.Handle("/ws-echo", gziphandler.GzipHandler(http.HandlerFunc(echoServer)))
r.Handle("/ws-pinger", gziphandler.GzipHandler(http.HandlerFunc(pinger)))
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
var tlsServer *http.Server
tlsAddr := ":" + strconv.FormatInt(int64(httpsPort), 10)
if certfile != "" {
tlsServer = &http.Server{Addr: tlsAddr,
Handler: r}
go func() {
log.Print("starting server on ", tlsAddr)
err := tlsServer.ListenAndServeTLS(certfile, certfile)
if err != nil {
log.Print("error from TLS server: ", err)
}
}()
}
addr := ":" + strconv.FormatInt(int64(httpPort), 10)
log.Print("starting server on ", addr)
server := &http.Server{Addr: addr, Handler: r}
go func() {
<-ctx.Done()
log.Printf("got signal %v to shut down", os.Interrupt)
shutdownCtx := context.Background()
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
defer cancel()
go func() {
if tlsServer != nil {
err := tlsServer.Shutdown(shutdownCtx)
if err != nil {
log.Fatalf("error shutting down TLS http.Server: %v", err)
}
}
}()
err := server.Shutdown(shutdownCtx)
if err != nil {
log.Fatalf("error shutting down http.Server: %v", err)
}
}()
err = server.ListenAndServe()
if err != http.ErrServerClosed {
log.Fatal(err)
}
}
func root(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, `Endpoints on this server:
/slow - responds slowly - accepts query params: chunk, delay, duration
/slam - closes the connection without writing headers or body - accepts query param: duration
/slam/headers - closes connection after writing headers - accepts query param: duration
/slam/body - closes connection after writing 1/2 the body - accepts query param: duration, len
/connections - list (GET) and create (POST) remote TCP connections
/headers - respond with request headers sent as text body
/ws-echo - a websocket connection which echoes lines in response
/ws-pinger - a websocket connection which pings every 10s - accepts query param: delay
/gs-echo - a go websocket connection which echoes lines in response
/gs-pinger - a go websocket connection which pings every 10s - accepts query param: delay
The /gs-echo and /gs-pinger endpoints use golang.org/x/net/websocket which does
not use data framing as defined in RFC6455.
`)
}
// slam closes the connection without writing anything.
func slam(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
t := timeQueryParam(r.Form, "duration", time.Duration(0))
time.Sleep(t)
panic("slam!")
}
// headerSlam writes some headers and then closes the connection before writing body.
func headerSlam(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
t := timeQueryParam(r.Form, "duration", time.Duration(0))
time.Sleep(t)
w.Header().Add("Content-Type", "text")
w.Header().Add("Content-Length", "1024")
w.WriteHeader(200)
}
// bodySlam writes headers and then closes the connection before completely writing body.
func bodySlam(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
t := timeQueryParam(r.Form, "duration", time.Duration(0))
l := r.Form.Get("len")
ll, err := strconv.Atoi(l)
if err != nil {
if l != "" {
log.Print(err)
}
ll = 512
}
w.Header().Add("Content-Type", "text")
w.Header().Add("Content-Length", strconv.Itoa(ll*2))
w.WriteHeader(200)
time.Sleep(t)
f, err := os.Open("/usr/share/dict/words")
if err != nil {
log.Print("couldn't open /usr/share/dict/words")
return
}
defer f.Close()
io.Copy(w, io.LimitReader(f, int64(ll)))
}
func headers(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text")
for i := range r.Header {
for _, v := range r.Header[i] {
io.WriteString(w, i)
io.WriteString(w, ": ")
io.WriteString(w, v)
io.WriteString(w, "\n")
}
}
}
func connections(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
connectionsGet(w, r)
case http.MethodPost:
connectionsPost(w, r)
case http.MethodDelete:
connectionsDelete(w, r)
}
}
// TODO: support more content types.
func connectionsGet(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
long := strings.ToLower(r.FormValue("long"))
for i := range conns {
if i != 0 {
fmt.Fprintln(w)
}
c := conns[i]
fmt.Fprintf(w, "%d reconnects:%d totalRR:%d %s %s\n", i,
c.reconnects, c.totalRR, c.RemoteAddr(), c.LocalAddr())
switch {
case long == "true" || len(c.last_read) < 80:
fmt.Fprintf(w, "last read: %s\n", c.last_read)
default:
fmt.Fprintf(w, "last read: %s\n", c.last_read[0:80])
}
if c.err != nil {
fmt.Fprintf(w, "err: %v", c.err)
c.err = nil
}
}
}
func connectionsPost(w http.ResponseWriter, r *http.Request) {
buf := make([]byte, 1024)
n, err := r.Body.Read(buf)
if err != nil && err != io.EOF {
log.Printf("error reading request body %#v: %s", r, err)
}
addConnection(string(buf[0:n]))
w.WriteHeader(http.StatusAccepted)
}
func connectionsDelete(w http.ResponseWriter, r *http.Request) {
buf := make([]byte, 1024)
n, err := r.Body.Read(buf)
if err != nil && err != io.EOF {
log.Printf("error reading request body %#v: %s", r, err)
}
i, err := strconv.Atoi(string(buf[0:n]))
if err != nil {
log.Printf("error parsing request body %#v: %s", r, err)
http.Error(w, http.StatusText(http.StatusBadRequest),
http.StatusBadRequest)
return
}
rmConnection(i)
}
func slow(w http.ResponseWriter, r *http.Request) {
slow_req_metric.Inc()
// The slow return of this function is to take 5 minutes.
// We shall return ~1MB total. and use american english dictionary for fun.
f, err := os.Open("/usr/share/dict/words")
if err != nil {
log.Print("couldn't open /usr/share/dict/words")
return
}
defer f.Close()
r.ParseForm()
help := `query params are chunk, delay, duration, help`
if !strings.HasPrefix(r.Form.Get("help"), "n") {
io.WriteString(w, help)
}
t := timeQueryParam(r.Form, "duration", 5*time.Minute)
delay := timeQueryParam(r.Form, "delay", 2*time.Second)
st, err := f.Stat()
if err != nil {
log.Print("couldn't stat /usr/share/dict/words")
http.Error(w, "could not stat /usr/share/dict/words", 500)
return
}
src, dst := f, w
sz := int(st.Size())
dd := int(t / delay)
chunk := 10
if dd != 0 {
chunk = sz / dd
}
if r.Form.Has("chunk") {
if c, err := strconv.ParseInt(r.Form.Get("chunk"), 10, 64); err == nil {
chunk = int(c)
} else {
log.Print("failed to parse chunk query param", r.Form.Get("chunk"))
}
}
log.Printf("/slow writing %d every %s for %s", chunk, delay, t)
// TODO: consider calculating correct content-length and setting it
if t == 5*time.Minute {
w.Header().Set("content-length", strconv.Itoa(sz))
}
buf := make([]byte, chunk)
start := time.Now()
// lifted from io:
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw < 0 || nr < nw {
nw = 0
if ew == nil {
ew = errInvalidWrite
}
}
w.(http.Flusher).Flush()
if time.Since(start) > t {
break
}
time.Sleep(delay)
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
if err != nil {
log.Printf("/slow error writing %s", err)
}
}
var upgrader = websocket.Upgrader{} // use default options
// Echo the data received on the WebSocket.
func echoServer(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
}
func pinger(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
delay := timeQueryParam(r.Form, "delay", 10*time.Second)
n := 0
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("pinger upgrade:", err)
return
}
defer c.Close()
for {
// TODO: use c.PingHandler()
n++
err = c.WriteMessage(websocket.TextMessage,
[]byte(fmt.Sprintf("%d\n", n)))
if err != nil {
if !errors.Is(err, syscall.EPIPE) && err != io.ErrClosedPipe {
log.Printf("pinger write error: %s", err)
}
return
}
time.Sleep(delay)
}
}
// Echo the data received on the WebSocket.
func echoServerXNWS(ws *xnws.Conn) {
io.Copy(ws, ws)
}
func pingerXNWS(ws *xnws.Conn) {
r := ws.Request()
r.ParseForm()
delay := timeQueryParam(r.Form, "delay", 10*time.Second)
buf := make([]byte, 1500)
n := 0
for {
ws.SetReadDeadline(time.Now().Add(1 * time.Second))
br, err := ws.Read(buf)
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
if errors.Is(err, io.EOF) {
return
}
log.Printf("pinger read error: %s %T", err, err)
return
}
if br > 0 {
log.Printf("pinger read: %s", buf[:br])
}
time.Sleep(delay)
n++
_, err = fmt.Fprintf(ws, "%d\n", n)
if err != nil {
log.Printf("pinger write error: %s", err)
return
}
}
}
func timeQueryParam(v url.Values, name string, t time.Duration) time.Duration {
d := v.Get(name)
if d != `` {
if t2, err := time.ParseDuration(d); err == nil {
t = t2
} else {
log.Print("couldn't parse query parameter", name, d, err)
}
}
return t
}
// errInvalidWrite means that a write returned an impossible count.
var errInvalidWrite = errors.New("invalid write result")
// Connection is a net.Conn wrapped for our purpose
type Connection struct {
net.Conn
err error
last_read string
addr string
delay time.Duration
reconnects int
totalRR int // total request responses.
payload string
}
func doinitconns(initconns string) {
if initconns == "" {
return
}
points := strings.Split(initconns, ",")
for i := range points {
addConnection(points[i])
}
}
func addConnection(conndef string) error {
addr, after, found := strings.Cut(conndef, "_")
delay := time.Minute
payload := ""
if found {
ds, ps, _ := strings.Cut(after, "_")
d, err := time.ParseDuration(ds)
if err != nil {
log.Print("could not parse ", conndef, err)
return err
}
delay = d
payload = ps
}
c, err := net.Dial("tcp", addr)
if err != nil {
log.Print("error connecting: ", addr, err)
return err
}
// Do naive \r\n replacement. Sadly, no support for a literal.
payload = strings.ReplaceAll(payload, "\\n", "\n")
payload = strings.ReplaceAll(payload, "\\r", "\r")
conn := &Connection{
Conn: c,
addr: addr,
delay: delay,
payload: payload,
}
cur_conn_metric.Inc()
i := len(conns)
conns = append(conns, conn)
log.Printf("parsed connection %#v", conn)
go connloop(i, conn)
return nil
}
func rmConnection(i int) error {
if i >= len(conns) {
return errors.ErrUnsupported
}
err := conns[i].Close()
if err != nil {
log.Printf("error closing conn %d %s", i, err)
// Intentionally not returning here because we must
}
cur_conn_metric.Dec()
// Use nil as sentinel that it has been removed.
conns[i].Conn = nil
conns = slices.Delete(conns, i, i+1)
return nil
}
func replaceConnection(i int) *Connection {
err := conns[i].Close()
if err != nil {
log.Printf("error closing conn %s", err)
}
c, err := net.Dial("tcp", conns[i].addr)
if err != nil {
log.Print("error connecting to", conns[i].addr, err)
}
conns[i] = &Connection{
Conn: c,
err: err,
addr: conns[i].addr,
delay: conns[i].delay,
reconnects: conns[i].reconnects + 1,
payload: conns[i].payload,
}
return conns[i]
}
func connloop(i int, c *Connection) {
buffer := make([]byte, 1024)
for {
if c.Conn == nil {
log.Print("ending old loop", i, "no connection")
return
}
n, err := fmt.Fprintf(c, c.payload)
if err != nil {
log.Printf("error writing to %v: %v", c, err)
c.err = err
c = replaceConnection(i)
continue
}
if n == 0 {
log.Printf("error 2 writing to %v: write returned 0", c)
c.err =
fmt.Errorf("error 2 writing to %v: write returned 0", c)
}
n, err = c.Read(buffer)
if err != nil {
log.Printf("error reading from %v", err)
c.err = fmt.Errorf("error reading from %v: Read returned 0", c)
c = replaceConnection(i)
continue
}
c.last_read = string(buffer[0:n])
c.totalRR += 1
time.Sleep(c.delay)
}
}
var (
slow_req_metric = promauto.NewCounter(prometheus.CounterOpts{
Name: "slowserver_processed_slow_req_total",
Help: "The total number of /slow requests",
})
cur_conn_metric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "slowserver_cur_conn",
Help: "The current number of tracked connections (/connection)",
})
)
func livezreadyz(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if r.Form.Has("verbose") { // 😀
io.WriteString(w, "READY OK")
}
io.WriteString(w, "OK")
}