internal

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

Limiter bounds concurrent executions using a buffered channel semaphore.

func NewLimiter

func NewLimiter(limit int) *Limiter

NewLimiter creates a limiter with the provided capacity.

func (*Limiter) Acquire

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

Acquire reserves an execution slot or returns when the context is canceled.

func (*Limiter) Release

func (l *Limiter) Release()

Release frees an execution slot.

func (*Limiter) TryAcquire added in v0.4.0

func (l *Limiter) TryAcquire() bool

TryAcquire reserves an execution slot without blocking. It returns false when all slots are in use.

type TokenBucket

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

TokenBucket is a non-blocking token bucket rate limiter.

Tokens are refilled continuously at refillRate tokens per second up to capacity. Each call to Allow consumes one token if available and returns true; otherwise it returns false immediately without blocking.

The caller controls the clock by passing the current time to Allow, which makes the bucket deterministic and easy to test.

func NewDrainedTokenBucket

func NewDrainedTokenBucket(capacity int, refillRate float64) *TokenBucket

NewDrainedTokenBucket is like NewTokenBucket but starts with zero tokens. Refill rate and capacity are unchanged; emissions pace from the first Allow instead of consuming an initial full bucket in one burst.

func NewTokenBucket

func NewTokenBucket(capacity int, refillRate float64) *TokenBucket

NewTokenBucket creates a full token bucket with the given capacity and refill rate (tokens per second).

Panics if capacity <= 0 or refillRate <= 0.

func (*TokenBucket) Allow

func (tb *TokenBucket) Allow(now time.Time) bool

Allow refills tokens based on elapsed time since the last call, then consumes one token if available. It returns true when the request is allowed, false when the bucket is empty.

now must be monotonically non-decreasing across calls from the same goroutine; passing a time earlier than the previous call is a no-op for the refill step (elapsed is clamped to zero).

func (*TokenBucket) SetRefillRate

func (tb *TokenBucket) SetRefillRate(rate float64, now time.Time)

SetRefillRate applies refill from lastRefill to now at the current rate, then switches the bucket to rate for future refills. Tokens stay capped at capacity.

Panics if rate <= 0, consistent with NewTokenBucket.

Directories

Path Synopsis
Package ssrf provides an opt-in policy that rejects HTTP requests to private, link-local, and cloud-metadata IP ranges before they leave the process.
Package ssrf provides an opt-in policy that rejects HTTP requests to private, link-local, and cloud-metadata IP ranges before they leave the process.

Jump to

Keyboard shortcuts

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