exp backoff and theads handling
This commit is contained in:
35
client-network/src/threads_handling.rs
Normal file
35
client-network/src/threads_handling.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
use std::thread::JoinHandle;
|
||||
|
||||
pub enum WorkerType {
|
||||
MSGRECEPTION,
|
||||
MSGSENDER,
|
||||
PING,
|
||||
MSGRETRY,
|
||||
}
|
||||
|
||||
pub struct Worker {
|
||||
thread: Option<JoinHandle<()>>,
|
||||
stop: Arc<AtomicBool>,
|
||||
workertype: WorkerType,
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
pub fn spawn(thread: JoinHandle<()>, workertype: WorkerType) -> Self {
|
||||
Worker {
|
||||
stop: Arc::new(AtomicBool::new(false)),
|
||||
thread: Some(thread),
|
||||
workertype,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(mut self) {
|
||||
self.stop.store(true, Ordering::Relaxed);
|
||||
if let Some(h) = self.thread.take() {
|
||||
let _ = h.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user