connection_limits

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLimitExceeded    = errors.New("too many concurrent connections")
	ErrThrottleExceeded = errors.New("too many recent connection attempts")
)

Functions

This section is empty.

Types

type GenericThrottle added in v1.0.0

type GenericThrottle struct {
	ThrottleDetails // variable state: what events have been seen
	// these are constant after creation:
	Duration time.Duration // window length to consider
	Limit    int           // number of events allowed per window
}

GenericThrottle allows enforcing limits of the form "at most X events per time window of duration Y"

func (*GenericThrottle) Touch added in v1.0.0

func (g *GenericThrottle) Touch() (throttled bool, remainingTime time.Duration)

Touch checks whether an additional event is allowed: it either denies it (by returning false) or allows it (by returning true) and records it

type Limiter

type Limiter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Limiter manages the automated client connection limits.

func (*Limiter) AddClient

func (cl *Limiter) AddClient(addr net.IP, force bool) error

AddClient adds a client to our population if possible. If we can't, throws an error instead. 'force' is used to add already-existing clients (i.e. ones that are already on the network).

func (*Limiter) ApplyConfig

func (cl *Limiter) ApplyConfig(config LimiterConfig) error

ApplyConfig atomically applies a config update to a connection limit handler

func (*Limiter) RemoveClient

func (cl *Limiter) RemoveClient(addr net.IP)

RemoveClient removes the given address from our population

type LimiterConfig

type LimiterConfig struct {
	Enabled        bool
	CidrLenIPv4    int `yaml:"cidr-len-ipv4"`
	CidrLenIPv6    int `yaml:"cidr-len-ipv6"`
	ConnsPerSubnet int `yaml:"connections-per-subnet"`
	IPsPerSubnet   int `yaml:"ips-per-subnet"` // legacy name for ConnsPerSubnet
	Exempted       []string
}

LimiterConfig controls the automated connection limits.

type ThrottleDetails

type ThrottleDetails struct {
	Start time.Time
	Count int
}

ThrottleDetails holds the connection-throttling details for a subnet/IP.

type Throttler

type Throttler struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Throttler manages automated client connection throttling.

func (*Throttler) AddClient

func (ct *Throttler) AddClient(addr net.IP) error

AddClient introduces a new client connection if possible. If we can't, throws an error instead.

func (*Throttler) ApplyConfig

func (ct *Throttler) ApplyConfig(config ThrottlerConfig) error

ApplyConfig atomically applies a config update to a throttler

func (*Throttler) BanDuration

func (ct *Throttler) BanDuration() time.Duration

func (*Throttler) BanMessage

func (ct *Throttler) BanMessage() string

func (*Throttler) ResetFor

func (ct *Throttler) ResetFor(addr net.IP)

ResetFor removes any existing count for the given address.

type ThrottlerConfig

type ThrottlerConfig struct {
	Enabled            bool
	CidrLenIPv4        int           `yaml:"cidr-len-ipv4"`
	CidrLenIPv6        int           `yaml:"cidr-len-ipv6"`
	ConnectionsPerCidr int           `yaml:"max-connections"`
	DurationString     string        `yaml:"duration"`
	Duration           time.Duration `yaml:"duration-time"`
	BanDurationString  string        `yaml:"ban-duration"`
	BanDuration        time.Duration
	BanMessage         string `yaml:"ban-message"`
	Exempted           []string
}

ThrottlerConfig controls the automated connection throttling.

type TorLimiter added in v1.1.0

type TorLimiter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

TorLimiter is a combined limiter and throttler for use on connections proxied from a Tor hidden service (so we don't have meaningful IPs, a notion of CIDR width, etc.)

func (*TorLimiter) AddClient added in v1.1.0

func (tl *TorLimiter) AddClient() error

func (*TorLimiter) Configure added in v1.1.0

func (tl *TorLimiter) Configure(maxConnections int, duration time.Duration, maxConnectionsPerDuration int)

func (*TorLimiter) RemoveClient added in v1.1.0

func (tl *TorLimiter) RemoveClient()

Jump to

Keyboard shortcuts

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