ratelimit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ratelimit provides a goroutine-safe token-bucket limiter used by the SYN scanner to pace packet emission (--rate flag). TCP-connect mode already self-paces via the socket-concurrency semaphore and does not use this package.

A zero-rate limiter (New(0)) is a no-op — Wait returns immediately — so callers can unconditionally hold a Limiter without conditional branches on a "rate limiting on" flag.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adaptive

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

Adaptive wraps a token-bucket Limiter with a feedback loop that halves the rate when the probe error-rate crosses a threshold, and doubles it (up to Max) when the error-rate stays well below.

Thresholds are static for v1 per plan §5 (2% high, 0.1% low). The window is fixed at 500 in-flight results.

func NewAdaptive

func NewAdaptive(startRate, maxRate int) *Adaptive

NewAdaptive returns an Adaptive limiter starting at startRate pps and capping at maxRate. startRate<=0 means unlimited (Adaptive becomes a no-op wrapper). maxRate<=0 defaults to startRate.

func (*Adaptive) Current

func (a *Adaptive) Current() int64

Current returns the active rate in pps (for tests and telemetry).

func (*Adaptive) ReportProbe

func (a *Adaptive) ReportProbe(isErr bool)

ReportProbe records the outcome of a probe. Pass isErr=true for timeout / unreachable / reset-storm; false for clean open/closed.

func (*Adaptive) Wait

func (a *Adaptive) Wait(ctx context.Context) error

Wait blocks for one token or until ctx is cancelled.

type Limiter

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

Limiter is an opaque handle around x/time/rate.Limiter that understands the "zero means unlimited" convention.

func New

func New(rps, burst int) *Limiter

New returns a limiter that admits at most rps events per second. burst is the maximum short-term burst; if burst <= 0 it defaults to rps (i.e. roughly one second of traffic). rps <= 0 disables the limiter; Wait is a no-op.

func (*Limiter) Limit

func (l *Limiter) Limit() int

Limit returns the configured rate in events per second. Zero means unlimited.

func (*Limiter) Wait

func (l *Limiter) Wait(ctx context.Context) error

Wait blocks until one token is available or ctx is cancelled. On the unlimited path it returns immediately with ctx.Err().

Jump to

Keyboard shortcuts

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