jsonrpc panic handle

This commit is contained in:
debris 2016-03-09 11:38:53 +01:00
parent f9e93d594b
commit a1640dcf72
3 changed files with 39 additions and 19 deletions

View File

@ -385,7 +385,7 @@ impl BlockQueue {
}
}
pub fn collect_garbage(&self) {
pub fn collect_garbage(&self) {
{
let mut verification = self.verification.lock().unwrap();
verification.unverified.shrink_to_fit();

View File

@ -43,7 +43,7 @@ use std::path::PathBuf;
use env_logger::LogBuilder;
use ctrlc::CtrlC;
use util::*;
use util::panics::MayPanic;
use util::panics::{MayPanic, PanicHandler};
use ethcore::spec::*;
use ethcore::client::*;
use ethcore::service::{ClientService, NetSyncMessage};
@ -151,7 +151,7 @@ fn setup_log(init: &Option<String>) {
}
#[cfg(feature = "rpc")]
fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>, url: &str, cors_domain: &str) {
fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>, url: &str, cors_domain: &str) -> Option<Arc<PanicHandler>> {
use rpc::v1::*;
let mut server = rpc::HttpServer::new(1);
@ -159,11 +159,12 @@ fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>, url: &str, cors_dom
server.add_delegate(EthClient::new(&client, &sync).to_delegate());
server.add_delegate(EthFilterClient::new(&client).to_delegate());
server.add_delegate(NetClient::new(&sync).to_delegate());
server.start_async(url, cors_domain);
Some(server.start_async(url, cors_domain))
}
#[cfg(not(feature = "rpc"))]
fn setup_rpc_server(_client: Arc<Client>, _sync: Arc<EthSync>, _url: &str) {
fn setup_rpc_server(_client: Arc<Client>, _sync: Arc<EthSync>, _url: &str) -> Option<Arc<PanicHandler>> {
None
}
fn print_version() {
@ -323,26 +324,28 @@ impl Configuration {
// Sync
let sync = EthSync::register(service.network(), sync_config, client);
// Setup rpc
if self.args.flag_jsonrpc {
setup_rpc_server(service.client(), sync.clone(), &self.args.flag_jsonrpc_url, &self.args.flag_jsonrpc_cors);
SocketAddr::from_str(&self.args.flag_jsonrpc_url).unwrap_or_else(|_|die!("{}: Invalid JSONRPC listen address given with --jsonrpc-url. Should be of the form 'IP:port'.", self.args.flag_jsonrpc_url));
}
// Register IO handler
let io_handler = Arc::new(ClientIoHandler {
client: service.client(),
info: Default::default(),
sync: sync
sync: sync.clone(),
});
service.io().register_handler(io_handler).expect("Error registering IO handler");
// Setup rpc
let server_handler = if self.args.flag_jsonrpc {
SocketAddr::from_str(&self.args.flag_jsonrpc_url).unwrap_or_else(|_|die!("{}: Invalid JSONRPC listen address given with --jsonrpc-url. Should be of the form 'IP:port'.", self.args.flag_jsonrpc_url));
setup_rpc_server(service.client(), sync, &self.args.flag_jsonrpc_url, &self.args.flag_jsonrpc_cors)
} else {
None
};
// Handle exit
wait_for_exit(&service);
wait_for_exit(&service, server_handler);
}
}
fn wait_for_exit(client_service: &ClientService) {
fn wait_for_exit(client_service: &ClientService, server_handler: Option<Arc<PanicHandler>>) {
let exit = Arc::new(Condvar::new());
// Handle possible exits
@ -351,6 +354,11 @@ fn wait_for_exit(client_service: &ClientService) {
let e = exit.clone();
client_service.on_panic(move |_reason| { e.notify_all(); });
if let Some(handler) = server_handler {
let e = exit.clone();
handler.on_panic(move |_reason| { e.notify_all(); });
}
// Wait for signal
let mutex = Mutex::new(());
let _ = exit.wait(mutex.lock().unwrap()).unwrap();

View File

@ -29,6 +29,9 @@ extern crate ethcore;
extern crate ethsync;
extern crate transient_hashmap;
use std::sync::Arc;
use std::thread;
use util::panics::PanicHandler;
use self::jsonrpc_core::{IoHandler, IoDelegate};
pub mod v1;
@ -36,7 +39,7 @@ pub mod v1;
/// Http server.
pub struct HttpServer {
handler: IoHandler,
threads: usize
threads: usize,
}
impl HttpServer {
@ -44,7 +47,7 @@ impl HttpServer {
pub fn new(threads: usize) -> HttpServer {
HttpServer {
handler: IoHandler::new(),
threads: threads
threads: threads,
}
}
@ -53,9 +56,18 @@ impl HttpServer {
self.handler.add_delegate(delegate);
}
/// Start server asynchronously in new thread
pub fn start_async(self, addr: &str, cors_domain: &str) {
/// Start server asynchronously in new thread and returns panic handler.
pub fn start_async(self, addr: &str, cors_domain: &str) -> Arc<PanicHandler> {
let addr = addr.to_owned();
let cors_domain = cors_domain.to_owned();
let panic_handler = PanicHandler::new_in_arc();
let ph = panic_handler.clone();
let server = jsonrpc_http_server::Server::new(self.handler, self.threads);
server.start_async(addr, jsonrpc_http_server::AccessControlAllowOrigin::Value(cors_domain.to_owned()))
thread::Builder::new().name("jsonrpc_http".to_string()).spawn(move || {
ph.catch_panic(move || {
server.start(addr.as_ref(), jsonrpc_http_server::AccessControlAllowOrigin::Value(cors_domain));
}).unwrap()
}).expect("Error while creating jsonrpc http thread");
panic_handler
}
}