Home / Function/ handleTimeout() — fiber Function Reference

handleTimeout() — fiber Function Reference

Architecture documentation for the handleTimeout() function in timeout.go from the fiber codebase.

Function go FiberCore Adapters calls 1 called by 1

Entity Profile

Dependency Diagram

graph TD
  4b8d9c60_fb0a_7187_d55e_7c36f415f74e["handleTimeout()"]
  d63e7e88_9c3e_b1e3_d15b_8b9f56cef6e5["timeout.go"]
  4b8d9c60_fb0a_7187_d55e_7c36f415f74e -->|defined in| d63e7e88_9c3e_b1e3_d15b_8b9f56cef6e5
  9bba4d56_edea_19ff_cd90_d760229c6404["New()"]
  9bba4d56_edea_19ff_cd90_d760229c6404 -->|calls| 4b8d9c60_fb0a_7187_d55e_7c36f415f74e
  75b6f1d6_3fad_ff35_a2de_cbdc4fc8bc86["invokeOnTimeout()"]
  4b8d9c60_fb0a_7187_d55e_7c36f415f74e -->|calls| 75b6f1d6_3fad_ff35_a2de_cbdc4fc8bc86
  style 4b8d9c60_fb0a_7187_d55e_7c36f415f74e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/timeout/timeout.go lines 95–155

func handleTimeout(
	parent context.Context,
	ctx fiber.Ctx,
	cancel context.CancelFunc,
	done <-chan error,
	panicChan <-chan any,
	cfg Config,
) error {
	// Mark fiber context as abandoned - ReleaseCtx will skip pooling.
	// The context will NOT be returned to the pool. This is an intentional
	// trade-off: we accept the small memory cost of not recycling timed-out
	// contexts in exchange for complete race-freedom.
	//
	// This is the same approach fasthttp uses - timed-out RequestCtx objects
	// are never returned to the pool (see fasthttp's releaseCtx which panics
	// if timeoutResponse is set).
	ctx.Abandon()

	// Prepare the timeout response before marking the RequestCtx as timed out so
	// custom OnTimeout handlers can shape the response body.
	timeoutErr := invokeOnTimeout(ctx, cfg)

	// If no OnTimeout handler is configured or the response is still the default
	// 200/empty, ensure a sensible timeout response is captured for fasthttp to send.
	if cfg.OnTimeout == nil || (ctx.Response().StatusCode() == fiber.StatusOK && len(ctx.Response().Body()) == 0) {
		ctx.Response().SetStatusCode(fiber.StatusRequestTimeout)
		if len(ctx.Response().Body()) == 0 {
			ctx.Response().SetBodyString(fiber.ErrRequestTimeout.Message)
		}
	}

	// Tell fasthttp to not recycle the RequestCtx - it will acquire a new one
	// for the response and send the captured payload (either default or from
	// OnTimeout). All ctx mutations after this call are ignored by fasthttp.
	ctx.RequestCtx().TimeoutErrorWithResponse(&ctx.RequestCtx().Response)

	// Spawn cleanup goroutine that waits for handler to finish.
	// This only does context cleanup (cancel + restore parent), NOT ctx release.
	// The fiber.Ctx is intentionally NOT released to avoid races with requestHandler
	// which may still access ctx (e.g., ErrorHandler) after this function returns.
	// ForceRelease cannot be called safely here for the same reason.
	go func() {
		select {
		case <-done:
		case <-panicChan:
		}
		// Handler finished - cancel timeout context and restore parent
		cancel()
		ctx.SetContext(parent)

		// TODO: Currently the ctx is not returned to the pool (memory leak for timed-out requests).
		// Future improvement: Implement a concurrent "garbage collector" list where abandoned
		// contexts are queued after both the handler AND requestHandler are done. A background
		// goroutine would periodically process this list and call ForceRelease() to recycle
		// the contexts safely. This would require tracking when requestHandler finishes
		// (e.g., via a channel signaled in ReleaseCtx) without adding per-request overhead
		// for non-timeout cases.
	}()

	return timeoutErr
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does handleTimeout() do?
handleTimeout() is a function in the fiber codebase, defined in middleware/timeout/timeout.go.
Where is handleTimeout() defined?
handleTimeout() is defined in middleware/timeout/timeout.go at line 95.
What does handleTimeout() call?
handleTimeout() calls 1 function(s): invokeOnTimeout.
What calls handleTimeout()?
handleTimeout() is called by 1 function(s): New.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free