ratelimit

package
v0.0.1-rc7 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ratelimit implements per-IP token-bucket rate limiting and per-target cooldown / recent-scan cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP extracts a stable source identifier from r. Honours one hop of `X-Forwarded-For` so a deployment behind a single trusted reverse proxy still counts the right IPs. Anything beyond that is the operator's responsibility (auth proxy, mTLS, …).

Types

type IPLimiter

type IPLimiter struct {
	// contains filtered or unexported fields
}

IPLimiter is a per-source-IP token bucket. The map of buckets is trimmed lazily — entries unused for cleanupAfter are evicted on the next Allow() call. No background goroutine.

func NewIPLimiter

func NewIPLimiter(maxPerPeriod int, period time.Duration) *IPLimiter

NewIPLimiter returns a limiter that enforces N requests per period with a small burst. period=0 → 1 minute default.

func (*IPLimiter) Allow

func (l *IPLimiter) Allow(src string) bool

Allow records one event from src and reports whether it fits inside the per-IP budget.

func (*IPLimiter) Middleware

func (l *IPLimiter) Middleware() func(http.Handler) http.Handler

Middleware enforces the per-IP budget on every request. On overflow it returns 429 with a JSON envelope.

type PreScanDecision

type PreScanDecision struct {
	CachedScanID string        // non-empty → reuse this scan instead of creating one
	CooldownLeft time.Duration // > 0 → 429 with Retry-After
	AbuseFlagged bool
}

PreScan inspects the (ip, host) pair before a new scan is launched. Returns the pre-existing scan ID when a fresh-enough cache entry exists and refresh is false.

type TargetTracker

type TargetTracker struct {
	// contains filtered or unexported fields
}

TargetTracker enforces the per-hostname cooldown, the 24h cached-scan reuse window, and the per-IP fan-out abuse heuristic.

func NewTargetTracker

func NewTargetTracker(cooldown, cacheTTL time.Duration, abuseLim int, abuseWin time.Duration) *TargetTracker

NewTargetTracker — zero values fall back to sensible defaults.

func (*TargetTracker) PreScan

func (t *TargetTracker) PreScan(ip, host string, refresh bool) PreScanDecision

PreScan registers the request and returns its decision.

func (*TargetTracker) Record

func (t *TargetTracker) Record(host, scanID string)

Record registers the freshly created scan ID against host so future requests can reuse it for the cacheTTL window.

Jump to

Keyboard shortcuts

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