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.
The primary use-case for this attribute macro is rate-limiting, e.g. to avoid hammering an online service.
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.