Shutdown runtime after task is completed (#331)

* Use current thread for tokio runtime

* Use single thread runtime with shutdown
This commit is contained in:
jaspervdm 2020-02-14 10:57:39 +01:00 committed by GitHub
parent 4bb0398e21
commit 4774704aeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View file

@ -31,7 +31,7 @@ use serde_json;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::time::Duration; use std::time::Duration;
use tokio::runtime::Runtime; use tokio::runtime::Builder;
/// Errors that can be returned by an ApiEndpoint implementation. /// Errors that can be returned by an ApiEndpoint implementation.
#[derive(Debug)] #[derive(Debug)]
@ -389,8 +389,12 @@ impl Client {
pub fn send_request(&self, req: Request<Body>) -> Result<String, Error> { pub fn send_request(&self, req: Request<Body>) -> Result<String, Error> {
let task = self.send_request_async(req); let task = self.send_request_async(req);
let mut rt = let mut rt = Builder::new()
Runtime::new().context(ErrorKind::Internal("can't create Tokio runtime".to_owned()))?; .core_threads(1)
Ok(rt.block_on(task)?) .build()
.context(ErrorKind::Internal("can't create Tokio runtime".to_owned()))?;
let res = rt.block_on(task);
let _ = rt.shutdown_now().wait();
res
} }
} }

View file

@ -17,10 +17,10 @@
use crate::api::{self, LocatedTxKernel, OutputListing, OutputPrintable}; use crate::api::{self, LocatedTxKernel, OutputListing, OutputPrintable};
use crate::core::core::{Transaction, TxKernel}; use crate::core::core::{Transaction, TxKernel};
use crate::libwallet::{NodeClient, NodeVersionInfo}; use crate::libwallet::{NodeClient, NodeVersionInfo};
use futures::{stream, Stream}; use futures::{stream, Future, Stream};
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use tokio::runtime::Runtime; use tokio::runtime::Builder;
use crate::client_utils::Client; use crate::client_utils::Client;
use crate::libwallet; use crate::libwallet;
@ -237,8 +237,10 @@ impl NodeClient for HTTPNodeClient {
} }
let task = stream::futures_unordered(tasks).collect(); let task = stream::futures_unordered(tasks).collect();
let mut rt = Runtime::new().unwrap(); let mut rt = Builder::new().core_threads(1).build().unwrap();
let results: Vec<OutputPrintable> = match rt.block_on(task) { let res = rt.block_on(task);
let _ = rt.shutdown_now().wait();
let results: Vec<OutputPrintable> = match res {
Ok(resps) => { Ok(resps) => {
let mut results = vec![]; let mut results = vec![];
for r in resps { for r in resps {