Home / File/ timeout.go — fiber Source File

timeout.go — fiber Source File

Architecture documentation for timeout.go, a go file in the fiber codebase. 1 imports, 0 dependents.

File go FiberCore Adapters 1 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  d63e7e88_9c3e_b1e3_d15b_8b9f56cef6e5["timeout.go"]
  cc7104af_aece_1fe5_3985_791c7f34910c["context"]
  d63e7e88_9c3e_b1e3_d15b_8b9f56cef6e5 --> cc7104af_aece_1fe5_3985_791c7f34910c
  style d63e7e88_9c3e_b1e3_d15b_8b9f56cef6e5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

package timeout

import (
	"context"
	"errors"
	"runtime/debug"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/log"
)

// New enforces a timeout for each incoming request. It replaces the request's
// context with one that has the configured deadline, which is exposed through
// c.Context(). Handlers can detect the timeout by listening on c.Context().Done()
// and return early.
//
// When a timeout occurs, the middleware returns immediately with fiber.ErrRequestTimeout
// (or the result of OnTimeout if configured). The handler goroutine can continue
// safely, and resources are recycled when it finishes via the Abandon/ForceRelease
// mechanism.
func New(h fiber.Handler, config ...Config) fiber.Handler {
	cfg := configDefault(config...)

	return func(ctx fiber.Ctx) error {
		if cfg.Next != nil && cfg.Next(ctx) {
			return h(ctx)
		}

		timeout := cfg.Timeout
		if timeout <= 0 {
			return h(ctx)
		}

		// Create timeout context - handler can check c.Context().Done()
		parent := ctx.Context()
		tCtx, cancel := context.WithTimeout(parent, timeout)
		ctx.SetContext(tCtx)

		// Channels for handler result and panics
		done := make(chan error, 1)
		panicChan := make(chan any, 1)

		// Run handler in goroutine so we can race against the timeout
		go func() {
			defer func() {
				if p := recover(); p != nil {
					log.Errorw("panic recovered in timeout handler", "panic", p, "stack", string(debug.Stack()))
					select {
					case panicChan <- p:
					default:
						// Middleware already returned, panic value discarded
					}
				}
			}()
			err := h(ctx)
			select {
			case done <- err:
			default:
				// Middleware already returned, error discarded
			}
// ... (120 more lines)

Domain

Subdomains

Dependencies

  • context

Frequently Asked Questions

What does timeout.go do?
timeout.go is a source file in the fiber codebase, written in go. It belongs to the FiberCore domain, Adapters subdomain.
What functions are defined in timeout.go?
timeout.go defines 5 function(s): New, handleResult, handleTimeout, invokeOnTimeout, isTimeoutError.
What does timeout.go depend on?
timeout.go imports 1 module(s): context.
Where is timeout.go in the architecture?
timeout.go is located at middleware/timeout/timeout.go (domain: FiberCore, subdomain: Adapters, directory: middleware/timeout).

Analyze Your Own Codebase

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

Try Supermodel Free