middleware

package
v0.0.0-...-9481380 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const StatusClientClosedRequest = 499

StatusClientClosedRequest is the de-facto status code (used by nginx) for a client that closed the connection before the server could send a response. It is not an official IANA code but is widely recognised in logs and metrics.

Variables

View Source
var ErrRequestTimeout = errors.New("request timeout exceeded")

ErrRequestTimeout is the cancel cause set when the per-request timeout fires. Callers can distinguish this from a client disconnection by checking:

errors.Is(CancelCause(c), middleware.ErrRequestTimeout)

Functions

func CancelCause

func CancelCause(c *gin.Context) error

CancelCause returns the cancel cause captured by the timeout middleware. It returns nil for normal (non-canceled/non-timed-out) requests.

func ExcludeRoutes

func ExcludeRoutes(middleware gin.HandlerFunc, notlogged ...string) gin.HandlerFunc

func IncludeRoutes

func IncludeRoutes(middleware gin.HandlerFunc, included ...string) gin.HandlerFunc

func LoggingMiddleware

func LoggingMiddleware(logger logger.Logger, conf Config) gin.HandlerFunc

func RequestTimeout

func RequestTimeout(timeout time.Duration) gin.HandlerFunc

RequestTimeout returns a Gin middleware that sets a context deadline on each request. This is needed because http.Server.WriteTimeout does NOT cancel r.Context() (see https://github.com/golang/go/issues/59602), so without an explicit deadline, blocking calls like pgxpool.Acquire will wait indefinitely when the connection pool is saturated.

After the handler returns, the middleware checks context.Cause to distinguish two cancellation scenarios and sets an appropriate status if nothing was written yet:

  • server-side timeout (cause is ErrRequestTimeout) → 408 Request Timeout
  • client disconnect (ctx.Err() == context.Canceled) → 499 Client Closed Request

Routes matching any of the excludedRoutes patterns are skipped (useful for health checks and long-polling endpoints).

Types

type Config

type Config struct {
	TimeFormat      string
	UTC             bool
	SkipPaths       []string
	SkipPathRegexps []*regexp.Regexp
	Context         Fn
	DefaultLevel    zapcore.Level
	// Skipper indicates which logs should not be written. Optional.
	Skipper Skipper
}

type Fn

type Fn func(c *gin.Context) []zapcore.Field

Fn is a function to get zap fields from gin.Context

type Skipper

type Skipper func(c *gin.Context) bool

Skipper is a function to skip logs based on provided Context

Directories

Path Synopsis
otel

Jump to

Keyboard shortcuts

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