Fortune for throttle's current commit: Half blessing − 半吉master
commit
07764d4deb
@ -0,0 +1 @@
|
|||||||
|
/target
|
@ -0,0 +1,75 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.123"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cb691a747a7ab48abc15c5b42066eaafde10dc427e3b6ee2a1cf43db04c763bd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "throttle"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.10.2+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "throttle"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.8.5"
|
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
mod prov;
|
||||||
|
mod stream;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
@ -0,0 +1,148 @@
|
|||||||
|
//! Throttle provider
|
||||||
|
use std::time::Duration;
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
|
pub trait DurationProvider
|
||||||
|
{
|
||||||
|
fn get_next_duration(&mut self) -> Option<Duration>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An iterator adaptor for a `DurationProvider`.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct DurationProviderIter<T: ?Sized>(T);
|
||||||
|
|
||||||
|
impl<T> DurationProviderIter<T>
|
||||||
|
{
|
||||||
|
/// Consume into the backing `DurationProvider`
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn into_inner(self) -> T
|
||||||
|
{
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized + DurationProvider> Iterator for DurationProviderIter<T>
|
||||||
|
{
|
||||||
|
type Item = Duration;
|
||||||
|
#[inline(always)]
|
||||||
|
fn next(&mut self) -> Option<Self::Item>
|
||||||
|
{
|
||||||
|
self.0.get_next_duration()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub trait DurationProviderIterExt<'a>
|
||||||
|
{
|
||||||
|
type Iter: iter::Iterator<Item=Duration> + 'a;
|
||||||
|
fn get_all_durations(self) -> Self::Iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait DurationProviderExt<'a>
|
||||||
|
{
|
||||||
|
fn get_all_durations_dyn(self: Box<Self>) -> Box<dyn iter::Iterator<Item=Duration> + 'a>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: DurationProvider + 'a> DurationProviderIterExt<'a> for T
|
||||||
|
{
|
||||||
|
type Iter = DurationProviderIter<Self>;
|
||||||
|
#[inline(always)]
|
||||||
|
fn get_all_durations(self) -> Self::Iter {
|
||||||
|
DurationProviderIter(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized + DurationProvider + 'a> DurationProviderExt<'a> for T
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn get_all_durations_dyn(self: Box<Self>) -> Box<dyn iter::Iterator<Item=Duration> + 'a>
|
||||||
|
{
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Iter<T: ?Sized>(Box<T>);
|
||||||
|
impl<T: DurationProvider+?Sized> Iterator for Iter<T>
|
||||||
|
{
|
||||||
|
type Item = Duration;
|
||||||
|
fn next(&mut self) -> Option<Self::Item>
|
||||||
|
{
|
||||||
|
self.0.get_next_duration()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Box::new(Iter(self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DurationProvider for Duration
|
||||||
|
{
|
||||||
|
#[inline(always)]
|
||||||
|
fn get_next_duration(&mut self) -> Option<Duration> {
|
||||||
|
Some(*self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait ThrottleProvider
|
||||||
|
{
|
||||||
|
type Timer: DurationProvider;
|
||||||
|
fn get_timeout(&self) -> Self::Timer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub trait DynThrottleProvider
|
||||||
|
{
|
||||||
|
fn get_timeout(&self) -> Box<dyn DurationProvider + '_>;
|
||||||
|
}
|
||||||
|
impl<T: ?Sized> DynThrottleProvider for T
|
||||||
|
where T: ThrottleProvider
|
||||||
|
{
|
||||||
|
#[inline(always)]
|
||||||
|
fn get_timeout(&self) -> Box<dyn DurationProvider + '_>
|
||||||
|
{
|
||||||
|
Box::new(ThrottleProvider::get_timeout(&self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized> ThrottleProvider for &'a T
|
||||||
|
where T: ThrottleProvider
|
||||||
|
{
|
||||||
|
type Timer = T::Timer;
|
||||||
|
#[inline(always)]
|
||||||
|
fn get_timeout(&self) -> Self::Timer {
|
||||||
|
T::get_timeout(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ThrottleProvider for Duration
|
||||||
|
{
|
||||||
|
type Timer = Self;
|
||||||
|
fn get_timeout(&self) -> Self::Timer {
|
||||||
|
self.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Provides a throttle adaptor that returns a uniformly-distributed `T` each time `get_timeout()` is called.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct UniformThrottleProvider<T: ?Sized, R: std::ops::RangeBounds<T>>(R, std::marker::PhantomData<T>);
|
||||||
|
|
||||||
|
impl<T: ?Sized, R> From<R> for UniformThrottleProvider<T, R>
|
||||||
|
where T: DurationProvider,
|
||||||
|
R: std::ops::RangeBounds<T>
|
||||||
|
{
|
||||||
|
#[inline(always)]
|
||||||
|
fn from(from: R) -> Self
|
||||||
|
{
|
||||||
|
Self(from, std::marker::PhantomData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, R> ThrottleProvider for UniformThrottleProvider<T, R>
|
||||||
|
where T: DurationProvider + rand::distributions::uniform::SampleUniform,
|
||||||
|
R: std::ops::RangeBounds<T>,
|
||||||
|
for <'r> &'r R: rand::distributions::uniform::SampleRange<T>,
|
||||||
|
{
|
||||||
|
type Timer = T;
|
||||||
|
#[inline]
|
||||||
|
fn get_timeout(&self) -> Self::Timer {
|
||||||
|
use rand::prelude::*;
|
||||||
|
rand::thread_rng().gen_range(&self.0)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
//! Stream adaptors
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct ThrottleAdaptor<R, T>
|
||||||
|
{
|
||||||
|
reader: R,
|
||||||
|
timeout_provider: T
|
||||||
|
}
|
Loading…
Reference in new issue