throttle_my_fn
: A Rust attribute macro to throttle the execution of functionsthrottle_my_fn
is a Rust attribute macro to limit a function's number of runs over a
specified period of time, even when called from multiple threads.
The primary use-case for this attribute macro is rate-limiting, e.g. to avoid hammering an online service or to avoid serving too many requests over a period of time.
The macro works by rewriting the function and prefixing it with the necessary book-keeping
for throttling (see Usage
below). The resulting function is thread-safe.
Add the dependency to your Cargo.toml
:
toml
[dependencies]
throttle_my_fn = "0.2"
Or, using cargo add
:
sh
$ cargo add throttle_my_fn
Include the macro:
rust
use throttle_my_fn::throttle;
Annotate the functions you want to throttle:
```rust
pub(crate) fn run10timespersecond(arg: &str) -> String { ... }
pub(crate) fn runonceper100milliseconds(arg: &str) -> String { ... } ```
Note that the function signatures are modified to wrap the return type in an Option
,
like so:
```rust
pub(crate) fn run10timespersecond(arg: &str) -> Option
pub(crate) fn runonceper100milliseconds(arg: &str) -> Option
The Option<T>
returned signifies whether the function executed or not.
0.2.3
0.2.2
0.2.1
@not-matthias
from the Rust Linz Discord server for feedback.0.2.0
MaybeUninit
and AtomicBool
with parking_lot::Mutex
and
parking_lot::const_mutex
.@mejrs
and @veber-alex
from the Rust Discord server for feedback.0.1.0