ratelimit

package
v3.20.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

Types

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.

func New added in v3.13.0

func New(ctx context.Context, conf config.RateLimiting, blobConf config.BlobConfig, httpClientProvider httpclient.Provider) (Interface, error)

New creates a new ratelimit.Interface from configuration.

func NewProfile added in v3.13.0

func NewProfile(ctx context.Context, conf config.RateLimitingProfile, size uint) (Interface, error)

NewProfile returns a new ratelimit.Interface from profile configuration.

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 RateLimitKeyer added in v3.14.0

type RateLimitKeyer interface {
	RateLimitKey() string
}

RateLimitKeyer can be implemented by request messages. If implemented, the string returned by the RateLimitKey() method is appended to the key used for rate limiting.

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