Documentation
¶
Overview ¶
Package ratelimit counts requests in fixed time windows, stored in Postgres so every API instance shares the counts.
A limit of "10 per hour" counts per clock hour. Near the boundary of two windows up to twice the limit can get through in a short burst: the trade for a single atomic statement per check.
Index ¶
- func Email(email string) string
- func IP(ip *netip.Addr) string
- type Limiter
- func (l *Limiter) DeleteEnded(ctx context.Context) (int64, error)
- func (l *Limiter) Hit(ctx context.Context, rule Rule, subject string) (ok bool, retryAfter time.Duration, err error)
- func (l *Limiter) Reached(ctx context.Context, rule Rule, subject string) (reached bool, retryAfter time.Duration, err error)
- type Rule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter counts requests against rules.
func New ¶
New returns a Limiter using conn. now supplies the current time; pass time.Now outside tests.
func (*Limiter) DeleteEnded ¶
DeleteEnded removes the counters of windows that have ended, and returns how many were removed.
func (*Limiter) Hit ¶
func (l *Limiter) Hit(ctx context.Context, rule Rule, subject string) (ok bool, retryAfter time.Duration, err error)
Hit counts one more request by subject under rule, and reports whether it's within the limit. Over the limit, retryAfter is how long until the window ends.
func (*Limiter) Reached ¶
func (l *Limiter) Reached(ctx context.Context, rule Rule, subject string) (reached bool, retryAfter time.Duration, err error)
Reached reports whether subject has already used up rule's limit, without counting a request. It's for limits on failures, which are counted with Hit only once a request has failed.