-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (74 loc) · 1.84 KB
/
Copy pathmain.go
File metadata and controls
90 lines (74 loc) · 1.84 KB
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
package main
import (
"flag"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/bazelbuild/rules_go/go/runfiles"
)
var fibSink int
func main() {
sleepTime := flag.Duration("sleep-time", 0, "How long to sleep before binding the port")
busyWaitTime := flag.Duration("busy-time", 0, "How long to busy-wait before binding the port")
dieAfter := flag.Duration("die-after", 0, "How long to wait before self-destructing")
fileToOpen := flag.String("file-to-open", "", "A file to open to check runfiles")
soReuseport := flag.Bool("so-reuseport", false, "If true, sets SO_REUSEPORT when binding the address")
port := flag.String("port", "", "Port to bind")
flag.Parse()
if *dieAfter != 0 {
go func() {
<-time.After(*dieAfter)
os.Exit(1)
}()
}
if *port == "" {
portStr := os.Getenv("PORT")
port = &portStr
}
if *fileToOpen != "" {
resolvedPath, err := runfiles.Rlocation(*fileToOpen)
if err != nil {
panic(err)
}
f, err := os.Open(resolvedPath)
if err != nil {
panic(err)
}
f.Close()
}
log.Println("started")
time.Sleep(*sleepTime)
log.Println("done sleeping")
finishBusyWait := time.Now().Add(*busyWaitTime)
for time.Now().Before(finishBusyWait) {
fibSink += fib(10)
}
dob := time.Now()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
http.HandleFunc("/dob", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(dob.String()))
})
http.HandleFunc("/fib", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(strconv.Itoa(fibSink)))
})
go func() {
for {
time.Sleep(100 * time.Millisecond)
log.Print("HIII STILL ALIVE")
}
}()
serve(*port, *soReuseport)
}
func fib(n int) int {
if n < 2 {
return 1
}
return fib(n-1) + fib(n-2)
}