Skip to content

Commit

Permalink
http: add Engine.OnRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Aug 26, 2024
1 parent a7eb6fb commit 428f1a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions nbhttp/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ type Config struct {
// Handler sets HTTP handler for Engine.
Handler http.Handler

// `OnRequest` sets HTTP handler which will be called before `Handler.ServeHTTP`.
//
// A `Request` is pushed into a task queue and waits to be executed by the goroutine pool by default, which means the `Request`
// may not be executed at once and may wait for long to be executed: if the client-side supports `pipeline` and the previous
// `Requests` are handled for long. In some scenarios, we need to know when the `Request` is received, then we can control and
// customize whether we should drop the task or record the real processing time for the `Request`. That is what this func should
// be used for.
OnRequest http.HandlerFunc

// ServerExecutor sets the executor for data reading callbacks.
ServerExecutor func(f func())

Expand Down
4 changes: 4 additions & 0 deletions nbhttp/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func (p *ServerProcessor) OnComplete(parser *Parser) {
}

response := NewResponse(parser, request)

if engine.OnRequest != nil {
engine.OnRequest(response, request)
}
if !parser.Execute(func() {
engine.Handler.ServeHTTP(response, request)
p.flushResponse(parser, response)
Expand Down

0 comments on commit 428f1a7

Please sign in to comment.