-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
188 lines (151 loc) · 3.55 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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
"os/exec"
"strings"
"time"
)
func waitingHandler() http.HandlerFunc {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
fmt.Println("received request")
customHeader := r.Header.Get("User-Agent")
if customHeader == "well-read" {
fmt.Printf("received request with user-agent=well-read\n")
}
t1 := time.Now()
_, err := io.Copy(ioutil.Discard, r.Body)
if err != nil {
log.Println("failed to discard the body")
w.WriteHeader(http.StatusTeapot)
return
}
t2 := time.Now()
fmt.Printf("received body in %s\n", t2.Sub(t1))
if t2.Sub(t1) > time.Second {
fmt.Printf("received body slower than 1s: %s\n", t2.Sub(t1))
}
dump, err := httputil.DumpRequest(r, false)
if err != nil {
log.Println("failed to dump the request")
w.WriteHeader(http.StatusTeapot)
return
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
fmt.Printf("dumping request: %s", dump)
})
}
var netstatStates = []string{
"LISTEN",
"ESTABLISHED",
"SYN_SENT",
"SYN_RECV",
"LAST_ACK",
"CLOSE_WAIT",
"TIME_WAIT",
"CLOSED",
"CLOSING",
"FIN_WAIT1",
"FIN_WAIT2",
}
func collectNetstat() {
nt, err := exec.Command("netstat", "-at").CombinedOutput()
if err != nil {
fmt.Printf("failed to get netstat: %s\n", err)
return
}
counts := make([]string, len(netstatStates))
for i, state := range netstatStates {
counts[i] = fmt.Sprintf("%s=%d", state, strings.Count(string(nt), state))
}
fmt.Printf("netstat stats: %s\n", strings.Join(counts, " "))
}
func main() {
fmt.Printf("args: %#v\n", os.Args)
if len(os.Args) == 4 && os.Args[1] == "slowpost" {
err := makeSlowReq(os.Args[2], os.Args[3])
if err != nil {
fmt.Println("slow req: %s", err)
os.Exit(1)
}
os.Exit(0)
}
go func() {
defer func() {
if e := recover(); e != nil {
fmt.Println("Recovered in netstat: %s", e)
}
}()
for {
collectNetstat()
time.Sleep(1 * time.Second)
}
}()
http.HandleFunc("/", waitingHandler())
log.Fatal(http.ListenAndServe(":8080", nil))
}
func makeSlowReq(reqURL string, readDelay string) error {
dur, err := time.ParseDuration(readDelay)
if err != nil {
return err
}
fmt.Printf("will sleep for %s\n", dur)
req, err := http.NewRequest("POST", reqURL, nil)
if err != nil {
return err
}
beginningData := []byte(strings.Repeat("k", 9000))
remainingData := []byte("=v\n")
req.ContentLength = int64(len(beginningData) + len(remainingData))
req.Body = &slowReader{dur, bytes.NewReader(beginningData), remainingData, 0}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("User-Agent", "well-read")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Printf("response: %s\n%s\n", resp.Status, respBytes)
return nil
}
type slowReader struct {
delay time.Duration
reader io.Reader
remainingData []byte
state int
}
func (r *slowReader) Read(p []byte) (int, error) {
switch r.state {
case 0:
n, err := r.reader.Read(p)
if err == io.EOF {
r.state = 1
return n, nil
}
return n, err
case 1:
r.state = 2
fmt.Printf("sleeping...\n")
time.Sleep(r.delay)
fmt.Printf("done sleeping...\n")
case 2:
r.state = 3
return copy(p, r.remainingData), nil
case 3:
return 0, io.EOF
}
return 0, nil
}
func (r *slowReader) Close() (err error) { return nil }