Skip to content

Commit 3bff930

Browse files
swordqiuQiu Jian
andauthored
fix: handler support worker manager callback (#22404)
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
1 parent d302eb2 commit 3bff930

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

pkg/appsrv/appsrv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func (app *Application) defaultHandle(w http.ResponseWriter, r *http.Request, ri
393393
fw: newResponseWriterChannel(w),
394394
r: r,
395395
segs: segs,
396-
to: hand.FetchProcessTimeout(r),
396+
to: hand.fetchProcessTimeout(r),
397397
cancel: nil,
398398
}
399399

@@ -408,7 +408,7 @@ func (app *Application) defaultHandle(w http.ResponseWriter, r *http.Request, ri
408408
defer task.cancel()
409409
}
410410
task.ctx = appctx.WithRequestLang(task.ctx, r)
411-
session := hand.workerMan
411+
session := hand.fetchWorkerManager(r)
412412
if session == nil {
413413
if r.Method == "GET" || r.Method == "HEAD" {
414414
session = app.readSession

pkg/appsrv/handlerinfo.go

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type handlerRequestCounter struct {
2828

2929
type TProcessTimeoutCallback func(*SHandlerInfo, *http.Request) time.Duration
3030

31+
type TWorkerManagerCallback func(*SHandlerInfo, *http.Request) *SWorkerManager
32+
3133
type SHandlerInfo struct {
3234
method string
3335
path []string
@@ -38,27 +40,66 @@ type SHandlerInfo struct {
3840
counter2XX handlerRequestCounter
3941
counter4XX handlerRequestCounter
4042
counter5XX handlerRequestCounter
41-
workerMan *SWorkerManager
42-
skipLog bool
43+
44+
skipLog bool
4345

4446
processTimeout time.Duration
4547
processTimeoutCallback TProcessTimeoutCallback
48+
49+
workerManager *SWorkerManager
50+
workerManagerCallback TWorkerManagerCallback
4651
}
4752

48-
func (this *SHandlerInfo) FetchProcessTimeout(r *http.Request) time.Duration {
49-
if this.processTimeoutCallback != nil {
50-
tm := this.processTimeoutCallback(this, r)
51-
if tm < this.processTimeout {
52-
tm = this.processTimeout
53+
func (hi *SHandlerInfo) fetchProcessTimeout(r *http.Request) time.Duration {
54+
if hi.processTimeoutCallback != nil {
55+
tm := hi.processTimeoutCallback(hi, r)
56+
if tm < hi.processTimeout {
57+
tm = hi.processTimeout
5358
}
5459
return tm
5560
} else {
56-
return this.processTimeout
61+
return hi.processTimeout
5762
}
5863
}
5964

60-
func (this *SHandlerInfo) SetProcessTimeoutCallback(callback TProcessTimeoutCallback) {
61-
this.processTimeoutCallback = callback
65+
func (hi *SHandlerInfo) SetProcessTimeoutCallback(callback TProcessTimeoutCallback) *SHandlerInfo {
66+
hi.processTimeoutCallback = callback
67+
return hi
68+
}
69+
70+
func (hi *SHandlerInfo) SetProcessTimeout(to time.Duration) *SHandlerInfo {
71+
hi.processTimeout = to
72+
return hi
73+
}
74+
75+
func (hi *SHandlerInfo) SetProcessNoTimeout() *SHandlerInfo {
76+
hi.processTimeout = -1
77+
return hi
78+
}
79+
80+
func (hi *SHandlerInfo) fetchWorkerManager(r *http.Request) *SWorkerManager {
81+
if hi.workerManagerCallback != nil {
82+
wm := hi.workerManagerCallback(hi, r)
83+
if wm != nil {
84+
return wm
85+
}
86+
}
87+
return hi.workerManager
88+
}
89+
90+
func (hi *SHandlerInfo) SetWorkerManagerCallback(callback TWorkerManagerCallback) *SHandlerInfo {
91+
hi.workerManagerCallback = callback
92+
return hi
93+
}
94+
95+
func (hi *SHandlerInfo) SetWorkerManager(workerMan *SWorkerManager) *SHandlerInfo {
96+
hi.workerManager = workerMan
97+
return hi
98+
}
99+
100+
func (hi *SHandlerInfo) ClearWorkerManager() *SHandlerInfo {
101+
hi.workerManager = nil
102+
return hi
62103
}
63104

64105
func (this *SHandlerInfo) GetName(params map[string]string) string {
@@ -126,21 +167,6 @@ func (hi *SHandlerInfo) SetTags(tags map[string]string) *SHandlerInfo {
126167
return hi
127168
}
128169

129-
func (hi *SHandlerInfo) SetProcessTimeout(to time.Duration) *SHandlerInfo {
130-
hi.processTimeout = to
131-
return hi
132-
}
133-
134-
func (hi *SHandlerInfo) SetProcessNoTimeout() *SHandlerInfo {
135-
hi.processTimeout = -1
136-
return hi
137-
}
138-
139-
func (hi *SHandlerInfo) SetWorkerManager(workerMan *SWorkerManager) *SHandlerInfo {
140-
hi.workerMan = workerMan
141-
return hi
142-
}
143-
144170
func (hi *SHandlerInfo) SetSkipLog(skip bool) *SHandlerInfo {
145171
hi.skipLog = skip
146172
return hi

0 commit comments

Comments
 (0)