ratelimit

package
v0.0.0-...-5090d89 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EchoMiddleware

func EchoMiddleware(limiter Interface, class string) echo.MiddlewareFunc

EchoMiddleware is an Echo middleware that rate limits HTTP requests by remote IP and request URL.

func HTTPMiddleware

func HTTPMiddleware(limiter Interface, class string) func(http.Handler) http.Handler

HTTPMiddleware is an HTTP middleware that rate limits by remote IP and the request URL. The remote IP is retrieved by the X-Real-IP header. Use this middleware after webmiddleware.ProxyHeaders()

func Require

func Require(limiter Interface, resource Resource) error

Require checks that the rate limit for a Resource has not been exceeded.

func StreamServerInterceptor

func StreamServerInterceptor(limiter Interface) grpc.StreamServerInterceptor

StreamServerInterceptor is a grpc.StreamServerInterceptor that rate limits new gRPC requests and messages sent by the client. If the X-Real-IP header is not set, it is assumed that the gRPC request originates from the cluster, and no rate limits are enforced.

func UnaryServerInterceptor

func UnaryServerInterceptor(limiter Interface) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a gRPC unary server interceptor that rate limits incoming gRPC requests. If the X-Real-IP header is not set, it is assumed that the request originates from the cluster, and no rate limits are enforced.

Types

type Config

type Config struct {
	Memory   MemoryConfig `name:"memory" description:"In-memory rate limiting store configuration"`
	Profiles []Profile    `name:"profiles" description:"Rate limiting profiles"`
}

Config represents configuration for rate limiting.

func (Config) New

func (c Config) New(ctx context.Context) (Interface, error)

New creates a new ratelimit.Interface from configuration.

type Interface

type Interface interface {
	// RateLimit limits access on a Resource.
	//
	// The RateLimit operations returns true if the rate limit for the requested resource has been exceeded, along
	// with metadata for the rate limiting operation.
	RateLimit(resource Resource) (limit bool, result Result)
}

Interface can be used to rate limit access to a Resource.

type MemoryConfig

type MemoryConfig struct {
	MaxSize uint `name:"max-size" description:"Maximum store size for the rate limiter"`
}

MemoryConfig represents configuration for the in-memory rate limiting store.

type NoopRateLimiter

type NoopRateLimiter struct{}

NoopRateLimiter does not enforce any rate limits.

func (*NoopRateLimiter) RateLimit

func (*NoopRateLimiter) RateLimit(Resource) (bool, Result)

RateLimit implements ratelimit.Interface.

type Profile

type Profile struct {
	Name          string   `name:"name" description:"Rate limiting class name"`
	MaxRatePerMin uint     `name:"max-rate-per-min" description:"Maximum allowed rate per minute"`
	MaxBurst      uint     `name:"max-burst" description:"Maximum rate allowed for short bursts"`
	Associations  []string `name:"associations" description:"List of classes to apply this profile on"`
}

Profile represents configuration for a rate limiting class.

func (Profile) New

func (c Profile) New(ctx context.Context, size uint) (Interface, error)

New creates a new ratelimit.Interface from configuration.

type Resource

type Resource interface {
	// Key represents the unique identifier for the resource.
	Key() string
	// Classes represents the rate limiting classes for the resource. A resource may
	// belong in multiple classes. The limiter instance will limit access to the resource
	// based on the limits of the first class that matches the configuration. If no
	// class is matched, the limiter instance can fallback to a default rate limit.
	Classes() []string
}

Resource represents an entity on which rate limits apply.

func ApplicationAcceptMQTTConnectionResource

func ApplicationAcceptMQTTConnectionResource(remoteAddr string) Resource

ApplicationAcceptMQTTConnectionResource represents a new MQTT client connection from a remote IP address.

func ApplicationMQTTDownResource

func ApplicationMQTTDownResource(ctx context.Context, ids ttnpb.ApplicationIdentifiers, authTokenID string) Resource

ApplicationMQTTDownResource represents downlink traffic for an application from an MQTT client.

func ApplicationWebhooksDownResource

func ApplicationWebhooksDownResource(ctx context.Context, ids ttnpb.EndDeviceIdentifiers, authTokenID string) Resource

ApplicationWebhooksDownResource represents downlink traffic for an application from a webhook.

func GatewayAcceptMQTTConnectionResource

func GatewayAcceptMQTTConnectionResource(remoteAddr string) Resource

GatewayAcceptMQTTConnectionResource represents a new MQTT gateway connection from a remote address.

func GatewayUDPTrafficResource

func GatewayUDPTrafficResource(addr *net.UDPAddr) Resource

GatewayUDPTrafficResource represents UDP gateway traffic from a remote IP address.

func GatewayUpResource

func GatewayUpResource(ctx context.Context, ids ttnpb.GatewayIdentifiers) Resource

GatewayUpResource represents uplink traffic from a gateway.

func NewCustomResource

func NewCustomResource(key string, classes ...string) Resource

NewCustomResource returns a new resource. It is used internally by other components.

type Result

type Result struct {
	Limit      int
	Remaining  int
	ResetAfter time.Duration
	RetryAfter time.Duration
}

Result contains rate limiting metadata.

func (Result) GRPCHeaders

func (r Result) GRPCHeaders() metadata.MD

GRPCHeaders returns gRPC headers from a rate limiting result.

func (Result) IsZero

func (r Result) IsZero() bool

IsZero checks if the result is empty.

func (Result) SetHTTPHeaders

func (r Result) SetHTTPHeaders(header http.Header)

SetHTTPHeaders sets HTTP headers from a rate limiting result.

Jump to

Keyboard shortcuts

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