KScript docs
EN

rate_limit fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

From KScript.Cache

These need <import package="KScript.Cache" />import KScript.Cache. See KScript.Cache.

#rate_limit->attempt(work: fn) → any KScript.Cache

Counts an attempt and runs work, returning what it returns. When the limit is used up, work doesn't run: it is a TooManyRequests error with retry_after, which kscript serve answers with 429.
ParameterTakes
work requiredfn
Example
<echo>{ @rate_limit('report', limit: 2, per: 1m)->attempt(fn() => 'built') }</echo>
echo @rate_limit('report', limit: 2, per: 1m)->attempt(fn() => 'built')

#rate_limit->available_in() → duration KScript.Cache

How long until attempts are allowed again, as a duration: 0s when some are left.
Example
<echo>{ @rate_limit('search', limit: 10, per: 1m)->available_in() }</echo>
echo @rate_limit('search', limit: 10, per: 1m)->available_in()

#rate_limit->clear() KScript.Cache

Forgets the attempts, such as after a successful sign-in.
Example
<do>@rate_limit('login:ann', limit: 5, per: 1m)->clear()</do>
@rate_limit('login:ann', limit: 5, per: 1m)->clear()

#rate_limit->hit() → number KScript.Cache

Counts an attempt without running anything, such as a failed sign-in, and returns how many are left.
Example
<echo>{ @rate_limit('login:ann', limit: 5, per: 1m)->hit() } left</echo>
echo "{ @rate_limit('login:ann', limit: 5, per: 1m)->hit() } left"

#rate_limit->is_limited() → bool KScript.Cache

True when the attempts are used up for now.
Example
<echo>{ @rate_limit('login:joan', limit: 5, per: 1m)->is_limited() }</echo>
echo @rate_limit('login:joan', limit: 5, per: 1m)->is_limited()

#rate_limit->remaining() → number KScript.Cache

How many attempts are left in this window.
Example
<echo>{ @rate_limit('search', limit: 10, per: 1m)->remaining() }</echo>
echo @rate_limit('search', limit: 10, per: 1m)->remaining()

Methods from any