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: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MiddlewareBuilder

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

MiddlewareBuilder is a struct type that encapsulates the logic for building rate limiting middleware which can be used in different contexts, such as an HTTP Server to control the rate of incoming requests. Fields:

  • limiter: This is an implementation of the Limiter interface, it controls how to check and handle rate limits (e.g., using a sliding window algorithm or token bucket strategy).

  • keyFn: This is a function that generates a unique key for each request, based on the provided mist.Context. For example, in an HTTP context this may generate keys based on a client IP address or authenticated user ID.

  • logFn: This function is used for logging purposes and can be customized as per your requirements. It receives a string for log level, a string for log message, and a variadic parameter for any additional arguments.

  • retryAfterSec: Integer value specifying the time in seconds a client should wait before retrying when they hit the rate limit.

func InitMiddlewareBuilder

func InitMiddlewareBuilder(limiter ratelimit.Limiter, retryAfterSec int) *MiddlewareBuilder

InitMiddlewareBuilder is a function used to initialize a MiddlewareBuilder instance. It sets up the rate limiter, retry timer, default key generation function and the logging function. The function can be further customized with MiddlewareOptions. Parameters:

  • limiter: This is the rate limiter instance that controls the rate of incoming requests based on the generated key.
  • retryAfterSec: This is an integer representing the time to wait before retrying in the event of a rate limit breach.
  • opts: These are optional arguments that provide further customization to the MiddlewareBuilder. They are functions that accept an all MiddlewareOptions and allow you to set various fields of the MiddlewareBuilder.

Returns:

  • A pointer to the newly created MiddlewareBuilder.

The InitMiddlewareBuilder function provides initially default implementations for the key generation function (keyFn) and logging function (logFn), but these can be replaced with custom implementations using the WithKeyGenFunc and WithLogFunc functions.

func (*MiddlewareBuilder) Build

func (b *MiddlewareBuilder) Build() mist.Middleware

Build is a method of the MiddlewareBuilder type. It returns a middleware that encompasses rate limiting logic. The returned middleware is a pipeline unit in the Mist web framework, which is a function accepting the next middleware (i.e., next mist.HandleFunc) and returns the resulting middleware function. Built middleware does the following on each incoming request:

  1. It uses the limiter to check if the request rate limit has been exceeded for the current client (using the rate limiter key generated by keyFn from the mist.Context).
  2. In case the limit function returns an error, it logs an error message, sets the HTTP response status code to 500 (Internal Server Error), and ends the request handling pipeline by not calling the next middleware.
  3. If the limit is exceeded (as indicated by the 'limited' boolean), it logs a warning message, sets the HTTP response status code to 429 (Too Many Requests), instructs the client when to retry by setting the 'Retry-After' response header, and ends the request-handling pipeline.
  4. If the rate limit has not been exceeded, it just passes the control to the next middleware in the pipeline by calling next with the mist.Context.

func (*MiddlewareBuilder) SetKeyGenFunc added in v0.0.27

func (b *MiddlewareBuilder) SetKeyGenFunc(fn func(*mist.Context) string) *MiddlewareBuilder

SetKeyGenFunc sets the key generation function to be used by the middleware. This function will be called to generate a key for each request, which can be used for various purposes, such as rate limiting or caching. Parameters: - fn: a function that takes a pointer to a mist.Context and returns a string key. Returns: - the pointer to the MiddlewareBuilder instance to allow method chaining.

func (*MiddlewareBuilder) SetLogFunc added in v0.0.27

func (b *MiddlewareBuilder) SetLogFunc(fn func(level string, msg any, args ...any)) *MiddlewareBuilder

SetLogFunc sets the logging function to be used by the middleware. This function will be called whenever the middleware needs to log information, allowing for custom logging implementations. Parameters: - fn: a function that takes a log level as a string, a message of any type, and optional additional arguments. Returns: - the pointer to the MiddlewareBuilder instance to allow method chaining.

Jump to

Keyboard shortcuts

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