Skip to content

ghostcode-sys/go-api-limits

Repository files navigation

Go Reverse Proxy with Dynamic Routing & Redis-Backed Rate Limiting

This project is a high-performance reverse proxy written in Go that supports:

  • Dynamic request re-routing via JSON configuration
  • API-level rate limiting using configurable algorithms
  • Redis-backed distributed rate limiting
  • Dockerized deployment for easy setup and scalability
  • Fallback-safe and production-ready architecture

📌 Features

  • 🚀 Reverse proxy built using Go’s net/http primitives
  • 🔀 Dynamic routing using regex-based URL matching
  • ⏱️ Four rate-limiting algorithms
    • Token Bucket
    • Leaky Bucket
    • Fixed Window Counter
    • Sliding Window Log
  • 🌐 IP-based and API-based throttling
  • 🧠 Redis used as centralized rate-limit store
  • 🐳 Fully Dockerized (App + Redis)
  • ⚙️ Configurable via JSON (no code changes required)


🔀 Request Re-Routing Configuration

File: /redirection.json

This file defines URL patterns and their corresponding upstream services.

Example

[
    {
        "urlPattern": "/index\\.php(?:/|$)",
        "NewEndpoint": {
            "protocol": "*",
            "host": "*",
            "port": "8080",
            "path": "*"
        }
    },
    {
        "urlPattern": "/go/api/hr-apis",
        "NewEndpoint": {
            "protocol": "*",
            "host": "*",
            "port": "3008",
            "path": "*"
        }
    }
]

Working

Incoming Request: GET http://example.com/go/api/hr-apis/v1/list

Forwarded To: GET http://example.com:3008/go/api/hr-apis/v1/list

🔀 Reverse Proxy & Request Routing

Configuration File

/redirection.json

{
    "algorithm": "TokenBucket",
    "apiList": [
        {
            "url": "/go/api/hr-apis/v1/fetchrecruiterJobApplication",
            "method": "*",
            "ipBased": true,
            "rateLimits": [
                {
                    "timeWindowSeconds": 5,
                    "maxRequests": 2,
                    "burstSize": 2
                }
            ]
        }
    ]
}

Supported Rate Limiting Algorithms

The project implements four algorithms, selectable using the algorithm field in JSON.

1️⃣ Token Bucket

  • Tokens are added at a fixed rate.
  • Each request consumes a token.
  • Allows short bursts.
  • Best for: APIs needing burst tolerance.

2️⃣ Leaky Bucket

  • Requests enter a queue.
  • Processed at a fixed rate.
  • Excess requests are dropped.
  • Best for: Smoothing traffic spikes.

3️⃣ Fixed Window Counter

  • Counts requests in a fixed time window.
  • Resets counter when window expires.
  • Best for: Simple, low-overhead limits.

4️⃣ Sliding Window Log

  • Stores timestamps of requests.
  • Counts requests within rolling time window.
  • Best for: Precise rate limiting.

⚙️ Rate Limit Configuration Fields

Field Description
algorithm Algorithm to use (TokenBucket, LeakyBucket, FixedWindow,
url API path to apply limit
method HTTP method (GET, POST, *)
ipBased Apply limits per client IP
timeWindowSeconds Duration of rate window
maxRequests Max allowed requests
burstSize Burst capacity (used by bucket algorithms)
userBased apply limits on the bases on a key
keyPath path of key to acess that key from request(not implemented for post request keys)

Requirements & Running the Project Using Docker

This document explains system requirements, dependencies, and step-by-step instructions to run the Go Reverse Proxy with Redis-backed Rate Limiting using Docker.


📦 System Requirements

1️⃣ Operating System

The application is OS-agnostic and can run on:

  • Linux (recommended for production)
  • macOS
  • Windows (with Docker Desktop)

2️⃣ Required Software

Ensure the following tools are installed on your system:

Software Minimum Version Purpose
Docker 20.10+ Container runtime
Docker Compose v2+ Multi-container orchestration
Git Latest Source code management

🔍 Verify installation

docker --version
docker compose version
git --version

Running the Project

1️⃣ Clone the Repository

git clone go-api-limits 
cd go-api-limits

2️⃣ Review Docker Configuration

docker-compose.yml
  • Starts Go Reverse Proxy
  • Starts Redis
  • Sets environment variables automatically
services:
  router:
    build: .
    ports:
      - "9000:9000"
    network_mode: "host"
  
  db: 
    image: redis
    ports:
      - "6379:6379"
    network_mode: "host"
    volumes:
      - redis-data:/data

volumes:
  redis-data:

3️⃣ Build & Start Containers

docker compose up --build

📌 This command will:

  • Build the Go application image
  • Start Redis
  • Start the reverse proxy
  • Connect proxy to Redis internally

Your traffic, your rules, one proxy.

Save it, run it, and enjoy the proxy magic 🚀

About

Go Reverse Proxy with Dynamic Routing & Redis-Backed Rate Limiting

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published