reqctx

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package reqctx provides request context utilities for timeout management, cancellation, and request ID tracking throughout the application.

Package reqctx provides request context utilities. This file contains HTTP middleware for context propagation.

Index

Constants

View Source
const RequestIDHeader = "X-Request-ID"

RequestIDHeader is the HTTP header used to propagate request IDs.

Variables

This section is empty.

Functions

func CheckContext

func CheckContext(ctx context.Context) error

CheckContext checks if context is still valid, returns error if canceled/timeout

func ContextError

func ContextError(ctx context.Context) error

ContextError returns a descriptive error based on context state

func EnrichContext

func EnrichContext(ctx context.Context, r *http.Request) context.Context

EnrichContext enriches a context with request metadata from an HTTP request. If the incoming request carries an X-Request-ID header the value is reused; otherwise a new UUID is generated.

func GetElapsedTime

func GetElapsedTime(ctx context.Context) time.Duration

GetElapsedTime returns the elapsed time since the request started

func GetRemoteAddr

func GetRemoteAddr(ctx context.Context) string

GetRemoteAddr retrieves the remote address from context

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID retrieves the request ID from context

func GetStartTime

func GetStartTime(ctx context.Context) time.Time

GetStartTime retrieves the request start time from context

func GetUserAgent

func GetUserAgent(ctx context.Context) string

GetUserAgent retrieves the user agent from context

func IsContextCanceled

func IsContextCanceled(ctx context.Context) bool

IsContextCanceled returns true if the context has been canceled

func Middleware

func Middleware() func(http.Handler) http.Handler

Middleware returns an HTTP middleware that enriches the request context with request metadata and handles client disconnection gracefully.

func SetGlobalConfig

func SetGlobalConfig(cfg *TimeoutConfig)

SetGlobalConfig sets the global timeout configuration

func TimeoutMiddleware

func TimeoutMiddleware(next http.Handler) http.Handler

TimeoutMiddleware returns an HTTP middleware that applies a timeout to the request context

func WithCustomTimeout

func WithCustomTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

WithCustomTimeout returns a context with a custom timeout

func WithDatabaseTimeout

func WithDatabaseTimeout(ctx context.Context) (context.Context, context.CancelFunc)

WithDatabaseTimeout returns a context with the database timeout applied

func WithDefaultTimeout

func WithDefaultTimeout(ctx context.Context) (context.Context, context.CancelFunc)

WithDefaultTimeout returns a context with the default timeout applied

func WithDownloadTimeout

func WithDownloadTimeout(ctx context.Context) (context.Context, context.CancelFunc)

WithDownloadTimeout returns a context with the download timeout applied

func WithExistingRequestID

func WithExistingRequestID(ctx context.Context, id string) context.Context

WithExistingRequestID adds an existing request ID to the context. If the provided id is empty, a new UUID is generated.

func WithRemoteAddr

func WithRemoteAddr(ctx context.Context, addr string) context.Context

WithRemoteAddr adds the remote address to the context

func WithRequestID

func WithRequestID(ctx context.Context) context.Context

WithRequestID adds a new generated request ID to the context

func WithStartTime

func WithStartTime(ctx context.Context) context.Context

WithStartTime adds the request start time to the context

func WithStorageTimeout

func WithStorageTimeout(ctx context.Context) (context.Context, context.CancelFunc)

WithStorageTimeout returns a context with the storage timeout applied

func WithUploadTimeout

func WithUploadTimeout(ctx context.Context) (context.Context, context.CancelFunc)

WithUploadTimeout returns a context with the upload timeout applied

func WithUserAgent

func WithUserAgent(ctx context.Context, ua string) context.Context

WithUserAgent adds the user agent to the context

Types

type ContextKey

type ContextKey string

ContextKey is a type for context keys to avoid collisions

const (
	// RequestIDKey is the context key for request ID
	RequestIDKey ContextKey = "request_id"
	// StartTimeKey is the context key for request start time
	StartTimeKey ContextKey = "start_time"
	// UserAgentKey is the context key for user agent
	UserAgentKey ContextKey = "user_agent"
	// RemoteAddrKey is the context key for remote address
	RemoteAddrKey ContextKey = "remote_addr"
	// AccessKeyIDKey is the context key for the authenticated API access key ID
	AccessKeyIDKey ContextKey = "access_key_id"
)

type TimeoutConfig

type TimeoutConfig struct {
	// UploadTimeout is the maximum duration for upload operations
	UploadTimeout time.Duration
	// DownloadTimeout is the maximum duration for download operations
	DownloadTimeout time.Duration
	// DefaultTimeout is the default timeout for general operations
	DefaultTimeout time.Duration
	// DatabaseTimeout is the timeout for database operations
	DatabaseTimeout time.Duration
	// StorageTimeout is the timeout for storage operations
	StorageTimeout time.Duration
}

TimeoutConfig holds configurable timeout durations

func DefaultTimeoutConfig

func DefaultTimeoutConfig() *TimeoutConfig

DefaultTimeoutConfig returns the default timeout configuration

func GetGlobalConfig

func GetGlobalConfig() *TimeoutConfig

GetGlobalConfig returns the current global timeout configuration

Jump to

Keyboard shortcuts

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