throttle_my_fn: A Rust attribute macro to throttle the execution of functions

License Crates.io docs.rs

throttle_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.

Usage

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

[throttle(10, Duration::from_secs(1))]

pub(crate) fn run10timespersecond(arg: &str) -> String { ... }

[throttle(1, Duration::from_millis(100))]

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.