-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
171 lines (144 loc) · 3.52 KB
/
app.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package winter
import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"net/http"
"net/http/pprof"
"strings"
"sync/atomic"
)
// HandlerFunc handler func with [Context] as argument
type HandlerFunc func(c Context)
// App the main interface of [summer]
type App interface {
// Handler inherit [http.Handler]
http.Handler
// Registry inherit [Registry]
Registry
// HandleFunc register an action function with given path pattern
//
// This function is similar with [http.ServeMux.HandleFunc]
HandleFunc(pattern string, fn HandlerFunc)
}
type app struct {
Registry
opts options
hMain *http.ServeMux
hProm http.Handler
hProf http.Handler
cc chan struct{}
failed int64
}
func (a *app) HandleFunc(pattern string, fn HandlerFunc) {
a.hMain.Handle(
pattern,
otelhttp.NewHandler(
otelhttp.WithRouteTag(
pattern,
http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
c := newContext(rw, req)
c.responseLogging = a.opts.responseLogging
func() {
defer c.Perform()
a.Wrap(fn)(c)
}()
}),
),
pattern,
),
)
}
func (a *app) serveReadiness(rw http.ResponseWriter, req *http.Request) {
c := newContext(rw, req)
defer c.Perform()
a.Wrap(func(c Context) {
cr := &checkResult{}
a.Check(c, cr.Collect)
s, failed := cr.Result()
status := http.StatusOK
if failed {
atomic.AddInt64(&a.failed, 1)
status = http.StatusInternalServerError
} else {
atomic.StoreInt64(&a.failed, 0)
}
c.Code(status)
c.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
c.Text(s)
})(c)
}
func (a *app) serveLiveness(rw http.ResponseWriter, req *http.Request) {
c := newContext(rw, req)
defer c.Perform()
c.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
if a.opts.readinessCascade > 0 &&
atomic.LoadInt64(&a.failed) > a.opts.readinessCascade {
c.Code(http.StatusInternalServerError)
c.Text("CASCADED")
} else {
c.Code(http.StatusOK)
c.Text("OK")
}
}
func (a *app) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// alive, ready, metrics
if req.URL.Path == a.opts.readinessPath {
// support readinessPath == livenessPath
a.serveReadiness(rw, req)
return
} else if req.URL.Path == a.opts.livenessPath {
a.serveLiveness(rw, req)
return
} else if req.URL.Path == a.opts.metricsPath {
a.hProm.ServeHTTP(rw, req)
return
}
// pprof
if strings.HasPrefix(req.URL.Path, "/debug/") {
a.hProf.ServeHTTP(rw, req)
return
}
// concurrency
if a.cc != nil {
<-a.cc
defer func() {
a.cc <- struct{}{}
}()
}
a.hMain.ServeHTTP(rw, req)
}
// New create an [App] with [Option]
func New(opts ...Option) App {
a := &app{
opts: options{
concurrency: 128,
readinessCascade: 5,
readinessPath: DefaultReadinessPath,
livenessPath: DefaultLivenessPath,
metricsPath: DefaultMetricsPath,
},
}
for _, opt := range opts {
opt(&a.opts)
}
a.Registry = NewRegistry()
{
a.hMain = &http.ServeMux{}
a.hProm = promhttp.Handler()
m := &http.ServeMux{}
m.HandleFunc("/debug/pprof/", pprof.Index)
m.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
m.HandleFunc("/debug/pprof/profile", pprof.Profile)
m.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
m.HandleFunc("/debug/pprof/trace", pprof.Trace)
a.hProf = m
}
// create concurrency controller
if a.opts.concurrency > 0 {
a.cc = make(chan struct{}, a.opts.concurrency)
for i := 0; i < a.opts.concurrency; i++ {
a.cc <- struct{}{}
}
}
return a
}