timeout

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package timeout provides an HTTP middleware that enforces a per-request timeout on downstream handler execution. When the timeout elapses, the context is cancelled and a 503 Service Unavailable response is returned.

Usage:

srv.Use(timeout.Middleware(30 * time.Second))
// or with a custom timeout response:
srv.Use(timeout.Middleware(30 * time.Second,
    timeout.WithStatus(http.StatusGatewayTimeout),
))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(defaultTimeout time.Duration, opts ...Option) httpPlugin.Middleware

Middleware returns a httpPlugin.Middleware that enforces a timeout on downstream handler execution. If the handler does not complete within the timeout, the context is cancelled and a timeout error response is sent.

Types

type Option

type Option func(*options)

Option configures the timeout middleware.

func WithMessage

func WithMessage(msg string) Option

WithMessage sets the response body text returned when the timeout fires. Default: "Service Unavailable".

func WithSkipFunc

func WithSkipFunc(fn func(r *http.Request) bool) Option

WithSkipFunc sets a function that returns true for requests that should bypass the timeout (e.g. long-polling or streaming endpoints).

func WithStatus

func WithStatus(code int) Option

WithStatus sets the HTTP status code returned when the timeout fires. Default: 503 Service Unavailable.

func WithTimeoutFunc

func WithTimeoutFunc(fn func(r *http.Request) time.Duration) Option

WithTimeoutFunc sets a function that returns a per-request timeout, overriding the static default timeout. This allows different timeouts for different routes.

Jump to

Keyboard shortcuts

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