-
Notifications
You must be signed in to change notification settings - Fork 0
/
gogc.go
49 lines (40 loc) · 1000 Bytes
/
gogc.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
package main
import (
"encoding/json"
"mime"
"net/http"
"runtime/debug"
)
func SetGCPercent(w http.ResponseWriter, req *http.Request) {
type GC struct {
GCPercent int `json:"gcpercent"`
}
contentType := req.Header.Get("Content-Type")
mediatype, _, err := mime.ParseMediaType(contentType)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if mediatype != "application/json" {
http.Error(w, "expect application/json Content-Type", http.StatusUnsupportedMediaType)
return
}
dec := json.NewDecoder(req.Body)
dec.UseNumber()
dec.DisallowUnknownFields()
var rt GC
if err := dec.Decode(&rt); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
oldGC := debug.SetGCPercent(rt.GCPercent)
type response struct {
GCPercent int `json:"new_gogc"`
PreviousGCPercent int `json:"previous_gogc"`
}
res := response{
GCPercent: rt.GCPercent,
PreviousGCPercent: oldGC,
}
json.NewEncoder(w).Encode(res)
}