From ff1e6ad91c2d2dea52a4f670a96c1023c2158070 Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Sat, 7 Sep 2019 18:44:24 +0800 Subject: [PATCH 1/3] fix: the cpuload 100% in case the tcpstream WouldBlock --- p2p/src/conn.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/p2p/src/conn.rs b/p2p/src/conn.rs index 534a1b9dde..b284b0916c 100644 --- a/p2p/src/conn.rs +++ b/p2p/src/conn.rs @@ -330,7 +330,14 @@ where .spawn(move || { loop { // check the read end - match try_break!(read_header(&mut reader, version)) { + let res = read_header(&mut reader, version); + if let Err(Error::Connection(ref e)) = res { + if e.kind() == io::ErrorKind::WouldBlock { + // to avoid the heavy polling which will consume CPU 100% + thread::sleep(Duration::from_millis(10)); + } + } + match try_break!(res) { Some(MsgHeaderWrapper::Known(header)) => { let msg = Message::from_header(header, &mut reader, version); @@ -375,7 +382,7 @@ where })?; let writer_thread = thread::Builder::new() - .name("peer_read".to_string()) + .name("peer_write".to_string()) .spawn(move || { let mut retry_send = Err(()); loop { From 7d9f47b438eed8eec1ff65e7133e01829d76254e Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Sat, 7 Sep 2019 19:30:17 +0800 Subject: [PATCH 2/3] extending the sleep on WouldBlock to all read/write --- p2p/src/conn.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/p2p/src/conn.rs b/p2p/src/conn.rs index b284b0916c..1130d77ade 100644 --- a/p2p/src/conn.rs +++ b/p2p/src/conn.rs @@ -58,11 +58,12 @@ macro_rules! try_break { ($inner:expr) => { match $inner { Ok(v) => Some(v), - Err(Error::Connection(ref e)) - if e.kind() == io::ErrorKind::WouldBlock || e.kind() == io::ErrorKind::TimedOut => - { + Err(Error::Connection(ref e)) if e.kind() == io::ErrorKind::TimedOut => None, + Err(Error::Connection(ref e)) if e.kind() == io::ErrorKind::WouldBlock => { + // to avoid the heavy polling which will consume CPU 100% + thread::sleep(Duration::from_millis(10)); None - } + } Err(Error::Store(_)) | Err(Error::Chain(_)) | Err(Error::Internal) @@ -330,14 +331,7 @@ where .spawn(move || { loop { // check the read end - let res = read_header(&mut reader, version); - if let Err(Error::Connection(ref e)) = res { - if e.kind() == io::ErrorKind::WouldBlock { - // to avoid the heavy polling which will consume CPU 100% - thread::sleep(Duration::from_millis(10)); - } - } - match try_break!(res) { + match try_break!(read_header(&mut reader, version)) { Some(MsgHeaderWrapper::Known(header)) => { let msg = Message::from_header(header, &mut reader, version); @@ -401,7 +395,7 @@ where } debug!( - "Shutting down reader connection with {}", + "Shutting down writer connection with {}", writer .peer_addr() .map(|a| a.to_string()) From 743b5adab1c39a3611d806347c91a3bbc3e507b9 Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Sat, 7 Sep 2019 19:31:01 +0800 Subject: [PATCH 3/3] rustfmt --- p2p/src/conn.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/src/conn.rs b/p2p/src/conn.rs index 1130d77ade..563521de21 100644 --- a/p2p/src/conn.rs +++ b/p2p/src/conn.rs @@ -63,7 +63,7 @@ macro_rules! try_break { // to avoid the heavy polling which will consume CPU 100% thread::sleep(Duration::from_millis(10)); None - } + } Err(Error::Store(_)) | Err(Error::Chain(_)) | Err(Error::Internal)