Documentation ¶
Index ¶
Constants ¶
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 ¶
WithLogger adds a logger instance to the context. The logger can then be retrieved from the context for structured logging with trace information.
Types ¶
This section is empty.