Skip to content

Commit bd15bf7

Browse files
committed
Graceful API shutdown
1 parent 350e2f3 commit bd15bf7

File tree

8 files changed

+145
-82
lines changed

8 files changed

+145
-82
lines changed

Cargo.lock

+39-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ blake2-rfc = "0.2"
2424
chrono = "0.4.11"
2525
clap = { version = "2.33", features = ["yaml"] }
2626
ctrlc = { version = "3.1", features = ["termination"] }
27+
failure = "0.1"
28+
failure_derive = "0.1"
29+
futures = "0.3.19"
2730
humansize = "1.1.0"
31+
log = "0.4"
2832
serde = "1"
2933
serde_json = "1"
30-
log = "0.4"
3134
term = "0.6"
32-
failure = "0.1"
33-
failure_derive = "0.1"
35+
3436

3537
grin_api = { path = "./api", version = "4.4.2" }
3638
grin_config = { path = "./config", version = "4.4.2" }

api/src/handlers.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ use crate::stratum::Stratum;
5959
use crate::stratum_rpc::StratumRpc;
6060
use crate::util::to_base64;
6161
use crate::util::RwLock;
62+
use crate::util::StopState;
6263
use crate::web::*;
6364
use easy_jsonrpc_mw::{Handler, MaybeReply};
65+
use futures::channel::oneshot;
6466
use hyper::{Body, Request, Response, StatusCode};
6567
use serde::Serialize;
6668
use std::net::SocketAddr;
6769
use std::sync::{Arc, Weak};
70+
use std::thread;
6871

6972
/// Listener version, providing same API but listening for requests on a
7073
/// port and wrapping the calls
@@ -79,6 +82,8 @@ pub fn node_apis<B, P, V>(
7982
tls_config: Option<TLSConfig>,
8083
allow_to_stop: bool,
8184
stratum_ip_pool: Arc<stratum::connections::StratumIpPool>,
85+
api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>),
86+
stop_state: Arc<StopState>,
8287
) -> Result<(), Error>
8388
where
8489
B: BlockChain + 'static,
@@ -155,10 +160,24 @@ where
155160
let mut apis = ApiServer::new();
156161
warn!("Starting HTTP Node APIs server at {}.", addr);
157162
let socket_addr: SocketAddr = addr.parse().expect("unable to parse socket address");
158-
let api_thread = apis.start(socket_addr, router, tls_config);
163+
let api_thread = apis.start(socket_addr, router, tls_config, api_chan);
159164

160165
warn!("HTTP Node listener started.");
161166

167+
thread::Builder::new()
168+
.name("api_monitor".to_string())
169+
.spawn(move || {
170+
// monitor for stop state is_stopped
171+
loop {
172+
std::thread::sleep(std::time::Duration::from_millis(100));
173+
if stop_state.is_stopped() {
174+
apis.stop();
175+
break;
176+
}
177+
}
178+
})
179+
.ok();
180+
162181
match api_thread {
163182
Ok(_) => Ok(()),
164183
Err(e) => {

0 commit comments

Comments
 (0)