limiter

package
v4.3.10+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2021 License: Apache-2.0 Imports: 11 Imported by: 188

Documentation

Overview

Package limiter implements connection and rate limiters for teleport

Index

Constants

View Source
const (
	DefaultMaxNumberOfUsers = 100000
	DefaultRate             = 100000000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectionsLimiter

type ConnectionsLimiter struct {
	*connlimit.ConnLimiter
	*sync.Mutex
	// contains filtered or unexported fields
}

ConnectionsLimiter is a network connection limiter and tracker

func NewConnectionsLimiter

func NewConnectionsLimiter(config LimiterConfig) (*ConnectionsLimiter, error)

NewConnectionsLimiter returns new connection limiter, in case if connection limits are not set, they won't be tracked

func (*ConnectionsLimiter) AcquireConnection

func (l *ConnectionsLimiter) AcquireConnection(token string) error

AcquireConnection acquires connection and bumps counter

func (*ConnectionsLimiter) ReleaseConnection

func (l *ConnectionsLimiter) ReleaseConnection(token string)

ReleaseConnection decrements the counter

func (*ConnectionsLimiter) WrapHandle

func (l *ConnectionsLimiter) WrapHandle(h http.Handler)

WrapHandle adds connection limiter to the handle

type Limiter

type Limiter struct {
	// ConnectionsLimiter limits simultaneous connection
	*ConnectionsLimiter
	// contains filtered or unexported fields
}

Limiter helps limiting connections and request rates

func NewLimiter

func NewLimiter(config LimiterConfig) (*Limiter, error)

NewLimiter returns new rate and connection limiter

func (*Limiter) RegisterRequest

func (l *Limiter) RegisterRequest(token string) error

func (*Limiter) WrapHandle

func (l *Limiter) WrapHandle(h http.Handler)

Add limiter to the handle

type LimiterConfig

type LimiterConfig struct {
	// Rates set ups rate limits
	Rates []Rate
	// MaxConnections configures maximum number of connections
	MaxConnections int64
	// MaxNumberOfUsers controls maximum number of simultaneously active users
	MaxNumberOfUsers int
	// Clock is an optional parameter, if not set, will use system time
	Clock timetools.TimeProvider
}

LimiterConfig sets up rate limits and configuration limits parameters

func (*LimiterConfig) SetEnv

func (l *LimiterConfig) SetEnv(v string) error

SetEnv reads LimiterConfig from JSON string

type Rate

type Rate struct {
	Period  time.Duration
	Average int64
	Burst   int64
}

Rate defines connection rate

func (*Rate) UnmarshalJSON

func (r *Rate) UnmarshalJSON(value []byte) error

type RateLimiter

type RateLimiter struct {
	*ratelimit.TokenLimiter

	*sync.Mutex
	// contains filtered or unexported fields
}

RateLimiter controls connection rate, it uses token bucket algo https://en.wikipedia.org/wiki/Token_bucket

func NewRateLimiter

func NewRateLimiter(config LimiterConfig) (*RateLimiter, error)

NewRateLimiter returns new request rate controller

func (*RateLimiter) RegisterRequest

func (l *RateLimiter) RegisterRequest(token string) error

RegisterRequest increases number of requests for the provided token Returns error if there are too many requests with the provided token

func (*RateLimiter) WrapHandle

func (l *RateLimiter) WrapHandle(h http.Handler)

Add rate limiter to the handle

Jump to

Keyboard shortcuts

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