Skip to content

Commit

Permalink
Shutdown runtime after task is completed (mimblewimble#331)
Browse files Browse the repository at this point in the history
* Use current thread for tokio runtime

* Use single thread runtime with shutdown
  • Loading branch information
jaspervdm authored Feb 14, 2020
1 parent 4bb0398 commit 4774704
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 8 additions & 4 deletions impls/src/client_utils/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use serde_json;
use std::fmt::{self, Display};
use std::net::SocketAddr;
use std::time::Duration;
use tokio::runtime::Runtime;
use tokio::runtime::Builder;

/// Errors that can be returned by an ApiEndpoint implementation.
#[derive(Debug)]
Expand Down Expand Up @@ -389,8 +389,12 @@ impl Client {

pub fn send_request(&self, req: Request<Body>) -> Result<String, Error> {
let task = self.send_request_async(req);
let mut rt =
Runtime::new().context(ErrorKind::Internal("can't create Tokio runtime".to_owned()))?;
Ok(rt.block_on(task)?)
let mut rt = Builder::new()
.core_threads(1)
.build()
.context(ErrorKind::Internal("can't create Tokio runtime".to_owned()))?;
let res = rt.block_on(task);
let _ = rt.shutdown_now().wait();
res
}
}
10 changes: 6 additions & 4 deletions impls/src/node_clients/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
use crate::api::{self, LocatedTxKernel, OutputListing, OutputPrintable};
use crate::core::core::{Transaction, TxKernel};
use crate::libwallet::{NodeClient, NodeVersionInfo};
use futures::{stream, Stream};
use futures::{stream, Future, Stream};
use std::collections::HashMap;
use std::env;
use tokio::runtime::Runtime;
use tokio::runtime::Builder;

use crate::client_utils::Client;
use crate::libwallet;
Expand Down Expand Up @@ -237,8 +237,10 @@ impl NodeClient for HTTPNodeClient {
}

let task = stream::futures_unordered(tasks).collect();
let mut rt = Runtime::new().unwrap();
let results: Vec<OutputPrintable> = match rt.block_on(task) {
let mut rt = Builder::new().core_threads(1).build().unwrap();
let res = rt.block_on(task);
let _ = rt.shutdown_now().wait();
let results: Vec<OutputPrintable> = match res {
Ok(resps) => {
let mut results = vec![];
for r in resps {
Expand Down

0 comments on commit 4774704

Please sign in to comment.