Use 10us sleep instead of 1ms sleep (#1453)

This commit is contained in:
Gary Yu 2018-09-01 01:20:59 +08:00 committed by Ignotus Peverell
parent 2f41003f90
commit 8a39f1fc75
3 changed files with 22 additions and 16 deletions

View file

@ -82,7 +82,12 @@ impl<'a> Message<'a> {
while written < len { while written < len {
let read_len = cmp::min(8000, len - written); let read_len = cmp::min(8000, len - written);
let mut buf = vec![0u8; read_len]; let mut buf = vec![0u8; read_len];
read_exact(&mut self.conn, &mut buf[..], 10000, true)?; read_exact(
&mut self.conn,
&mut buf[..],
time::Duration::from_secs(10),
true,
)?;
writer.write_all(&mut buf)?; writer.write_all(&mut buf)?;
written += read_len; written += read_len;
} }
@ -117,13 +122,13 @@ impl<'a> Response<'a> {
let mut msg = let mut msg =
ser::ser_vec(&MsgHeader::new(self.resp_type, self.body.len() as u64)).unwrap(); ser::ser_vec(&MsgHeader::new(self.resp_type, self.body.len() as u64)).unwrap();
msg.append(&mut self.body); msg.append(&mut self.body);
write_all(&mut self.conn, &msg[..], 10000)?; write_all(&mut self.conn, &msg[..], time::Duration::from_secs(10))?;
if let Some(mut file) = self.attachment { if let Some(mut file) = self.attachment {
let mut buf = [0u8; 8000]; let mut buf = [0u8; 8000];
loop { loop {
match file.read(&mut buf[..]) { match file.read(&mut buf[..]) {
Ok(0) => break, Ok(0) => break,
Ok(n) => write_all(&mut self.conn, &buf[..n], 10000)?, Ok(n) => write_all(&mut self.conn, &buf[..n], time::Duration::from_secs(10))?,
Err(e) => return Err(From::from(e)), Err(e) => return Err(From::from(e)),
} }
} }

View file

@ -112,11 +112,11 @@ fn max_msg_size(msg_type: Type) -> u64 {
pub fn read_exact( pub fn read_exact(
conn: &mut TcpStream, conn: &mut TcpStream,
mut buf: &mut [u8], mut buf: &mut [u8],
timeout: u32, timeout: time::Duration,
block_on_empty: bool, block_on_empty: bool,
) -> io::Result<()> { ) -> io::Result<()> {
let sleep_time = time::Duration::from_millis(1); let sleep_time = time::Duration::from_micros(10);
let mut count = 0; let mut count = time::Duration::new(0, 0);
let mut read = 0; let mut read = 0;
loop { loop {
@ -137,7 +137,7 @@ pub fn read_exact(
} }
if !buf.is_empty() { if !buf.is_empty() {
thread::sleep(sleep_time); thread::sleep(sleep_time);
count += 1; count += sleep_time;
} else { } else {
break; break;
} }
@ -152,9 +152,9 @@ pub fn read_exact(
} }
/// Same as `read_exact` but for writing. /// Same as `read_exact` but for writing.
pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: u32) -> io::Result<()> { pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: time::Duration) -> io::Result<()> {
let sleep_time = time::Duration::from_millis(1); let sleep_time = time::Duration::from_micros(10);
let mut count = 0; let mut count = time::Duration::new(0, 0);
while !buf.is_empty() { while !buf.is_empty() {
match conn.write(buf) { match conn.write(buf) {
@ -171,7 +171,7 @@ pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: u32) -> io::Result<(
} }
if !buf.is_empty() { if !buf.is_empty() {
thread::sleep(sleep_time); thread::sleep(sleep_time);
count += 1; count += sleep_time;
} else { } else {
break; break;
} }
@ -191,9 +191,9 @@ pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: u32) -> io::Result<(
pub fn read_header(conn: &mut TcpStream, msg_type: Option<Type>) -> Result<MsgHeader, Error> { pub fn read_header(conn: &mut TcpStream, msg_type: Option<Type>) -> Result<MsgHeader, Error> {
let mut head = vec![0u8; HEADER_LEN as usize]; let mut head = vec![0u8; HEADER_LEN as usize];
if Some(Type::Hand) == msg_type { if Some(Type::Hand) == msg_type {
read_exact(conn, &mut head, 10, true)?; read_exact(conn, &mut head, time::Duration::from_millis(10), true)?;
} else { } else {
read_exact(conn, &mut head, 10000, false)?; read_exact(conn, &mut head, time::Duration::from_secs(10), false)?;
} }
let header = ser::deserialize::<MsgHeader>(&mut &head[..])?; let header = ser::deserialize::<MsgHeader>(&mut &head[..])?;
let max_len = max_msg_size(header.msg_type); let max_len = max_msg_size(header.msg_type);
@ -215,7 +215,7 @@ where
T: Readable, T: Readable,
{ {
let mut body = vec![0u8; h.msg_len as usize]; let mut body = vec![0u8; h.msg_len as usize];
read_exact(conn, &mut body, 20000, true)?; read_exact(conn, &mut body, time::Duration::from_secs(20), true)?;
ser::deserialize(&mut &body[..]).map_err(From::from) ser::deserialize(&mut &body[..]).map_err(From::from)
} }

View file

@ -17,6 +17,7 @@ use std::fs::File;
use std::io::{self, BufWriter}; use std::io::{self, BufWriter};
use std::net::{SocketAddr, TcpStream}; use std::net::{SocketAddr, TcpStream};
use std::sync::Arc; use std::sync::Arc;
use std::time;
use conn::{Message, MessageHandler, Response}; use conn::{Message, MessageHandler, Response};
use core::core::{self, hash::Hash, CompactBlock}; use core::core::{self, hash::Hash, CompactBlock};
@ -317,7 +318,7 @@ impl MessageHandler for Protocol {
fn headers_header_size(conn: &mut TcpStream, msg_len: u64) -> Result<u64, Error> { fn headers_header_size(conn: &mut TcpStream, msg_len: u64) -> Result<u64, Error> {
let mut size = vec![0u8; 2]; let mut size = vec![0u8; 2];
// read size of Vec<BlockHeader> // read size of Vec<BlockHeader>
read_exact(conn, &mut size, 20000, true)?; read_exact(conn, &mut size, time::Duration::from_millis(10), true)?;
let total_headers = size[0] as u64 * 256 + size[1] as u64; let total_headers = size[0] as u64 * 256 + size[1] as u64;
if total_headers == 0 || total_headers > 10_000 { if total_headers == 0 || total_headers > 10_000 {
@ -391,7 +392,7 @@ fn headers_streaming_body(
// 3rd part // 3rd part
let mut read_body = vec![0u8; read_size as usize]; let mut read_body = vec![0u8; read_size as usize];
if read_size > 0 { if read_size > 0 {
read_exact(conn, &mut read_body, 20000, true)?; read_exact(conn, &mut read_body, time::Duration::from_secs(20), true)?;
*total_read += read_size; *total_read += read_size;
} }
body.append(&mut read_body); body.append(&mut read_body);