Skip to content

Commit b15d01b

Browse files
authored
Prepare for support performance trace (#33286)
For #32973
1 parent 6659a38 commit b15d01b

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

modules/web/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func wrapHandlerProvider[T http.Handler](hp func(next http.Handler) T, funcInfo
121121
return func(next http.Handler) http.Handler {
122122
h := hp(next) // this handle could be dynamically generated, so we can't use it for debug info
123123
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
124-
routing.UpdateFuncInfo(req.Context(), funcInfo)
124+
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
125125
h.ServeHTTP(resp, req)
126126
})
127127
}
@@ -157,7 +157,7 @@ func toHandlerProvider(handler any) func(next http.Handler) http.Handler {
157157
return // it's doing pre-check, just return
158158
}
159159

160-
routing.UpdateFuncInfo(req.Context(), funcInfo)
160+
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
161161
ret := fn.Call(argsIn)
162162

163163
// handle the return value (no-op at the moment)

modules/web/routing/context.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ type contextKeyType struct{}
1212

1313
var contextKey contextKeyType
1414

15-
// UpdateFuncInfo updates a context's func info
16-
func UpdateFuncInfo(ctx context.Context, funcInfo *FuncInfo) {
17-
record, ok := ctx.Value(contextKey).(*requestRecord)
18-
if !ok {
19-
return
15+
// RecordFuncInfo records a func info into context
16+
func RecordFuncInfo(ctx context.Context, funcInfo *FuncInfo) (end func()) {
17+
// TODO: reqCtx := reqctx.FromContext(ctx), add trace support
18+
end = func() {}
19+
20+
// save the func info into the context record
21+
if record, ok := ctx.Value(contextKey).(*requestRecord); ok {
22+
record.lock.Lock()
23+
record.funcInfo = funcInfo
24+
record.lock.Unlock()
2025
}
21-
22-
record.lock.Lock()
23-
record.funcInfo = funcInfo
24-
record.lock.Unlock()
26+
return end
2527
}
2628

2729
// MarkLongPolling marks the request is a long-polling request, and the logger may output different message for it

routers/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func NormalRoutes() *web.Router {
213213
}
214214

215215
r.NotFound(func(w http.ResponseWriter, req *http.Request) {
216-
routing.UpdateFuncInfo(req.Context(), routing.GetFuncInfo(http.NotFound, "GlobalNotFound"))
216+
defer routing.RecordFuncInfo(req.Context(), routing.GetFuncInfo(http.NotFound, "GlobalNotFound"))()
217217
http.NotFound(w, req)
218218
})
219219
return r

routers/web/base.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func storageHandler(storageSetting *setting.Storage, prefix string, objStore sto
3434
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
3535
return
3636
}
37-
routing.UpdateFuncInfo(req.Context(), funcInfo)
37+
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
3838

3939
rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
4040
rPath = util.PathJoinRelX(rPath)
@@ -65,7 +65,7 @@ func storageHandler(storageSetting *setting.Storage, prefix string, objStore sto
6565
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
6666
return
6767
}
68-
routing.UpdateFuncInfo(req.Context(), funcInfo)
68+
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
6969

7070
rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
7171
rPath = util.PathJoinRelX(rPath)

routers/web/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ func registerRoutes(m *web.Router) {
16221622

16231623
m.NotFound(func(w http.ResponseWriter, req *http.Request) {
16241624
ctx := context.GetWebContext(req)
1625-
routing.UpdateFuncInfo(ctx, routing.GetFuncInfo(ctx.NotFound, "WebNotFound"))
1625+
defer routing.RecordFuncInfo(ctx, routing.GetFuncInfo(ctx.NotFound, "WebNotFound"))()
16261626
ctx.NotFound("", nil)
16271627
})
16281628
}

services/context/base.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package context
55

66
import (
7-
"context"
87
"fmt"
98
"html/template"
109
"io"
@@ -25,8 +24,7 @@ type BaseContextKeyType struct{}
2524
var BaseContextKey BaseContextKeyType
2625

2726
type Base struct {
28-
context.Context
29-
reqctx.RequestDataStore
27+
reqctx.RequestContext
3028

3129
Resp ResponseWriter
3230
Req *http.Request
@@ -172,19 +170,19 @@ func (b *Base) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
172170
}
173171

174172
func NewBaseContext(resp http.ResponseWriter, req *http.Request) *Base {
175-
ds := reqctx.GetRequestDataStore(req.Context())
173+
reqCtx := reqctx.FromContext(req.Context())
176174
b := &Base{
177-
Context: req.Context(),
178-
RequestDataStore: ds,
179-
Req: req,
180-
Resp: WrapResponseWriter(resp),
181-
Locale: middleware.Locale(resp, req),
182-
Data: ds.GetData(),
175+
RequestContext: reqCtx,
176+
177+
Req: req,
178+
Resp: WrapResponseWriter(resp),
179+
Locale: middleware.Locale(resp, req),
180+
Data: reqCtx.GetData(),
183181
}
184182
b.Req = b.Req.WithContext(b)
185-
ds.SetContextValue(BaseContextKey, b)
186-
ds.SetContextValue(translation.ContextKey, b.Locale)
187-
ds.SetContextValue(httplib.RequestContextKey, b.Req)
183+
reqCtx.SetContextValue(BaseContextKey, b)
184+
reqCtx.SetContextValue(translation.ContextKey, b.Locale)
185+
reqCtx.SetContextValue(httplib.RequestContextKey, b.Req)
188186
return b
189187
}
190188

0 commit comments

Comments
 (0)