Skip to content

Commit

Permalink
make pub
Browse files Browse the repository at this point in the history
  • Loading branch information
lytefast committed Jun 12, 2022
1 parent d8e711a commit fa617b6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Sam Shih

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# GCRA: A basic implementation

This library implements the core GCRA functionality in rust.

## Usage

```rust
use ::gcra::{GcraState, RateLimit};

fn check_rate_limit() {
const LIMIT: u32 = 1;
const PERIOD: Duration = Duration::from_secs(1);
// Create a rate limit that allows `5/1s`
let rate_limit = RateLimit::new(LIMIT, PERIOD);

let mut user_state = GcraState::default();
assert!(user_state.check_and_modify(&rate_limit, 1).is_ok());
assert!(
!assert!(user_state.check_and_modify(&rate_limit, 1).is_err(),
"We should be over the limit now"
);
}
```
34 changes: 34 additions & 0 deletions examples/rate_limit_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use gcra::{GcraState, RateLimit};

fn check_rate_limit(rate_limit: &RateLimit, gcra_state: &mut GcraState) -> bool {
const COST: u32 = 1;
match gcra_state.check_and_modify(rate_limit, COST) {
Ok(_) => {
println!("allowed");
true
}
Err(next_allowed_at) => {
println!("denied. Try again at {:?}", next_allowed_at);
false
}
}
}

fn main() {
const LIMIT: u32 = 3;
// Create a rate limit that allows `3/1s`
let rate_limit = RateLimit::per_sec(LIMIT);

let mut user_state = GcraState::default();
for i in 0..LIMIT {
assert!(
check_rate_limit(&rate_limit, &mut user_state),
"Attempt #{} should be allowed",
i + 1
);
}
assert!(
!check_rate_limit(&rate_limit, &mut user_state),
"We should be over the limit now"
);
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod gcra;
mod rate_limit;

pub use crate::gcra::GcraState;
pub use crate::rate_limit::RateLimit;

0 comments on commit fa617b6

Please sign in to comment.