krakendrate

package module
v3.0.0-...-854be10 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

KrakenD rate limit

A rate limit middleware for KrakenD

Documentation

  1. Official: https://www.krakend.io/docs/throttling/

  2. Autogenerated: https://godoc.org/github.com/anshulgoel27/krakend-ratelimit/v3

Documentation

Overview

krakendrate contains a collection of curated rate limit adaptors for the KrakenD framework

krakendrate contains a collection of curated rate limit adaptors for the KrakenD framework

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLimited is the error returned when the rate limit has been exceded
	ErrLimited = errors.New("rate limit exceded")

	// DataTTL is the default eviction time
	DataTTL = 10 * time.Minute

	// DefaultShards are the number of shards to create by default
	DefaultShards uint64 = 2048
)

Functions

func PseudoFNV64a

func PseudoFNV64a(s string) uint64

Types

type Backend

type Backend interface {
	Load(string, func() interface{}) interface{}
	Store(string, interface{}) error
}

Backend is the interface of the persistence layer

func MemoryBackendBuilder

func MemoryBackendBuilder(ctx context.Context, ttl, cleanupRate time.Duration,
	cleanUpThreads, amount uint64,
) []Backend

type BackendBuilder

type BackendBuilder func(ctx context.Context, ttl time.Duration, cleanUpRate time.Duration, cleanUpThreads uint64, amount uint64) []Backend

BackendBuilder is the type for a function that can build a Backend. Is is used by the ShardedMemoryBackend to create several backends / shards.

type Clock

type Clock interface {
	Now() time.Time
	Since(time.Time) time.Duration
}

Clock defines the interface for clock sources

type Hasher

type Hasher func(string) uint64

Hasher gets a hash for the received string

type Limiter

type Limiter interface {
	Allow() bool
}

Limiter defines a simple interface for a rate limiter

type LimiterBuilderFn

type LimiterBuilderFn func() interface{}

LimiterBuilderFn defines the function that will be called when there is no entry in the backend for a given token.

func NewTokenBucketBuilder

func NewTokenBucketBuilder(rate float64, capacity, initialStock uint64, clk Clock) LimiterBuilderFn

type LimiterStore

type LimiterStore func(string) Limiter

LimiterStore defines the interface for a limiter lookup function

func NewLimiterFromBackendAndBuilder

func NewLimiterFromBackendAndBuilder(backend Backend, limiterBuilder LimiterBuilderFn) LimiterStore

NewLimiterFromBackendAndBuilder creates a LimiterStore that uses limiterBuilder to creat new token buckets.

func NewLimiterStore

func NewLimiterStore(maxRate float64, capacity int, backend Backend) LimiterStore

NewLimiterStore returns a LimiterStore using the received backend for persistence

func NewMemoryStore deprecated

func NewMemoryStore(maxRate float64, capacity int) LimiterStore

NewMemoryStore returns a LimiterStore using the memory backend

Deprecated: Use NewLimiterStore instead

type MemoryBackend

type MemoryBackend struct {
	// contains filtered or unexported fields
}

MemoryBackend implements the backend interface by wrapping a sync.Map

func NewMemoryBackend

func NewMemoryBackend(ctx context.Context, ttl time.Duration) *MemoryBackend

func (*MemoryBackend) Load

func (m *MemoryBackend) Load(key string, f func() interface{}) interface{}

Load implements the Backend interface. The f function should always return a non nil value, or that nil value will be assigned and returned on load.

func (*MemoryBackend) Store

func (m *MemoryBackend) Store(key string, v interface{}) error

Store implements the Backend interface

type ShardedMemoryBackend

type ShardedMemoryBackend struct {
	// contains filtered or unexported fields
}

ShardedMemoryBackend is a memory backend shardering the data in order to avoid mutex contention

func DefaultShardedMemoryBackend

func DefaultShardedMemoryBackend(ctx context.Context) *ShardedMemoryBackend

DefaultShardedMemoryBackend is a 2048 sharded ShardedMemoryBackend

func NewShardedBackend

func NewShardedBackend(ctx context.Context, shards uint64, ttl time.Duration,
	cleanUpRate time.Duration, cleanUpThreads uint64, h Hasher, backendBuilder BackendBuilder,
) *ShardedMemoryBackend

func NewShardedMemoryBackend

func NewShardedMemoryBackend(ctx context.Context, shards uint64, ttl time.Duration, h Hasher) *ShardedMemoryBackend

NewShardedMemoryBackend returns a ShardedMemoryBackend with 'shards' shards

func (*ShardedMemoryBackend) Load

func (b *ShardedMemoryBackend) Load(key string, f func() interface{}) interface{}

Load implements the Backend interface

func (*ShardedMemoryBackend) Store

func (b *ShardedMemoryBackend) Store(key string, v interface{}) error

Store implements the Backend interface

type TokenBucket

type TokenBucket struct {
	// contains filtered or unexported fields
}

TokenBucket is an implementation of the token bucket pattern

func NewTokenBucket

func NewTokenBucket(rate float64, capacity uint64) *TokenBucket

NewTokenBucket returns a token bucket with the given rate and capacity, using the default clock and an initial stock of cap

func NewTokenBucketWithClock

func NewTokenBucketWithClock(rate float64, capacity uint64, c Clock) *TokenBucket

NewTokenBucketWithClock returns a token bucket with the given rate, capacity, and clock and an initial stock of capacity

func NewTokenBucketWithInitialStock

func NewTokenBucketWithInitialStock(r float64, capacity, i uint64, c Clock) *TokenBucket

NewTokenBucketWithInitialStock returns a token bucket with the given rate, capacity, clock and initial stock

func (*TokenBucket) Allow

func (t *TokenBucket) Allow() bool

Allow flags if the current request can be processed or not. It updates the internal state if the request can be processed

Directories

Path Synopsis
Package router provides several rate-limit routers.
Package router provides several rate-limit routers.
gin

Jump to

Keyboard shortcuts

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