Internal

internal/requesthold

import "github.com/gofiber/fiber/v3/internal/requesthold"

Package requesthold keeps the fasthttp.RequestCtx of a request out of reuse for as long as the net/http handler adapted for it runs.

fasthttpadaptor builds the *http.Request out of views of the buffers of the RequestCtx (method, URL, host, every header key and value, the body) and makes the RequestCtx its context. It runs the handler in a goroutine, and once the handler flushes the response it returns, without waiting for the handler. When the client leaves, the server closes the response stream, takes the RequestCtx back and the next request overwrites its buffers while the handler still runs: its r.Header.Get, r.URL.Path, r.Host, r.Method, the strings it read earlier and r.Context().Value then answer for another request. A net/http server never does that to a handler. The adaptor is in fasthttp and cannot be changed from here.

The handler returned by NewFastHTTPHandler registers a hold in the user values of the RequestCtx. fasthttp closes the user values of a request when it resets it for the next one, which happens after the response was written and before the buffers are reused, and the hold's Close waits for the handler. A handler that returns before the adaptor does, which is nearly all of them, leaves nothing to wait for.

The hold covers the call of the handler. A goroutine the handler leaves running after it returned, such as the one http.TimeoutHandler runs the wrapped handler in once it has answered 503, is not waited for: it reads a RequestCtx that may already serve the next request, as it did before the hold.

Functions

func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler

Source: internal/requesthold/requesthold.go:82

NewFastHTTPHandler is fasthttpadaptor.NewFastHTTPHandler(h) that does not let the RequestCtx be reused while h still runs, and hands h the lines of a repeated header in the order they were received.

func Pending(ctx *fasthttp.RequestCtx) bool

Source: internal/requesthold/requesthold.go:131

Pending reports whether the net/http handler of the request on ctx still runs. A caller that owns ctx and would reuse it must leave it to the handler then: the next Reset waits for the handler.

func Rebind(ctx *fasthttp.RequestCtx, resp *fasthttp.Response)

Source: internal/requesthold/requesthold.go:123

Rebind points the hold on ctx, if any, at resp, the response that took over its body stream, so that the stream is closed by resp alone.