storelimit

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package storelimit is a rate limiter that keeps its counters in the Auth-All database. Two instances therefore share one count.

The limiter fails closed. It uses the database that sign-in uses, so a database failure fails sign-in anyway, and a fail-open limiter would only remove the bound at the worst moment.

Index

Constants

View Source
const Owner = "ratelimit"

Owner names the owner of the counter table.

Variables

This section is empty.

Functions

func IPKey

func IPKey(raw string) string

IPKey returns the counted address of one client address.

An IPv6 address counts per /64 block, because one customer owns a whole block. An IPv4-mapped address counts as the IPv4 address.

func Subject

func Subject(scope ratelimit.Scope, k ratelimit.Key) string

Subject returns the counted subject of one key in one scope. It returns an empty value when the key carries no subject of the scope.

func Table

func Table(o schema.Options) schema.Table

Table returns the counter table for the physical schema options.

func Units

func Units(o schema.Options) ([]schema.Unit, error)

Units returns the migration units of the counter table.

Types

type Limiter

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

Limiter counts the attempts of every rule in the store.

func New

func New(s store.Store, rules []ratelimit.Rule, opts ...Option) (*Limiter, error)

New returns a limiter that keeps the counters of the rules in s.

The store must implement store.RateLimitCounter. Every first-party SQL store does.

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, k ratelimit.Key) (bool, error)

Allow implements ratelimit.Limiter.

func (*Limiter) Cleanup

func (l *Limiter) Cleanup(ctx context.Context, before time.Time) (int, error)

Cleanup removes every counter whose window ended before the given time.

func (*Limiter) Decide

func (l *Limiter) Decide(ctx context.Context, k ratelimit.Key) (ratelimit.Decision, error)

Decide implements ratelimit.Decider. Every rule of the operation counts the attempt, and the request fails when any rule is over its limit.

func (*Limiter) SchemaTables

func (l *Limiter) SchemaTables(o schema.Options) []schema.Table

SchemaTables implements the schema contribution of a limiter.

func (*Limiter) SchemaUnits

func (l *Limiter) SchemaUnits(o schema.Options) ([]schema.Unit, error)

SchemaUnits implements the schema contribution of a limiter.

type Option

type Option func(*Limiter)

Option configures the limiter.

func WithClock

func WithClock(now func() time.Time) Option

WithClock replaces the clock. A test uses it.

Jump to

Keyboard shortcuts

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