Skip to content

Commit

Permalink
Merge pull request #222 from mammothbane/master
Browse files Browse the repository at this point in the history
no_std: spinning_top, portable-atomic
  • Loading branch information
antifuchs authored Feb 15, 2024
2 parents f0cb09c + c2cfc0b commit b8bc796
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions governor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ all_asserts = "2.2.0"
[features]
default = ["std", "dashmap", "jitter", "quanta"]
quanta = ["dep:quanta"]
std = ["no-std-compat/std", "nonzero_ext/std", "futures-timer", "futures"]
std = ["no-std-compat/std", "nonzero_ext/std", "futures-timer", "futures", "dep:parking_lot"]
jitter = ["rand"]
no_std = ["no-std-compat/compat_hash"]

[dependencies]
nonzero_ext = { version = "0.3.0", default-features = false }
parking_lot = "0.12.0"
parking_lot = { version = "0.12", optional = true }
spinning_top = "0.3"
portable-atomic = { version = "1.6", features = ["require-cas"] }
futures-timer = { version = "3.0.2", optional = true }
futures = { version = "0.3.5", optional = true }
rand = { version = "0.8.0", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion governor/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ use std::prelude::v1::*;
use std::convert::TryInto;
use std::fmt::Debug;
use std::ops::Add;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;

use portable_atomic::AtomicU64;

use crate::nanos::Nanos;

/// A measurement from a clock.
Expand Down
3 changes: 2 additions & 1 deletion governor/src/state/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::state::{NotKeyed, StateStore};
use std::fmt;
use std::fmt::Debug;
use std::num::NonZeroU64;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::time::Duration;

use portable_atomic::AtomicU64;

/// An in-memory representation of a GCRA's rate-limiting state.
///
/// Implemented using [`AtomicU64`] operations, this state representation can be used to
Expand Down
9 changes: 7 additions & 2 deletions governor/src/state/keyed/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ use std::collections::HashMap;
use std::hash::Hash;

use crate::state::keyed::ShrinkableKeyedStateStore;
use parking_lot::Mutex;

#[cfg(feature = "std")]
type Mutex<T> = parking_lot::Mutex<T>;

#[cfg(not(feature = "std"))]
type Mutex<T> = spinning_top::Spinlock<T>;

/// A thread-safe (but not very performant) implementation of a keyed rate limiter state
/// store using [`HashMap`].
Expand Down Expand Up @@ -66,7 +71,7 @@ where
{
/// Constructs a new rate limiter with a custom clock, backed by a [`HashMap`].
pub fn hashmap_with_clock(quota: Quota, clock: &C) -> Self {
let state: HashMapStateStore<K> = Mutex::new(HashMap::new());
let state: HashMapStateStore<K> = HashMapStateStore::new(HashMap::new());
RateLimiter::new(quota, state, clock)
}
}

0 comments on commit b8bc796

Please sign in to comment.