Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merges #201 with current master, adds support for development on Windows #233

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions governor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@ harness = false
[lib]
bench = false

[dev-dependencies]
criterion = {version = "0.5.1", features = ["html_reports"]}
tynm = "0.1.4"
crossbeam = "0.8.0"
libc = "0.2.70"
futures = "0.3.5"
proptest = "1.0.0"
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", "dep:parking_lot"]
std = [
"no-std-compat/std",
"nonzero_ext/std",
"futures-timer",
"futures",
"dep:parking_lot",
]
jitter = ["rand"]
no_std = ["no-std-compat/compat_hash"]

Expand All @@ -51,8 +48,32 @@ futures = { version = "0.3.5", optional = true }
rand = { version = "0.8.0", optional = true }
dashmap = { version = "5.1.0", optional = true }
quanta = { version = "0.12.0", optional = true }
no-std-compat = { version = "0.4.1", features = [ "alloc" ] }
no-std-compat = { version = "0.4.1", features = ["alloc"] }
cfg-if = "1.0"

# To ensure we don't pull in vulnerable smallvec, see https://github.com/antifuchs/governor/issues/60
smallvec = "1.6.1"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
tynm = "0.1.4"
crossbeam = "0.8.0"
futures = "0.3.5"
proptest = "1.0.0"
all_asserts = "2.2.0"

[target.'cfg(unix)'.dev-dependencies]
libc = "0.2.70"

[target.'cfg(windows)'.dev-dependencies]
windows = { version = "0.56.0", features = ["System_Diagnostics"] }

# Due to #![deny(warnings)] you can't compile the crate without these lints disabled.
# These are simple to resolve lints, it's up to Andreas Fuchs if they should be resolved.
#
# -- koshell
[lints.clippy]
default_constructed_unit_structs = "allow"
single_char_pattern = "allow"
len_zero = "allow"
clone_on_copy = "allow"
Comment on lines +71 to +79
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'd prefer to resolve warning-scale lints than to ignore them (unless resolving them is extremely noisy or leads to API clutter). Where do you see these trigger? I tried running clippy from stable rust 1.79.0, and it doesn't warn (with the clippy fixes from latest HEAD).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antifuchs Looked over the master branch to confirm if it still produced clippy warnings.

#235 is a list of the warnings it spits out for me. I went through and tagged where in the code each warning originated instead of in the Cargo.toml to make it easier to review.

I left some suggestions in the pull request to resolve the lints.

25 changes: 21 additions & 4 deletions governor/src/clock/with_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,32 @@ mod test {

#[test]
fn system_clock_impls_coverage() {
let one_ns = Nanos::new(1);
const NS: u64 = {
// SystemTime precision is OS-specific.
// For Windows it is only precise to 100ns.
//
// As far as I'm aware unix systems are typically precise to 1ns.
//
// See: https://doc.rust-lang.org/std/time/struct.SystemTime.html
//
// -- koshell
if cfg!(windows) {
100
} else {
1
}
};

let ns = Nanos::new(NS);
let c = SystemClock::default();
let now = c.now();
assert_ne!(now + one_ns, now);

assert_ne!(now + ns, now);
// Thankfully, we're not comparing two system clock readings
// here so that ought to be safe, I think:
assert_eq!(one_ns, Reference::duration_since(&(now + one_ns), now));
assert_eq!(ns, Reference::duration_since(&(now + ns), now));
assert_eq!(
Reference::saturating_sub(&(now + Duration::from_nanos(1)), one_ns),
Reference::saturating_sub(&(now + Duration::from_nanos(NS)), ns),
now
);
}
Expand Down
39 changes: 39 additions & 0 deletions governor/src/gcra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ impl Gcra {
}
}))
}

/// Consumes the `Gcra` and returns `Quota`.
pub(crate) fn to_quota(&self) -> Quota {
let max_burst = self.tau / self.t;
Quota {
max_burst: NonZeroU32::new(max_burst as u32).expect("max_burst expect u32 value"),
replenish_1_per: Duration::from_nanos(self.t.as_u64()),
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -224,6 +233,7 @@ mod test {

#[derive(Debug)]
struct Count(NonZeroU32);

impl Arbitrary for Count {
type Parameters = ();
fn arbitrary_with(_args: ()) -> Self::Strategy {
Expand Down Expand Up @@ -253,4 +263,33 @@ mod test {
assert_eq!(quota, back);
})
}

#[test]
fn test_to_quota() {
use nonzero_ext::nonzero;

let quota = Quota {
max_burst: nonzero!(32_u32),
replenish_1_per: Duration::from_secs(3),
};
let gcra = Gcra::new(quota);
let expect = gcra.to_quota();
assert_eq!(quota, expect);

let quota = Quota {
max_burst: nonzero!(94_u32),
replenish_1_per: Duration::from_millis(310),
};
let gcra = Gcra::new(quota);
let expect = gcra.to_quota();
assert_eq!(quota, expect);

let quota = Quota {
max_burst: nonzero!(1016_u32),
replenish_1_per: Duration::from_micros(30310),
};
let gcra = Gcra::new(quota);
let expect = gcra.to_quota();
assert_eq!(quota, expect);
}
}
5 changes: 5 additions & 0 deletions governor/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ where
pub fn clock(&self) -> &C {
&self.clock
}

/// Consumes the `RateLimiter` and returns `Quota`.
pub fn quota(&self) -> Quota {
self.gcra.to_quota()
}
}

impl<K, S, C, MW> RateLimiter<K, S, C, MW>
Expand Down
27 changes: 23 additions & 4 deletions governor/tests/memory_leaks.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
#![cfg(feature = "std")]

// This test uses procinfo, so can only be run on Linux.
#[cfg(unix)]
extern crate libc;

use all_asserts::*;
use governor::{Quota, RateLimiter};
use nonzero_ext::*;
use std::convert::TryInto;
use std::sync::Arc;
use std::thread;

fn resident_memory_size() -> i64 {
let mut out: libc::rusage = unsafe { std::mem::zeroed() };
assert!(unsafe { libc::getrusage(libc::RUSAGE_SELF, &mut out) } == 0);
out.ru_maxrss
#[cfg(unix)]
{
let mut out: libc::rusage = unsafe { std::mem::zeroed() };
assert!(unsafe { libc::getrusage(libc::RUSAGE_SELF, &mut out) } == 0);
out.ru_maxrss
}

#[cfg(windows)]
{
(windows::System::Diagnostics::ProcessDiagnosticInfo::GetForCurrentProcess()
.unwrap()
.MemoryUsage()
.unwrap()
.GetReport()
.unwrap()
.WorkingSetSizeInBytes()
.unwrap()
/ 1024) // B => KiB
.try_into()
.unwrap()
}
}

const LEAK_TOLERANCE: i64 = 1024 * 1024 * 10;
Expand Down