MCPcopy Index your code
hub / github.com/JGLTechnologies/gin-rate-limit

github.com/JGLTechnologies/gin-rate-limit @v2.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.0.0 ↗ · + Follow
11 symbols 17 edges 2 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

GinRateLimit

GinRateLimit is a rate limiter for the gin framework. By default, it can only store rate limit info in memory and with redis. If you want to store it somewhere else you can make your own store or use third party stores. The library is new so there are no third party stores yet, so I would appreciate if someone could make one.

Install

```shell go get github.com/JGLTechnologies/GinRateLimit






Redis Example

```go
package main

import (
    "github.com/JGLTechnologies/GinRateLimit"
    "github.com/gin-gonic/gin"
    "github.com/go-redis/redis/v8"
    "time"
)

func keyFunc(c *gin.Context) string {
    return c.ClientIP()
}

func errorHandler(c *gin.Context, remaining time.Duration) {
    c.String(429, "Too many requests. Try again in "+remaining.String())
}

func main() {
    server := gin.Default()
    // This makes it so each ip can only make 5 requests per second
    // The redis store is still being tested. If you experience any bugs, report it on our GitHub or make a pull request to fix it.
    store := GinRateLimit.RedisStore(time.Second, 5, redis.NewClient(&redis.Options{
        ReadTimeout:  time.Second,
        WriteTimeout: time.Second,
        IdleTimeout:  time.Second * 5,
    }))
    mw := GinRateLimit.RateLimiter(keyFunc, errorHandler, store)
    server.GET("/", mw, func(c *gin.Context) {
        c.String(200, "Hello World")
    })
    server.Run(":8080")
}

Basic Setup

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/JGLTechnologies/GinRateLimit"
    "time"
)

func keyFunc(c *gin.Context) string {
    return c.ClientIP()
}

func errorHandler(c *gin.Context, remaining time.Duration) {
    c.String(429, "Too many requests. Try again in "+remaining.String())
}

func main() {
    server := gin.Default()
    // This makes it so each ip can only make 5 requests per second
    store := GinRateLimit.InMemoryStore(time.Second, 5)
    mw := GinRateLimit.RateLimiter(keyFunc, errorHandler, store)
    server.GET("/", mw, func(c *gin.Context) {
        c.String(200, "Hello World")
    })
    server.Run(":8080")
}

Custom Store Example

package main

import "time"

type CustomStore struct {
}

// Your store must have a method called Limit that takes a key and returns a bool
func (s *CustomStore) Limit(key string) (bool, time.Duration) {
    // Do your rate limit logic, and return true if the user went over the rate limit, otherwise return false
    // Return the amount of time the client needs to wait to make a new request
    if UserWentOverLimit {
        return true
    }
    return false
}

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Function 4
Method 3
Struct 3
Interface 1

Languages

Go100%

Modules by API surface

GinRateLimit.go8 symbols
redis.go3 symbols

For agents

$ claude mcp add gin-rate-limit \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page