Removing overengineered stuff

This commit is contained in:
Tomusdrw 2016-02-10 15:28:43 +01:00
parent 0d121dd51a
commit 7925642b1b
6 changed files with 65 additions and 108 deletions

View File

@ -60,7 +60,7 @@ impl BlockQueueInfo {
/// A queue of blocks. Sits between network or other I/O and the BlockChain.
/// Sorts them ready for blockchain insertion.
pub struct BlockQueue {
panic_handler: Arc<StringPanicHandler>,
panic_handler: Arc<PanicHandler>,
engine: Arc<Box<Engine>>,
more_to_verify: Arc<Condvar>,
verification: Arc<Mutex<Verification>>,
@ -115,7 +115,7 @@ impl BlockQueue {
let ready_signal = Arc::new(QueueSignal { signalled: AtomicBool::new(false), message_channel: message_channel });
let deleting = Arc::new(AtomicBool::new(false));
let empty = Arc::new(Condvar::new());
let panic_handler = StringPanicHandler::new_arc();
let panic_handler = PanicHandler::new_arc();
let mut verifiers: Vec<JoinHandle<()>> = Vec::new();
let thread_count = max(::num_cpus::get(), 3) - 2;
@ -337,8 +337,8 @@ impl BlockQueue {
}
}
impl MayPanic<String> for BlockQueue {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener<String> {
impl MayPanic for BlockQueue {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}

View File

@ -162,7 +162,7 @@ pub struct Client {
block_queue: RwLock<BlockQueue>,
report: RwLock<ClientReport>,
import_lock: Mutex<()>,
panic_handler: Arc<StringPanicHandler>,
panic_handler: Arc<PanicHandler>,
}
const HISTORY: u64 = 1000;
@ -211,9 +211,9 @@ impl Client {
}
let block_queue = BlockQueue::new(engine.clone(), message_channel);
let panic_handler = StringPanicHandler::new_arc();
let panic_handler = PanicHandler::new_arc();
let panic = panic_handler.clone();
block_queue.on_panic(move |t: &String| panic.notify_all(t));
block_queue.on_panic(move |t| panic.notify_all(t));
Ok(Arc::new(Client {
chain: chain,
@ -440,8 +440,8 @@ impl BlockChainClient for Client {
}
}
impl MayPanic<String> for Client {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener<String> {
impl MayPanic for Client {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}

View File

@ -169,7 +169,7 @@ fn wait_for_exit(client: Arc<Client>) {
let e = exit.clone();
CtrlC::set_handler(move || { e.notify_all(); });
let e = exit.clone();
client.on_panic(move |_t: &String| { e.notify_all(); });
client.on_panic(move |_reason| { e.notify_all(); });
// Wait for signal
let mutex = Mutex::new(());
let _ = exit.wait(mutex.lock().unwrap()).unwrap();

View File

@ -307,13 +307,13 @@ impl<Message> IoChannel<Message> where Message: Send + Clone {
/// General IO Service. Starts an event loop and dispatches IO requests.
/// 'Message' is a notification message type
pub struct IoService<Message> where Message: Send + Sync + Clone + 'static {
panic_handler: Arc<StringPanicHandler>,
panic_handler: Arc<PanicHandler>,
thread: Option<JoinHandle<()>>,
host_channel: Sender<IoMessage<Message>>,
}
impl<Message> MayPanic<String> for IoService<Message> where Message: Send + Sync + Clone + 'static {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener<String> {
impl<Message> MayPanic for IoService<Message> where Message: Send + Sync + Clone + 'static {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}
@ -321,7 +321,7 @@ impl<Message> MayPanic<String> for IoService<Message> where Message: Send + Sync
impl<Message> IoService<Message> where Message: Send + Sync + Clone + 'static {
/// Starts IO event loop
pub fn start() -> Result<IoService<Message>, UtilError> {
let panic_handler = StringPanicHandler::new_arc();
let panic_handler = PanicHandler::new_arc();
let mut event_loop = EventLoop::new().unwrap();
let channel = event_loop.channel();
let panic = panic_handler.clone();

View File

@ -44,7 +44,7 @@ pub struct Worker {
thread: Option<JoinHandle<()>>,
wait: Arc<Condvar>,
deleting: Arc<AtomicBool>,
panic_handler: Arc<StringPanicHandler>,
panic_handler: Arc<PanicHandler>,
}
impl Worker {
@ -55,7 +55,7 @@ impl Worker {
wait: Arc<Condvar>,
wait_mutex: Arc<Mutex<()>>) -> Worker
where Message: Send + Sync + Clone + 'static {
let panic_handler = StringPanicHandler::new_arc();
let panic_handler = PanicHandler::new_arc();
let deleting = Arc::new(AtomicBool::new(false));
let mut worker = Worker {
panic_handler: panic_handler.clone(),
@ -114,8 +114,8 @@ impl Worker {
}
}
impl MayPanic<String> for Worker {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener<String> {
impl MayPanic for Worker {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}

View File

@ -21,126 +21,83 @@ use std::ops::DerefMut;
use std::any::Any;
use std::sync::{Arc, Mutex};
pub trait OnPanicListener<T>: Send + Sync + 'static {
fn call(&mut self, arg: &T);
/// Thread-safe closure for handling possible panics
pub trait OnPanicListener: Send + Sync + 'static {
/// Invoke listener
fn call(&mut self, arg: &str);
}
impl<F, T> OnPanicListener<T> for F
where F: FnMut(&T) + Send + Sync + 'static {
fn call(&mut self, arg: &T) {
self(arg)
}
}
pub trait ArgsConverter<T> : Send + Sync {
fn convert(&self, t: &Box<Any + Send>) -> Option<T>;
}
pub trait MayPanic<T> {
/// Trait indicating that the structure catches some of the panics (most probably from spawned threads)
/// and it's possbile to be notified when one of the threads panics.
pub trait MayPanic {
/// `closure` will be invoked whenever panic in thread is caught
fn on_panic<F>(&self, closure: F)
where F: OnPanicListener<T>;
where F: OnPanicListener;
}
pub trait PanicHandler<T, C: ArgsConverter<T>> : MayPanic<T>{
fn with_converter(converter: C) -> Self;
fn catch_panic<G, R>(&self, g: G) -> thread::Result<R>
where G: FnOnce() -> R + Send + 'static;
fn notify_all(&self, &T);
/// Structure that allows to catch panics and notify listeners
pub struct PanicHandler {
listeners: Mutex<Vec<Box<OnPanicListener>>>
}
pub struct StringConverter;
impl ArgsConverter<String> for StringConverter {
fn convert(&self, t: &Box<Any + Send>) -> Option<String> {
let as_str = t.downcast_ref::<&'static str>().map(|t| t.clone().to_owned());
let as_string = t.downcast_ref::<String>().cloned();
as_str.or(as_string)
}
impl PanicHandler {
/// Creates new `PanicHandler` wrapped in `Arc`
pub fn new_arc() -> Arc<PanicHandler> {
Arc::new(Self::new())
}
pub struct BasePanicHandler<T, C>
where C: ArgsConverter<T>, T: 'static {
converter: C,
listeners: Mutex<Vec<Box<OnPanicListener<T>>>>
}
impl<T, C> PanicHandler<T, C> for BasePanicHandler<T, C>
where C: ArgsConverter<T>, T: 'static {
fn with_converter(converter: C) -> Self {
BasePanicHandler {
converter: converter,
/// Creates new `PanicHandler`
pub fn new() -> PanicHandler {
PanicHandler {
listeners: Mutex::new(vec![])
}
}
/// Invoke closure and catch any possible panics.
/// In case of panic notifies all listeners about it.
#[allow(deprecated)]
// TODO [todr] catch_panic is deprecated but panic::recover has different bounds (not allowing mutex)
fn catch_panic<G, R>(&self, g: G) -> thread::Result<R> where G: FnOnce() -> R + Send + 'static {
pub fn catch_panic<G, R>(&self, g: G) -> thread::Result<R> where G: FnOnce() -> R + Send + 'static {
let result = thread::catch_panic(g);
if let Err(ref e) = result {
let res = self.converter.convert(e);
let res = convert_to_string(e);
if let Some(r) = res {
self.notify_all(&r);
self.notify_all(r);
}
}
result
}
fn notify_all(&self, r: &T) {
/// Notify listeners about panic
pub fn notify_all(&self, r: String) {
let mut listeners = self.listeners.lock().unwrap();
for listener in listeners.deref_mut() {
listener.call(r);
listener.call(&r);
}
}
}
impl<T, C> MayPanic<T> for BasePanicHandler<T, C>
where C: ArgsConverter<T>, T: 'static {
impl MayPanic for PanicHandler {
fn on_panic<F>(&self, closure: F)
where F: OnPanicListener<T> {
where F: OnPanicListener {
self.listeners.lock().unwrap().push(Box::new(closure));
}
}
pub struct StringPanicHandler {
handler: BasePanicHandler<String, StringConverter>
}
impl StringPanicHandler {
pub fn new_arc() -> Arc<StringPanicHandler> {
Arc::new(Self::new())
}
pub fn new () -> Self {
Self::with_converter(StringConverter)
impl<F> OnPanicListener for F
where F: FnMut(String) + Send + Sync + 'static {
fn call(&mut self, arg: &str) {
self(arg.to_owned())
}
}
impl PanicHandler<String, StringConverter> for StringPanicHandler {
fn convert_to_string(t: &Box<Any + Send>) -> Option<String> {
let as_str = t.downcast_ref::<&'static str>().map(|t| t.clone().to_owned());
let as_string = t.downcast_ref::<String>().cloned();
fn with_converter(converter: StringConverter) -> Self {
StringPanicHandler {
handler: BasePanicHandler::with_converter(converter)
}
}
fn catch_panic<G, R>(&self, g: G) -> thread::Result<R> where G: FnOnce() -> R + Send + 'static {
self.handler.catch_panic(g)
}
fn notify_all(&self, r: &String) {
self.handler.notify_all(r);
}
}
impl MayPanic<String> for StringPanicHandler {
fn on_panic<F>(&self, closure: F)
where F: OnPanicListener<String> {
self.handler.on_panic(closure)
}
as_str.or(as_string)
}
#[test]
@ -149,8 +106,8 @@ fn should_notify_listeners_about_panic () {
// given
let invocations = Arc::new(RwLock::new(vec![]));
let i = invocations.clone();
let p = StringPanicHandler::new();
p.on_panic(move |t: &String| i.write().unwrap().push(t.clone()));
let p = PanicHandler::new();
p.on_panic(move |t| i.write().unwrap().push(t));
// when
p.catch_panic(|| panic!("Panic!")).unwrap_err();
@ -165,8 +122,8 @@ fn should_notify_listeners_about_panic_when_string_is_dynamic () {
// given
let invocations = Arc::new(RwLock::new(vec![]));
let i = invocations.clone();
let p = StringPanicHandler::new();
p.on_panic(move |t: &String| i.write().unwrap().push(t.clone()));
let p = PanicHandler::new();
p.on_panic(move |t| i.write().unwrap().push(t));
// when
p.catch_panic(|| panic!("Panic: {}", 1)).unwrap_err();
@ -183,8 +140,8 @@ fn should_notify_listeners_about_panic_in_other_thread () {
// given
let invocations = Arc::new(RwLock::new(vec![]));
let i = invocations.clone();
let p = StringPanicHandler::new();
p.on_panic(move |t: &String| i.write().unwrap().push(t.clone()));
let p = PanicHandler::new();
p.on_panic(move |t| i.write().unwrap().push(t));
// when
let t = thread::spawn(move ||
@ -202,11 +159,11 @@ use std::sync::RwLock;
// given
let invocations = Arc::new(RwLock::new(vec![]));
let i = invocations.clone();
let p = StringPanicHandler::new();
p.on_panic(move |t: &String| i.write().unwrap().push(t.clone()));
let p = PanicHandler::new();
p.on_panic(move |t| i.write().unwrap().push(t));
let p2 = StringPanicHandler::new();
p2.on_panic(move |t: &String| p.notify_all(t));
let p2 = PanicHandler::new();
p2.on_panic(move |t| p.notify_all(t));
// when
p2.catch_panic(|| panic!("Panic!")).unwrap_err();