-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
175 lines (151 loc) · 3.13 KB
/
http.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
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"time"
"golang.org/x/time/rate"
)
var (
host = flag.String("host", "localhost", "Host to listen on.")
port = flag.Int("port", 8080, "Port to listen on.")
)
func main() {
flag.Parse()
http.HandleFunc("/", http.FileServer(http.Dir("static")).ServeHTTP)
http.HandleFunc("/boom", boom)
addr := fmt.Sprintf("%s:%d", *host, *port)
log.Printf("Listening on %s", addr)
http.ListenAndServe(addr, nil)
}
type result struct {
err error
code int
duration time.Duration
}
type bundle struct {
Err int
Good int
Bad int
Min time.Duration
Max time.Duration
ErrText string
}
func (b *bundle) add(r result) {
if r.err != nil {
b.Err++
b.ErrText = r.err.Error()
} else if r.code > 299 {
b.Bad++
} else {
b.Good++
}
if r.duration > b.Max {
b.Max = r.duration
}
if r.duration < b.Min {
b.Min = r.duration
}
}
func semaphore(n int) chan bool {
ch := make(chan bool, n)
for i := 0; i < n; i++ {
ch <- true
}
return ch
}
func boom(w http.ResponseWriter, r *http.Request) {
wf, ok := w.(WriteFlusher)
if !ok {
http.Error(w, "ResponseWriter is not a Flusher", 500)
return
}
url := r.FormValue("url")
if url == "" {
http.Error(w, "Missing URL", http.StatusMethodNotAllowed)
return
}
log.Printf("GET %q", url)
if _, err := http.NewRequest("GET", url, nil); err != nil {
http.Error(w, "Invalid URL", http.StatusBadRequest)
return
}
// NOTE: ctx is cancelled when the client goes away.
ctx := r.Context()
w.Header().Set("Content-Type", "text/event-stream")
results := make(chan result, 10000)
// Write bundles to event stream.
go func() {
for b := range bundleResults(ctx, results, time.Second) {
j, _ := json.Marshal(b)
writeSSE(wf, j)
}
}()
const qps = 200
const concurrency = 50
rate := rate.NewLimiter(rate.Every(time.Second/qps), 1)
sem := semaphore(concurrency)
for {
if err := rate.Wait(ctx); err != nil {
log.Printf("rate.Wait(%q): %v", url, err)
return
}
<-sem
go func() {
start := time.Now()
resp, err := http.Get(url)
if err != nil {
r := result{err: err, duration: time.Now().Sub(start)}
sem <- true
results <- r
return
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
r := result{code: resp.StatusCode, duration: time.Now().Sub(start)}
sem <- true
results <- r
}()
}
}
type WriteFlusher interface {
io.Writer
http.Flusher
}
func writeSSE(wf WriteFlusher, data []byte) {
bb := bytes.Split(data, []byte("\n"))
for _, b := range bb {
wf.Write([]byte("data: "))
wf.Write(b)
wf.Write([]byte("\n"))
}
wf.Write([]byte("\n"))
wf.Flush()
}
func bundleResults(ctx context.Context, results chan result, d time.Duration) chan bundle {
bundles := make(chan bundle, 1)
tick := time.NewTicker(time.Second)
go func() {
b := bundle{Min: time.Duration(1<<63 - 1)}
for {
select {
case <-ctx.Done():
close(bundles)
tick.Stop()
return
case <-tick.C:
bundles <- b
b = bundle{Min: time.Duration(1<<63 - 1)}
case r := <-results:
b.add(r)
}
}
}()
return bundles
}