Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit 4784876

Browse files
authored
refactor(fetch) : light use only one DNS thread (#9647)
* refactor(fetch) : light use only one `DNS` thread * grumbles(fetch) : pass number of threads directly
1 parent a8f6f5b commit 4784876

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

parity/run.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ const SNAPSHOT_HISTORY: u64 = 100;
7777
const GAS_CORPUS_EXPIRATION_MINUTES: u64 = 60 * 6;
7878

7979
// Pops along with error messages when a password is missing or invalid.
80-
const VERIFY_PASSWORD_HINT: &'static str = "Make sure valid password is present in files passed using `--password` or in the configuration file.";
80+
const VERIFY_PASSWORD_HINT: &str = "Make sure valid password is present in files passed using `--password` or in the configuration file.";
81+
82+
// Full client number of DNS threads
83+
const FETCH_FULL_NUM_DNS_THREADS: usize = 4;
84+
85+
// Light client number of DNS threads
86+
const FETCH_LIGHT_NUM_DNS_THREADS: usize = 1;
8187

8288
#[derive(Debug, PartialEq)]
8389
pub struct RunCmd {
@@ -283,7 +289,7 @@ fn execute_light_impl(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<Runnin
283289
let cpu_pool = CpuPool::new(4);
284290

285291
// fetch service
286-
let fetch = fetch::Client::new().map_err(|e| format!("Error starting fetch client: {:?}", e))?;
292+
let fetch = fetch::Client::new(FETCH_LIGHT_NUM_DNS_THREADS).map_err(|e| format!("Error starting fetch client: {:?}", e))?;
287293
let passwords = passwords_from_files(&cmd.acc_conf.password_files)?;
288294

289295
// prepare account provider
@@ -477,7 +483,7 @@ fn execute_impl<Cr, Rr>(cmd: RunCmd, logger: Arc<RotatingLogger>, on_client_rq:
477483
let event_loop = EventLoop::spawn();
478484

479485
// fetch service
480-
let fetch = fetch::Client::new().map_err(|e| format!("Error starting fetch client: {:?}", e))?;
486+
let fetch = fetch::Client::new(FETCH_FULL_NUM_DNS_THREADS).map_err(|e| format!("Error starting fetch client: {:?}", e))?;
481487

482488
let txpool_size = cmd.miner_options.pool_limits.max_count;
483489
// create miner

util/fetch/src/client.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ impl Drop for Client {
170170

171171
impl Client {
172172
/// Create a new fetch client.
173-
pub fn new() -> Result<Self, Error> {
173+
pub fn new(num_dns_threads: usize) -> Result<Self, Error> {
174174
let (tx_start, rx_start) = std::sync::mpsc::sync_channel(1);
175175
let (tx_proto, rx_proto) = mpsc::channel(64);
176176

177-
Client::background_thread(tx_start, rx_proto)?;
177+
Client::background_thread(tx_start, rx_proto, num_dns_threads)?;
178178

179179
match rx_start.recv_timeout(Duration::from_secs(10)) {
180180
Err(RecvTimeoutError::Timeout) => {
@@ -199,7 +199,7 @@ impl Client {
199199
})
200200
}
201201

202-
fn background_thread(tx_start: TxStartup, rx_proto: mpsc::Receiver<ChanItem>) -> io::Result<thread::JoinHandle<()>> {
202+
fn background_thread(tx_start: TxStartup, rx_proto: mpsc::Receiver<ChanItem>, num_dns_threads: usize) -> io::Result<thread::JoinHandle<()>> {
203203
thread::Builder::new().name("fetch".into()).spawn(move || {
204204
let mut core = match reactor::Core::new() {
205205
Ok(c) => c,
@@ -208,7 +208,7 @@ impl Client {
208208

209209
let handle = core.handle();
210210
let hyper = hyper::Client::configure()
211-
.connector(hyper_rustls::HttpsConnector::new(4, &core.handle()))
211+
.connector(hyper_rustls::HttpsConnector::new(num_dns_threads, &core.handle()))
212212
.build(&core.handle());
213213

214214
let future = rx_proto.take_while(|item| Ok(item.is_some()))
@@ -640,7 +640,18 @@ mod test {
640640
#[test]
641641
fn it_should_fetch() {
642642
let server = TestServer::run();
643-
let client = Client::new().unwrap();
643+
let client = Client::new(4).unwrap();
644+
let future = client.get(&format!("http://{}?123", server.addr()), Default::default());
645+
let resp = future.wait().unwrap();
646+
assert!(resp.is_success());
647+
let body = resp.concat2().wait().unwrap();
648+
assert_eq!(&body[..], b"123")
649+
}
650+
651+
#[test]
652+
fn it_should_fetch_in_light_mode() {
653+
let server = TestServer::run();
654+
let client = Client::new(1).unwrap();
644655
let future = client.get(&format!("http://{}?123", server.addr()), Default::default());
645656
let resp = future.wait().unwrap();
646657
assert!(resp.is_success());
@@ -651,7 +662,7 @@ mod test {
651662
#[test]
652663
fn it_should_timeout() {
653664
let server = TestServer::run();
654-
let client = Client::new().unwrap();
665+
let client = Client::new(4).unwrap();
655666
let abort = Abort::default().with_max_duration(Duration::from_secs(1));
656667
match client.get(&format!("http://{}/delay?3", server.addr()), abort).wait() {
657668
Err(Error::Timeout) => {}
@@ -662,7 +673,7 @@ mod test {
662673
#[test]
663674
fn it_should_follow_redirects() {
664675
let server = TestServer::run();
665-
let client = Client::new().unwrap();
676+
let client = Client::new(4).unwrap();
666677
let abort = Abort::default();
667678
let future = client.get(&format!("http://{}/redirect?http://{}/", server.addr(), server.addr()), abort);
668679
assert!(future.wait().unwrap().is_success())
@@ -671,7 +682,7 @@ mod test {
671682
#[test]
672683
fn it_should_follow_relative_redirects() {
673684
let server = TestServer::run();
674-
let client = Client::new().unwrap();
685+
let client = Client::new(4).unwrap();
675686
let abort = Abort::default().with_max_redirects(4);
676687
let future = client.get(&format!("http://{}/redirect?/", server.addr()), abort);
677688
assert!(future.wait().unwrap().is_success())
@@ -680,7 +691,7 @@ mod test {
680691
#[test]
681692
fn it_should_not_follow_too_many_redirects() {
682693
let server = TestServer::run();
683-
let client = Client::new().unwrap();
694+
let client = Client::new(4).unwrap();
684695
let abort = Abort::default().with_max_redirects(3);
685696
match client.get(&format!("http://{}/loop", server.addr()), abort).wait() {
686697
Err(Error::TooManyRedirects) => {}
@@ -691,7 +702,7 @@ mod test {
691702
#[test]
692703
fn it_should_read_data() {
693704
let server = TestServer::run();
694-
let client = Client::new().unwrap();
705+
let client = Client::new(4).unwrap();
695706
let abort = Abort::default();
696707
let future = client.get(&format!("http://{}?abcdefghijklmnopqrstuvwxyz", server.addr()), abort);
697708
let resp = future.wait().unwrap();
@@ -702,7 +713,7 @@ mod test {
702713
#[test]
703714
fn it_should_not_read_too_much_data() {
704715
let server = TestServer::run();
705-
let client = Client::new().unwrap();
716+
let client = Client::new(4).unwrap();
706717
let abort = Abort::default().with_max_size(3);
707718
let resp = client.get(&format!("http://{}/?1234", server.addr()), abort).wait().unwrap();
708719
assert!(resp.is_success());
@@ -715,7 +726,7 @@ mod test {
715726
#[test]
716727
fn it_should_not_read_too_much_data_sync() {
717728
let server = TestServer::run();
718-
let client = Client::new().unwrap();
729+
let client = Client::new(4).unwrap();
719730
let abort = Abort::default().with_max_size(3);
720731
let resp = client.get(&format!("http://{}/?1234", server.addr()), abort).wait().unwrap();
721732
assert!(resp.is_success());

0 commit comments

Comments
 (0)