middlewares

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const HeaderTraceID = "X-Trace-ID"

HeaderTraceID defines the HTTP header used for providing or propagating a trace ID, enabling consistent tracking of requests across services.

Variables

This section is empty.

Functions

func NewRateLimiter

func NewRateLimiter(config *config.SlotConfig, redisClient *libredis.Client) gin.HandlerFunc

NewRateLimiter sets up and returns a Gin middleware for rate limiting requests. The rate limiter uses Redis as a store and applies limits based on the provided configuration.

Parameters:

  • config (*config.SlotConfig): Configuration structure containing rate limit settings.
  • redisClient (*libredis.Client): Redis client instance used as the backend for the rate limiter.

Returns:

  • (gin.HandlerFunc): Gin middleware handler function to enforce rate limiting.

Usage:

Add this middleware to your Gin router to restrict the rate of requests per client IP.
Example rate format: "5-S" (5 requests per second), "100-M" (100 requests per minute).

Example:

router := gin.Default()
rateLimiter := NewRateLimiter(slotConfig, redisClient)
router.Use(rateLimiter)

func TraceMiddleware

func TraceMiddleware() gin.HandlerFunc

TraceMiddleware is a middleware that attaches a trace ID and logger to both gin.Context and context.Context. If a trace ID is present in the request header, it is used; otherwise, a new trace ID is generated. The trace ID and logger are then added to the request context for consistent logging across services.

func WithLogger

func WithLogger(ctx context.Context, logger log.Logger) context.Context

WithLogger adds a logger instance to the context. The logger can then be retrieved from the context for structured logging with trace information.

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID adds the trace ID to the context for request tracing purposes. This trace ID helps uniquely identify and track a request across services.

Types

This section is empty.

Jump to

Keyboard shortcuts

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