NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

About
LoggingHealth ChecksProfilingMulti-Server SupportStatic FilesRate LimitingPanic RecoveryCustom 404 HandlerCustom 404 PageError Handling
RouterFeatures

Rate Limiting

Protect routes from excessive traffic using IP and route-based request throttling middleware.

The router package includes rate limiting middleware for throttling repeated requests from the same client.

Rate limiting is useful for:

  • protecting public endpoints
  • reducing abuse and accidental traffic spikes
  • limiting expensive handlers
  • slowing down brute-force attempts
  • adding lightweight per-route traffic control

The limiter tracks requests by client and route, then rejects requests that arrive too frequently.

When a client exceeds the configured limit, the router responds with:

429 Too Many Requests

Basic Usage

r.Use(router.RateLimit(50 * time.Millisecond))

This allows one request per client and route every 50ms.

That is approximately:

20 requests per second

Example

package main

import (
	"log/slog"
	"net/http"
	"os"
	"time"

	"github.com/netlifeguru/router"
)

func main() {
	r := router.New()

	r.Use(router.RateLimit(50 * time.Millisecond))

	r.HandleFunc("/", "GET", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
		w.Write([]byte("Success! You are not rate-limited."))
	})

	if err := r.ListenAndServe(":8000"); err != nil {
		slog.Error("failed to start server", "error", err)
		os.Exit(1)
	}
}

Run the application:

go run .

Test the endpoint:

curl http://localhost:8000/

If requests are sent too quickly, the router returns:

429 Too Many Requests

Custom Configuration

The rate limiter can be customized using configuration options.

func CustomRateLimitOpt(cfg *router.RateLimitConfig) {
	cfg.TTL = 5 * time.Minute
	cfg.CleanupInterval = 1 * time.Minute
}

Then pass the option to the middleware:

r.Use(router.RateLimit(50*time.Millisecond, CustomRateLimitOpt))

Full example:

package main

import (
	"log/slog"
	"net/http"
	"os"
	"time"

	"github.com/netlifeguru/router"
)

func CustomRateLimitOpt(cfg *router.RateLimitConfig) {
	cfg.TTL = 5 * time.Minute
	cfg.CleanupInterval = 1 * time.Minute
}

func main() {
	r := router.New()

	r.Use(router.RateLimit(50*time.Millisecond, CustomRateLimitOpt))

	r.HandleFunc("/", "GET", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
		w.Write([]byte("Success! You are not rate-limited."))
	})

	if err := r.ListenAndServe(":8000"); err != nil {
		slog.Error("failed to start server", "error", err)
		os.Exit(1)
	}
}

Configuration Options

TTL

TTL defines how long a client tracking record is kept in memory.

cfg.TTL = 5 * time.Minute

Longer TTL values remember clients for a longer period of time.

CleanupInterval

CleanupInterval defines how often old client tracking records are removed.

cfg.CleanupInterval = 1 * time.Minute

Shorter cleanup intervals remove expired records more frequently, while longer intervals reduce cleanup overhead.


Notes

Rate limiting is registered as middleware.

When applied globally:

r.Use(router.RateLimit(50 * time.Millisecond))

it affects all routes.

When applied to a route group, it only affects routes inside that group.

The limiter is intended as a lightweight application-level guard. For large distributed systems, combine it with infrastructure-level rate limiting such as reverse proxies, API gateways, or load balancers.

Static Files

Serve CSS, JavaScript, images, and favicon files from a local directory using a URL prefix.

Panic Recovery

Handle panics from route handlers safely using a custom recovery handler and fail-safe internal recovery.

On this page

Basic UsageExampleCustom ConfigurationConfiguration OptionsTTLCleanupIntervalNotes