timeout

package
v3.1.32 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package timeout 提供请求超时控制中间件。

使用方式:

app.Use(timeout.New(timeout.Config{Timeout: 30 * time.Second}))

超时后,请求 context 被取消,handler 逻辑通过 c.Context().Done() 感知超时。 零开销 — context.WithTimeout 仅在超时时触发 cancel。

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	Timeout:    30 * time.Second,
	StatusCode: http.StatusServiceUnavailable,
	Message:    "request timeout",
}

Functions

func New

func New(conf ...Config) core.HandlerFunc

New 返回请求超时中间件。

Types

type Config

type Config struct {
	// Timeout 请求超时时间。0 表示不设置超时。
	Timeout time.Duration
	// StatusCode 超时后返回的 HTTP 状态码,默认 503
	StatusCode int
	// Message 超时后返回的消息
	Message string
	// Skip 可选跳过函数
	Skip func(c core.Ctx) bool
}

Config 超时配置

Jump to

Keyboard shortcuts

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