ratelimit

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 4 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 interface {
	// Limit restricts the request rate based on the provided context and key.
	//
	// Parameters:
	//     ctx (context.Context): The context for the request, which is used to control the lifecycle of the request.
	//                            It can convey deadlines, cancellations signals, and other request-scoped values across API boundaries and goroutines.
	//     key (string): A unique string that identifies the request or resource to be limited.
	//                   This key is typically derived from the user's ID, IP address, or other identifying information.
	//
	// Returns:
	//     (bool, error): Returns a boolean indicating whether the request is allowed (true) or rate-limited (false).
	//                    If an error occurs during the process, it returns a non-nil error value.
	Limit(ctx context.Context, key string) (bool, error)
}

Limiter is an interface that defines a rate limiting method.

type RedisSlidingWindowLimiter

type RedisSlidingWindowLimiter struct {

	// Cmd is an interface from the go-redis package (redis.Cmdable).
	// This interface includes methods for all Redis commands to execute queries.
	// Using an interface here instead of a specific type makes the limiter more flexible,
	// as it can accept any type that implements the `redis.Cmdable` interface, such as a Redis client or a Redis Cluster client.
	Cmd redis.Cmdable

	// Interval is of type time.Duration, representing the time window size for the rate limiter.
	// Interval is a type from the time package defining a duration or elapsed time in nanoseconds.
	// In terms of rate limiting, the interval is the time span during which a certain maximum number of requests can be made.
	Interval time.Duration

	// Rate is an integer that defines the maximum number of requests that can occur within the provided duration or interval.
	// For example, if Interval is 1 minute (`time.Minute`), and Rate is 100, this means a maximum of 100 requests can be made per minute.
	Rate int
}

RedisSlidingWindowLimiter struct is a structure in Go which represents a rate limiter using sliding window algorithm.

func (*RedisSlidingWindowLimiter) Limit

func (r *RedisSlidingWindowLimiter) Limit(ctx context.Context, key string) (bool, error)

Limit is a method of the RedisSlidingWindowLimiter struct. It determines if a specific key has exceeded the allowed number of requests (rate) within the defined interval.

Params:

  • ctx: A context.Context object. It carries deadlines, cancellations signals, and other request-scoped values across API boundaries and between processes. It is often used for timeout management.
  • key: A string that serves as a unique identifier for the request to be rate-limited.

Returns:

  • A boolean value indicating whether the request associated with the key is within the allowed rate limits. It returns `true` when the rate limit is not reached, and `false` otherwise.
  • An error object that will hold an error (if any) that may have occurred during the function execution.

The method uses the Eval command of the Redis server to execute a Lua script (luaSlideWindow) that implements the sliding window rate limit algorithm. It passes converted interval in milliseconds (r.Interval.Milliseconds()), maximum requests allowed (r.Rate), and the current Unix timestamp in milliseconds (time.Now().UnixMilli()) as parameters to the Lua script.

Jump to

Keyboard shortcuts

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