limiter

package module
v0.0.0-...-f1f0b2f Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 21 Imported by: 0

README

API Rate Limit with DynamoDB

TEST

$ make local-start
$ make test

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRateLimitExceeded = errors.New("API rate limit exceeded")
	ErrInternal          = errors.New("internal API error")
	ErrInvalidBucketID   = errors.New("invalid bucket id")
)
View Source
var DefaultInterval = 60 * time.Second

Functions

func CreateTable

func CreateTable(ctx context.Context, tableName string, client *dynamodb.Client) error

Types

type AWS

type AWS struct {
	CloudWatchMetrics []CloudWatchMetrics
	Timestamp         int64 `json:"Timestamp"`
}

type AdditionalConfig

type AdditionalConfig struct {
	NegativeCache *NegativeCacheConfig `json:"negative_cache,omitempty"`
	Anonymous     *AnonymousConfig     `json:"anonymous,omitempty"`
}

type AnonymousConfig

type AnonymousConfig struct {
	RecordTTL time.Duration `json:"record_ttl,"`
}

AnonymousConfig allows not registered bucket id. This is used for such as an IP address basis throttling.

type CloudWatchMetrics

type CloudWatchMetrics struct {
	Namespace  string     `json:"Namespace"`
	Dimensions [][]string `json:"Dimensions"`
	Metrics    []Metric   `json:"Metrics"`
}

type Config

type Config struct {
	TableName        string            `json:"table_name"`
	TokenPerInterval int64             `json:"token_per_interval"`
	BucketSize       int64             `json:"bucket_size"`
	Interval         time.Duration     `json:"interval,omitempty"`
	AdditionalConfig *AdditionalConfig `json:"additional,omitempty"`
}

func NewConfig

func NewConfig(r io.Reader) (*Config, error)

type EMF

type EMF struct {
	AWS       AWS    `json:"_aws"`
	TableName string `json:"TableName"`
	BucketID  string `json:"BucketID"`
	ShardID   string `json:"ShardID"`
	Operation string `json:"Operation,omitempty"`
	Throttle  int    `json:"Throttle,omitempty"`
	RequestID string `json:"requestId"`
}

type LimitPreparer

type LimitPreparer interface {
	Limiter
	Preparer
}

type Limiter

type Limiter interface {
	ShouldThrottle(context.Context, string) (bool, error)
}

type Metric

type Metric struct {
	Name string `json:"Name"`
	Unit string `json:"Unit"`
}

type NegativeCacheConfig

type NegativeCacheConfig struct {
	Size int `json:"size"`
}

WithNegativeCache configures negative cache entry size for invalid bucket ids. specifying 0 means to disable cache

type Opt

type Opt func(rl *RateLimit)

func WithEMFMetrics

func WithEMFMetrics(w io.Writer) Opt

func WithLogger

func WithLogger(logger *slog.Logger) Opt

type Preparer

type Preparer interface {
	PrepareTokens(context.Context, string) error
}

type RateLimit

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

func New

func New(cfg *Config, client DDBClient, opts ...Opt) (*RateLimit, error)

func (*RateLimit) PrepareTokens

func (l *RateLimit) PrepareTokens(ctx context.Context, bucketID string) (err error)

func (*RateLimit) ShouldThrottle

func (l *RateLimit) ShouldThrottle(ctx context.Context, bucketID string) (bool, error)

ShouldThrottle return throttled and an error. If throttle is true, it means tokens run out. If an error is ErrRateLimitExceeded, DynamoDB API rate limit exceeded.

type TokenBucket

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

Directories

Path Synopsis
Package mock_limiter is a generated GoMock package.
Package mock_limiter is a generated GoMock package.

Jump to

Keyboard shortcuts

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