ratelimit

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MIT Imports: 5 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(ctx *mist.Context, key string) (bool, error)
}

Limiter is an interface designed to abstract the rate-limiting logic. It confirms whether a particular action exceeds a predefined rate limit. Implementations of this interface can enforce rate limits in different ways, such as using a sliding window algorithm, token bucket, or any other strategy. Method: Limit The Limit method is responsible for determining if the given key has exceeded its allowable number of requests within a given time frame.

Params:

  • ctx: A pointer to a mist.Context object, which transports request-scoped values, cancellation signals, deadlines, and other information across API boundaries.
  • key: A string acting as the unique identifier to which the rate limit should be applied.

Returns:

  • A boolean indicating whether the request is within the rate limits (`true` if it's within limit, `false` if it exceeds).
  • An error object that will be non-nil if an error occurs during the check (e.g., database or network issues).

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