Rate-Limit-Macro

Introduction

rate-limit-macro is a procedural macro that provides a simple way to rate-limit blocks of code. This crate is part of the logdna-agent-v2 project and is located under common/misc/rate-limit/macro.

Installation

Add the following line to your Cargo.toml under [dependencies]:

toml rate-limit-macro = "0.1.0"

Usage

Basic Usage

Here's a simple example:

```rust use ratelimitmacro::rate_limit;

let mut called_times = 0;

for _ in 0..10 { ratelimit!(rate = 5, interval = 1, { calledtimes += 1; }); }

// Only 5 calls should have been allowed due to rate limiting. asserteq!(calledtimes, 5); ```

With Fallback Block

You can also provide an optional fallback block that will be executed when the rate limit is exceeded:

```rust let mut calledtimes = 0; let mut fallbackcalled_times = 0;

for _ in 0..10 { ratelimit!(rate = 5, interval = 1, { calledtimes += 1; }, { fallbackcalledtimes += 1; }); }

// Check that the number of rate-limited calls and fallback calls add up to the total calls. asserteq!(calledtimes + fallbackcalledtimes, 10); ```

API Documentation

Notes

Related Crates

Source Code

The source code for this crate is part of the logdna-agent-v2 GitHub repository.

License

This crate is licensed under the MIT License.

Authors

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.