Help us shape the future of Go! Take the 2026 Go Developer Survey

timeout

package
v3.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 7 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Next:      nil,
	Timeout:   0,
	OnTimeout: nil,
	Errors:    nil,
}

ConfigDefault is the default configuration.

Functions

func New

func New(h fiber.Handler, config ...Config) fiber.Handler

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.ErrServiceUnavailable (or the result of OnTimeout if configured). The status is 503, not 408: a slow handler is a server-side delay, and 408 (RFC 9110 §15.5.9) invites the client to resend a request whose handler may still be running. The handler goroutine can continue safely, and resources are recycled when it finishes via the Abandon/ForceRelease mechanism.

Types

type Config

type Config struct {
	// Next defines a function to skip this middleware.
	// Optional. Default: nil
	Next func(c fiber.Ctx) bool

	// OnTimeout is executed when a timeout occurs. It may write the response or
	// return a *fiber.Error that shapes it; otherwise the default 503 is sent.
	// The timed-out handler may still be running and must stop using the context.
	// Optional. Default: nil (return fiber.ErrServiceUnavailable)
	OnTimeout fiber.Handler

	// Errors defines custom errors that are treated as timeouts.
	// Optional. Default: nil
	Errors []error

	// Timeout defines the timeout duration for all routes.
	// Optional. Default: 0 (no timeout)
	Timeout time.Duration
}

Config holds the configuration for the timeout middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL