/
adapter.go
72 lines (63 loc) · 2.33 KB
/
adapter.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
package utils
import (
"github.com/GoCollaborate/src/artifacts/stats"
"github.com/GoCollaborate/src/constants"
"github.com/gorilla/mux"
"golang.org/x/time/rate"
"io"
"net/http"
"net/http/pprof"
)
func AdaptRouterToDebugMode(router *mux.Router) *mux.Router {
router.HandleFunc("/debug/pprof/", pprof.Index).Name("Debug Index")
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline).Name("Debug Commandline")
router.HandleFunc("/debug/pprof/profile", pprof.Profile).Name("Debug Profiling")
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol).Name("Debug Symbol")
router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handle("/debug/pprof/heap", pprof.Handler("heap"))
router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
router.Handle("/debug/pprof/block", pprof.Handler("block"))
return router
}
// make sure AdaptHTTPWithStatus is lastly added to the response writer, otherwise
// it would not work (ref: https://golang.org/pkg/net/http/#ResponseWriter)
func AdaptHTTPWithStatus(w http.ResponseWriter,
header int) http.ResponseWriter {
w.WriteHeader(header)
return w
}
func AdaptHTTPWithHeader(w http.ResponseWriter,
header constants.Header) http.ResponseWriter {
w.Header().Add(header.Key, header.Value)
return w
}
// AdaptLimiter adapts the HTTP handler f to a limited handler that allows events up to rate lr and permits bursts of at most lb tokens.
func AdaptLimiter(lim *rate.Limiter, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if !lim.Allow() {
AdaptHTTPWithStatus(w, http.StatusTooManyRequests)
io.WriteString(w, "Job request exceeds the maximum handling limit, please try later.\n")
return
}
f(w, r)
return
}
}
func AdaptStatsHits(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
sm := stats.GetStatsInstance()
sm.Record("hits", 1)
f(w, r)
return
}
}
func AdaptStatsRouteHits(route string, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
sm := stats.GetStatsInstance()
sm.Observe(route)
return func(w http.ResponseWriter, r *http.Request) {
sm := stats.GetStatsInstance()
sm.Record(route, 1)
f(w, r)
return
}
}