config

package
v2.28.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

Package config provides data structure to configure rate-limiter.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	// HTTP message when limit is reached.
	Message string

	// HTTP status code when limit is reached.
	StatusCode int

	// Maximum number of requests to limit per duration.
	Max int64

	// Duration of rate-limiter.
	TTL time.Duration

	// List of places to look up IP address.
	// Default is "RemoteAddr", "X-Forwarded-For", "X-Real-IP".
	// You can rearrange the order as you like.
	IPLookups []string

	// List of HTTP Methods to limit (GET, POST, PUT, etc.).
	// Empty means limit all methods.
	Methods []string

	// List of HTTP headers to limit.
	// Empty means skip headers checking.
	Headers map[string][]string

	// List of basic auth usernames to limit.
	BasicAuthUsers []string

	// Able to configure token bucket expirations.
	TokenBuckets struct {
		// Default TTL to expire bucket per key basis.
		DefaultExpirationTTL time.Duration

		// How frequently tollbooth will trigger the expire job
		ExpireJobInterval time.Duration
	}

	sync.RWMutex
	// contains filtered or unexported fields
}

Limiter is a config struct to limit a particular request handler.

func NewLimiter

func NewLimiter(max int64, ttl time.Duration) *Limiter

NewLimiter is a constructor for Limiter.

func NewLimiterExpiringBuckets added in v1.3.0

func NewLimiterExpiringBuckets(max int64, ttl, bucketDefaultExpirationTTL, bucketExpireJobInterval time.Duration) *Limiter

NewLimiterExpiringBuckets constructs Limiter with expirable TokenBuckets.

func (*Limiter) LimitReached

func (l *Limiter) LimitReached(key string) bool

LimitReached returns a bool indicating if the Bucket identified by key ran out of tokens.

func (*Limiter) LimitReachedWithCustomTokenBucketTTL added in v1.3.0

func (l *Limiter) LimitReachedWithCustomTokenBucketTTL(key string, tokenBucketTTL time.Duration) bool

LimitReachedWithCustomTokenBucketTTL returns a bool indicating if the Bucket identified by key ran out of tokens. This public API allows user to define custom expiration TTL on the key.

func (*Limiter) SetExpiring added in v1.3.0

func (l *Limiter) SetExpiring(bucketDefaultExpirationTTL, bucketExpireJobInterval time.Duration) *Limiter

Jump to

Keyboard shortcuts

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