forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
56 lines (46 loc) · 1.09 KB
/
trace.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program that implements a simple web service that will allow us to
// explore how to use the schedtrace. This code is leaking a gorutine.
package main
import (
"encoding/json"
"log"
"math/rand"
"net/http"
"os"
"time"
)
var leak bool
func main() {
http.HandleFunc("/sendjson", sendJSON)
// Leak goroutines if we have any argument.
if len(os.Args) == 2 {
leak = true
}
log.Printf("listener : Started : Listening on: http://localhost:4000 : Leak[%v]\n", leak)
http.ListenAndServe(":4000", nil)
}
// sendJSON returns a simple JSON document.
func sendJSON(rw http.ResponseWriter, r *http.Request) {
// Leak a goroutine every so often.
if leak {
if rand.Intn(100) == 5 {
go func() {
for {
time.Sleep(time.Millisecond * 10)
}
}()
}
}
u := struct {
Name string
Email string
}{
Name: "bill",
Email: "bill@ardanlabs.com",
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(200)
json.NewEncoder(rw).Encode(&u)
}