-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
53 lines (46 loc) · 1011 Bytes
/
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
package main
import (
"net/http"
"os"
"strconv"
"sync/atomic"
"time"
)
var spinning int32
var niceMicros int64
func main() {
niceMicros = 1000
http.HandleFunc("/spin", func(w http.ResponseWriter, r *http.Request) {
myVal := atomic.AddInt32(&spinning, 1)
go func() {
for {
isSpinning := atomic.LoadInt32(&spinning)
if isSpinning < myVal {
break
}
micros := atomic.LoadInt64(&niceMicros)
time.Sleep(time.Duration(micros) * time.Microsecond)
// spin!
}
}()
})
http.HandleFunc("/unspin", func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&spinning, -1)
})
http.HandleFunc("/nice", func(w http.ResponseWriter, r *http.Request) {
microVals, ok := r.URL.Query()["micros"]
if !ok || len(microVals) < 1 {
return
}
val, err := strconv.Atoi(microVals[0])
if err != nil {
return
}
atomic.StoreInt64(&niceMicros, int64(val))
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.ListenAndServe(":"+port, nil)
}