ratelimit

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewThrottledTransport

func NewThrottledTransport(limitPeriod time.Duration, requestCount int, transportWrap http.RoundTripper) http.RoundTripper

NewThrottledTransport creates a new ThrottledTransport with the specified rate limit.

func Take

func Take(ctx context.Context, l Limiter)

Take is a helper function that calls the Take method on a Limiter. It is used to apply rate limiting to an operation.

Types

type Limiter

type Limiter interface {
	Take(ctx context.Context) time.Duration
}

Limiter is an interface for rate limiting functionality. It defines a method for taking a rate-limited action.

func NewLocalLimiter

func NewLocalLimiter(maximumRPS int, burstableRPS int) Limiter

NewLocalLimiter creates a new local rate limiter with specified maximum and burstable requests per second.

func NewRedisLimiter

func NewRedisLimiter(redisClient *redis.Client, maxRPS int) Limiter

NewRedisLimiter creates a new Redis-based rate limiter.

type Local

type Local struct {
	*rate.Limiter // Embedded rate limiter from the rate package
}

Local represents a local rate limiter using the golang.org/x/time/rate package.

func (Local) Take

func (l Local) Take(ctx context.Context) time.Duration

Take attempts to allow an action under the rate limit and returns the duration taken.

type Redis

type Redis struct {
	*redis_rate.Limiter     // Embedded Redis rate limiter
	MaxRPS              int // Maximum requests per second allowed
}

Redis represents a rate limiter using Redis.

func (Redis) Take

func (r Redis) Take(ctx context.Context) time.Duration

Take attempts to allow a request under the rate limit and blocks until allowed.

type ThrottledTransport

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

ThrottledTransport is a custom HTTP transport that implements rate limiting.

func (*ThrottledTransport) RoundTrip

func (t *ThrottledTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface for ThrottledTransport. It ensures that requests are made according to the rate limit.

Jump to

Keyboard shortcuts

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