ratelimiter

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 13 Imported by: 0

README

ratelimiter — Gin rate-limiting middleware

import "github.com/downsized-devs/sdk-go/ratelimiter"

Stability: Stable — see STABILITY.md

Per-path rate limiting middleware for Gin, backed by ulule/limiter's in-memory store.

Features

  • Init returns a GinMiddleware ready to attach.
  • Per-path overrides via ConfigPath.
  • In-memory store (replace with Redis store if you need cross-replica limits — not done here).

Installation

go get github.com/downsized-devs/sdk-go/ratelimiter

Quick Start

rl := ratelimiter.Init(ratelimiter.Config{
    Enabled: true,
    Default: "100-S", // 100 requests per second
    Paths: []ratelimiter.ConfigPath{
        { Path: "/api/login", Rate: "5-M" }, // 5/minute on login
    },
}, log, appcontext, checker)

r := gin.New()
r.Use(rl.Limiter())

API Reference

Symbol Signature
Init func Init(cfg Config, ...deps) Interface
Interface.Limiter () gin.HandlerFunc
Config { Enabled bool; Default string; Paths []ConfigPath }
ConfigPath { Path string; Rate string }

Rate strings follow ulule format: <count>-<unit> where unit is S, M, H, or D (e.g. 10-S, 1000-H).

Configuration

Field Description
Enabled Master toggle.
Default Fallback rate for paths not explicitly listed.
Paths Overrides per request path.

Error Handling

Limited requests get 429 Too Many Requests. The middleware does not return errors to callers.

Dependencies

  • Internal: appcontext, checker, codes, errors, logger
  • External: github.com/gin-gonic/gin, github.com/ulule/limiter/v3, .../drivers/middleware/gin, .../drivers/store/memory

Testing

go test ./ratelimiter/...

Contributing

See CONTRIBUTING.md. For multi-replica deployments, contribute a Redis-store variant.

  • auth — typically paired so unauthenticated traffic is rate-limited.
  • logger — required at Init time.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled bool
	Period  string
	Limit   int64
	Paths   []ConfigPath
}

type ConfigPath

type ConfigPath struct {
	Enabled bool
	Period  string
	Limit   int64
	Path    string
}

type GinMiddleware

type GinMiddleware struct {
	*mgin.Middleware
}

type HTTPMessage

type HTTPMessage struct {
	Title string `json:"title"`
	Body  string `json:"body"`
}

type HTTPResp

type HTTPResp struct {
	Message HTTPMessage `json:"message"`
	Meta    Meta        `json:"metadata"`
}

type Interface

type Interface interface {
	Limiter() gin.HandlerFunc
}

func Init

func Init(cfg Config, log logger.Interface) Interface

type Meta

type Meta struct {
	Path       string `json:"path"`
	StatusCode int    `json:"statusCode"`
	Status     string `json:"status"`
	Message    string `json:"message"`
	Timestamp  string `json:"timestamp"`
}

Jump to

Keyboard shortcuts

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