-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathmain.go
105 lines (91 loc) · 1.92 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
package main
import (
"bytes"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"sync"
)
type RequestTracker struct {
mu sync.Mutex
requests [][]byte
}
func (rt *RequestTracker) Track(req *http.Request) {
rt.mu.Lock()
defer rt.mu.Unlock()
// alloc 10KB for each track
rt.requests = append(rt.requests, bytes.Repeat([]byte("a"), 10000))
}
var (
requests RequestTracker
)
func init() {
requests = RequestTracker{}
}
func read(w http.ResponseWriter, r *http.Request) {
requests.Track(r)
r.Body.Close()
w.WriteHeader(http.StatusOK)
f, err := os.Open("/proc/1/status")
if err != nil {
fmt.Println(err)
}
stat := fmt.Sprint(f.Stat())
w.Write([]byte(stat))
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func testBreak(w http.ResponseWriter, r *http.Request) {
i := 0
for i < 1000 {
if fileExists("/etc/test_break") {
fmt.Println("File exists, breaking.")
break
} else {
fmt.Println("File not created yet.")
}
i = i + 1
}
}
func write(w http.ResponseWriter, r *http.Request) {
f, err := os.Create("/etc/test_break")
if err != nil {
fmt.Println(err)
return
}
l, err := f.WriteString("Hello World")
if err != nil {
fmt.Println(err)
f.Close()
return
}
requests.Track(r)
r.Body.Close()
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprint(l, "bytes written successfully")))
err = f.Close()
if err != nil {
fmt.Println(err)
return
}
}
func main() {
fmt.Printf("Starting Server on port :%s\n", "8080")
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
requests.Track(r)
r.Body.Close()
w.WriteHeader(http.StatusOK)
w.Write([]byte(`OK`))
})
http.HandleFunc("/read", read)
http.HandleFunc("/break", testBreak)
http.HandleFunc("/write", write)
log.Fatal(http.ListenAndServe(":8080", nil))
}