throttle_my_fn
: A Rust attribute macro to throttle the execution of functions
throttle_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.
Usage
Add the dependency to your Cargo.toml
:
[]
= "0.2"
Or, using cargo add
:
Include the macro:
use throttle;
Annotate the functions you want to throttle:
pub
pub
Note that the function signatures are modified to wrap the return type in an Option
,
like so:
pub
pub
The Option<T>
returned signifies whether the function executed or not.
Changelog
-
0.2.3
- Add this Changelog section to the README.
-
0.2.2
- Documentation updates regarding thread-safety.
-
0.2.1
- Documentation update about possible use cases for this crate.
- Thanks go to
@not-matthias
from the Rust Linz Discord server for feedback.
-
0.2.0
- Replace the use of
MaybeUninit
andAtomicBool
withparking_lot::Mutex
andparking_lot::const_mutex
. - Thanks go to
@mejrs
and@veber-alex
from the Rust Discord server for feedback.
- Replace the use of
-
0.1.0
- Initial release