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

@ -43,7 +43,7 @@ use std::path::PathBuf;
use env_logger::LogBuilder; use env_logger::LogBuilder;
use ctrlc::CtrlC; use ctrlc::CtrlC;
use util::*; use util::*;
use util::panics::MayPanic; use util::panics::{MayPanic, PanicHandler};
use ethcore::spec::*; use ethcore::spec::*;
use ethcore::client::*; use ethcore::client::*;
use ethcore::service::{ClientService, NetSyncMessage}; use ethcore::service::{ClientService, NetSyncMessage};
@ -151,7 +151,7 @@ fn setup_log(init: &Option<String>) {
} }
#[cfg(feature = "rpc")] #[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::*; use rpc::v1::*;
let mut server = rpc::HttpServer::new(1); 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(EthClient::new(&client, &sync).to_delegate());
server.add_delegate(EthFilterClient::new(&client).to_delegate()); server.add_delegate(EthFilterClient::new(&client).to_delegate());
server.add_delegate(NetClient::new(&sync).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"))] #[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() { fn print_version() {
@ -323,26 +324,28 @@ impl Configuration {
// Sync // Sync
let sync = EthSync::register(service.network(), sync_config, client); 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 // Register IO handler
let io_handler = Arc::new(ClientIoHandler { let io_handler = Arc::new(ClientIoHandler {
client: service.client(), client: service.client(),
info: Default::default(), info: Default::default(),
sync: sync sync: sync.clone(),
}); });
service.io().register_handler(io_handler).expect("Error registering IO handler"); 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 // 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()); let exit = Arc::new(Condvar::new());
// Handle possible exits // Handle possible exits
@ -351,6 +354,11 @@ fn wait_for_exit(client_service: &ClientService) {
let e = exit.clone(); let e = exit.clone();
client_service.on_panic(move |_reason| { e.notify_all(); }); 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 // Wait for signal
let mutex = Mutex::new(()); let mutex = Mutex::new(());
let _ = exit.wait(mutex.lock().unwrap()).unwrap(); let _ = exit.wait(mutex.lock().unwrap()).unwrap();

View File

@ -29,6 +29,9 @@ extern crate ethcore;
extern crate ethsync; extern crate ethsync;
extern crate transient_hashmap; extern crate transient_hashmap;
use std::sync::Arc;
use std::thread;
use util::panics::PanicHandler;
use self::jsonrpc_core::{IoHandler, IoDelegate}; use self::jsonrpc_core::{IoHandler, IoDelegate};
pub mod v1; pub mod v1;
@ -36,7 +39,7 @@ pub mod v1;
/// Http server. /// Http server.
pub struct HttpServer { pub struct HttpServer {
handler: IoHandler, handler: IoHandler,
threads: usize threads: usize,
} }
impl HttpServer { impl HttpServer {
@ -44,7 +47,7 @@ impl HttpServer {
pub fn new(threads: usize) -> HttpServer { pub fn new(threads: usize) -> HttpServer {
HttpServer { HttpServer {
handler: IoHandler::new(), handler: IoHandler::new(),
threads: threads threads: threads,
} }
} }
@ -53,9 +56,18 @@ impl HttpServer {
self.handler.add_delegate(delegate); self.handler.add_delegate(delegate);
} }
/// Start server asynchronously in new thread /// Start server asynchronously in new thread and returns panic handler.
pub fn start_async(self, addr: &str, cors_domain: &str) { 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); 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
} }
} }