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
- func CheckContext(ctx context.Context) error
- func ContextError(ctx context.Context) error
- func EnrichContext(ctx context.Context, r *http.Request) context.Context
- func GetElapsedTime(ctx context.Context) time.Duration
- func GetRemoteAddr(ctx context.Context) string
- func GetRequestID(ctx context.Context) string
- func GetStartTime(ctx context.Context) time.Time
- func GetUserAgent(ctx context.Context) string
- func IsContextCanceled(ctx context.Context) bool
- func Middleware() func(http.Handler) http.Handler
- func SetGlobalConfig(cfg *TimeoutConfig)
- func TimeoutMiddleware(next http.Handler) http.Handler
- func WithCustomTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
- func WithDatabaseTimeout(ctx context.Context) (context.Context, context.CancelFunc)
- func WithDefaultTimeout(ctx context.Context) (context.Context, context.CancelFunc)
- func WithDownloadTimeout(ctx context.Context) (context.Context, context.CancelFunc)
- func WithExistingRequestID(ctx context.Context, id string) context.Context
- func WithRemoteAddr(ctx context.Context, addr string) context.Context
- func WithRequestID(ctx context.Context) context.Context
- func WithStartTime(ctx context.Context) context.Context
- func WithStorageTimeout(ctx context.Context) (context.Context, context.CancelFunc)
- func WithUploadTimeout(ctx context.Context) (context.Context, context.CancelFunc)
- func WithUserAgent(ctx context.Context, ua string) context.Context
- type ContextKey
- type TimeoutConfig
Constants ¶
const RequestIDHeader = "X-Request-ID"
RequestIDHeader is the HTTP header used to propagate request IDs.
Variables ¶
This section is empty.
Functions ¶
func CheckContext ¶
CheckContext checks if context is still valid, returns error if canceled/timeout
func ContextError ¶
ContextError returns a descriptive error based on context state
func EnrichContext ¶
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 ¶
GetElapsedTime returns the elapsed time since the request started
func GetRemoteAddr ¶
GetRemoteAddr retrieves the remote address from context
func GetRequestID ¶
GetRequestID retrieves the request ID from context
func GetStartTime ¶
GetStartTime retrieves the request start time from context
func GetUserAgent ¶
GetUserAgent retrieves the user agent from context
func IsContextCanceled ¶
IsContextCanceled returns true if the context has been canceled
func Middleware ¶
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 ¶
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 ¶
WithDatabaseTimeout returns a context with the database timeout applied
func WithDefaultTimeout ¶
WithDefaultTimeout returns a context with the default timeout applied
func WithDownloadTimeout ¶
WithDownloadTimeout returns a context with the download timeout applied
func WithExistingRequestID ¶
WithExistingRequestID adds an existing request ID to the context. If the provided id is empty, a new UUID is generated.
func WithRemoteAddr ¶
WithRemoteAddr adds the remote address to the context
func WithRequestID ¶
WithRequestID adds a new generated request ID to the context
func WithStartTime ¶
WithStartTime adds the request start time to the context
func WithStorageTimeout ¶
WithStorageTimeout returns a context with the storage timeout applied
func WithUploadTimeout ¶
WithUploadTimeout returns a context with the upload timeout applied
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" )
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