-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use std::{ | ||
io::{Error as IOError, ErrorKind as IOErrorKind, Read, Result as IOResult}, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
pub(super) struct BreakSlow<R: Read> { | ||
source: R, | ||
low_speed_duration: Duration, | ||
low_speed_bytes_per_second: usize, | ||
read_bytes_in_this_second: usize, | ||
this_seconds_starts_at: Option<Instant>, | ||
slow_duration: Duration, | ||
} | ||
|
||
impl<R: Read> BreakSlow<R> { | ||
pub(super) fn new( | ||
source: R, | ||
low_speed_duration: Duration, | ||
low_speed_bytes_per_second: usize, | ||
) -> Self { | ||
Self { | ||
source, | ||
low_speed_bytes_per_second, | ||
low_speed_duration, | ||
read_bytes_in_this_second: Default::default(), | ||
this_seconds_starts_at: Default::default(), | ||
slow_duration: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<R: Read> Read for BreakSlow<R> { | ||
fn read(&mut self, buf: &mut [u8]) -> IOResult<usize> { | ||
let have_read = self.source.read(buf)?; | ||
if self.low_speed_bytes_per_second > 0 { | ||
if let Some(this_seconds_starts_at) = self.this_seconds_starts_at { | ||
self.read_bytes_in_this_second += have_read; | ||
if this_seconds_starts_at.elapsed() >= Duration::from_secs(1) { | ||
if self.read_bytes_in_this_second < self.low_speed_bytes_per_second { | ||
self.slow_duration += Duration::from_secs(1); | ||
if self.slow_duration >= self.low_speed_duration { | ||
return Err(IOError::new(IOErrorKind::TimedOut, "Slow Response")); | ||
} | ||
} else { | ||
self.slow_duration = Default::default(); | ||
} | ||
self.this_seconds_starts_at = Some(Instant::now()); | ||
self.read_bytes_in_this_second = 0; | ||
} | ||
} else { | ||
self.this_seconds_starts_at = Some(Instant::now()); | ||
} | ||
} | ||
Ok(have_read) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use rand::{thread_rng, RngCore}; | ||
use std::{ | ||
error::Error, | ||
io::{copy as io_copy, sink}, | ||
thread::sleep, | ||
}; | ||
|
||
struct SlowCopyIO<R: Read> { | ||
source: R, | ||
copied: usize, | ||
sleep_when_copied: usize, | ||
sleep_duration: Duration, | ||
} | ||
|
||
impl<R: Read> SlowCopyIO<R> { | ||
fn new(source: R, sleep_when_copied: usize, sleep_duration: Duration) -> Self { | ||
Self { | ||
source, | ||
sleep_duration, | ||
sleep_when_copied, | ||
copied: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<R: Read> Read for SlowCopyIO<R> { | ||
fn read(&mut self, buf: &mut [u8]) -> IOResult<usize> { | ||
let have_read = self.source.read(buf)?; | ||
self.copied += have_read; | ||
if self.copied > self.sleep_when_copied { | ||
self.copied = 0; | ||
sleep(self.sleep_duration); | ||
} | ||
Ok(have_read) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_break_slow_io() -> Result<(), Box<dyn Error>> { | ||
let rand_source = Box::new(thread_rng()) as Box<dyn RngCore>; | ||
let mut r = BreakSlow::new( | ||
SlowCopyIO::new( | ||
rand_source.take(5 * (1 << 20)), | ||
100_000, | ||
Duration::from_millis(200), | ||
), | ||
Duration::from_secs(5), | ||
1 << 20, | ||
); | ||
let err = io_copy(&mut r, &mut sink()).unwrap_err(); | ||
assert_eq!(err.kind(), IOErrorKind::TimedOut); | ||
assert_eq!(&err.to_string(), "Slow Response"); | ||
|
||
let rand_source = Box::new(thread_rng()) as Box<dyn RngCore>; | ||
let mut r = BreakSlow::new( | ||
SlowCopyIO::new( | ||
rand_source.take(5 * (1 << 20)), | ||
100_000, | ||
Duration::from_millis(50), | ||
), | ||
Duration::from_secs(5), | ||
1 << 20, | ||
); | ||
io_copy(&mut r, &mut sink())?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
//! 负责下载完整或部分七牛对象 | ||
mod base; | ||
mod breakslow; | ||
mod config; | ||
mod download; | ||
mod host_selector; | ||
|