limiter

package
v0.0.0-...-c8acfb9 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package limiter provides an IP request rate limiter with Gin middleware.

Copyright (c) 2018 - 2024 PhotoPrism UG. All rights reserved.

This program is free software: you can redistribute it and/or modify
it under Version 3 of the GNU Affero General Public License (the "AGPL"):
<https://docs.photoprism.app/license/agpl>

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

The AGPL is supplemented by our Trademark and Brand Guidelines,
which describe how our Brand Assets may be used:
<https://www.photoprism.app/trademark>

Feel free to send an email to hello@photoprism.app if you have questions, want to support our work, or just want to say hello.

Additional information can be found in our Developer Guide: <https://docs.photoprism.app/developer-guide/>

Index

Constants

View Source
const (
	DefaultAuthInterval = time.Second * 10 // average authentication errors per second
	DefaultAuthLimit    = 60               // authentication failure burst rate limit (for access tokens)
)
View Source
const (
	DefaultLoginInterval = time.Minute // average failed logins per second
	DefaultLoginLimit    = 10          // login failure burst rate limit (for passwords and 2FA)
)
View Source
const (
	DefaultIP = "0.0.0.0"
)

Variables

Auth limits the number of authentication errors from a single IP per time interval (every 15 seconds by default).

Login limits the number of failed login attempts from a single IP per time interval (one per minute by default).

Functions

func Abort

func Abort(c *gin.Context)

Abort cancels the request with error 429 (too many requests).

func AbortJSON

func AbortJSON(c *gin.Context)

AbortJSON cancels the request with error 429 (too many requests).

func Middleware

func Middleware(limiter *Limit) gin.HandlerFunc

Middleware registers the IP rate limiter middleware.

Types

type Limit

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

Limit represents an IP-based rate limiter.

func NewLimit

func NewLimit(limit rate.Limit, burst int) *Limit

NewLimit returns a new Limit with the specified request and burst rate limit per second.

func (*Limit) AddIP

func (i *Limit) AddIP(ip string) *rate.Limiter

AddIP adds a new rate limiter for the specified IP address.

func (*Limit) Allow

func (i *Limit) Allow(ip string) bool

Allow checks if a new request is allowed at this time and increments the request counter by 1.

func (*Limit) AllowN

func (i *Limit) AllowN(ip string, n int) bool

AllowN checks if a new request is allowed at this time and increments the request counter by n.

func (*Limit) IP

func (i *Limit) IP(ip string) *rate.Limiter

IP returns the rate limiter for the specified IP address. TODO: Normalize IPv6 addresses so that hosts with multiple addresses cannot be used for spray attacks.

func (*Limit) Reject

func (i *Limit) Reject(ip string) bool

Reject checks if the request rate limit has been exceeded, but does not modify the counter.

func (*Limit) Request

func (i *Limit) Request(ip string) *Request

Request tries to increment the request counter and returns the result as new *Request.

func (*Limit) RequestN

func (i *Limit) RequestN(ip string, n int) *Request

RequestN tries to increment the request counter by n and returns the result as new *Request.

func (*Limit) Reserve

func (i *Limit) Reserve(ip string) *rate.Reservation

Reserve increments the request counter and returns a rate.Reservation.

func (*Limit) ReserveN

func (i *Limit) ReserveN(ip string, n int) *rate.Reservation

ReserveN increments the request counter by n and returns a rate.Reservation.

type Request

type Request struct {
	Tokens int
	// contains filtered or unexported fields
}

Request represents a request for the specified number of limiter tokens.

func NewRequest

func NewRequest(l *rate.Limiter, n int) *Request

NewRequest checks if a request is allowed, reserves the required tokens, and returns a new Request to revert the reservation if successful.

func (*Request) Allow

func (r *Request) Allow() bool

Allow checks if the request is allowed.

func (*Request) Reject

func (r *Request) Reject() bool

Reject returns true if the request should be rejected.

func (*Request) Success

func (r *Request) Success()

Success returns the rate limit tokens that have been reserved for this request, if any.

Jump to

Keyboard shortcuts

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