ratelimit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ratelimit enforces Seznam's published limits across processes.

Agents usually call a CLI in a loop, starting a new process per URL, so an in-memory limiter would protect nothing. Call timestamps live in a small state file keyed by a fingerprint of the API key (never the key itself).

State is best effort: if it cannot be read or written, szwm falls back to an in-process limiter rather than failing the API call.

Index

Constants

View Source
const (
	PerSecond     = 5
	PerMinute     = 100
	ReindexPerDay = 500
	QuotaTimezone = "Europe/Prague"
)

Published limits, per user and per site.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ledger

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

Ledger counts successful reindex requests per day.

func NewLedger

func NewLedger(store Store, keyID string) *Ledger

NewLedger returns the ledger for one key fingerprint.

func (*Ledger) Add

func (l *Ledger) Add(n int) error

Add records n submitted requests and drops older days.

func (*Ledger) Status

func (l *Ledger) Status() Quota

Status returns today's estimate.

type Limiter

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

Limiter is a sliding-window limiter shared between processes.

func NewLimiter

func NewLimiter(store Store, keyID string) *Limiter

NewLimiter returns a limiter for one key fingerprint.

func (*Limiter) Wait

func (l *Limiter) Wait(ctx context.Context) error

Wait blocks until one more call fits both windows, then records it.

type Quota

type Quota struct {
	Day       string `json:"day"`
	Timezone  string `json:"timezone"`
	Limit     int    `json:"limit"`
	Used      int    `json:"used_estimate"`
	Remaining int    `json:"remaining_estimate"`
	Estimated bool   `json:"estimated"`
}

Quota is a snapshot of the local reindex ledger.

The API exposes no counter, so this is an estimate of calls made from this machine. Seznam does not document when "a day" starts; szwm assumes midnight in Europe/Prague.

type Store

type Store struct {
	Dir string
	Now func() time.Time
}

Store is the on-disk state location.

Jump to

Keyboard shortcuts

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