-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
173 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::{ | ||
sync::Arc, | ||
thread::{self, JoinHandle}, | ||
}; | ||
|
||
use parking_lot::Mutex; | ||
use tokio::sync::oneshot::{self, error::TryRecvError}; | ||
|
||
use crate::{data::ProviderData, IntervalConfig}; | ||
|
||
/// Type for interval mining on a separate thread. | ||
pub struct IntervalMiner { | ||
inner: Option<Inner>, | ||
} | ||
|
||
/// Inner type for interval mining on a separate thread, required for | ||
/// implementation of `Drop`. | ||
struct Inner { | ||
cancellation_sender: oneshot::Sender<()>, | ||
handle: JoinHandle<()>, | ||
} | ||
|
||
impl IntervalMiner { | ||
pub fn new(config: IntervalConfig, data: Arc<Mutex<ProviderData>>) -> Self { | ||
let (cancellation_sender, mut cancellation_receiver) = oneshot::channel(); | ||
let handle = thread::spawn(move || loop { | ||
let delay = config.generate_interval(); | ||
thread::sleep(std::time::Duration::from_secs(delay)); | ||
|
||
match cancellation_receiver.try_recv() { | ||
Ok(_) | Err(TryRecvError::Closed) => return, | ||
Err(TryRecvError::Empty) => {} | ||
} | ||
|
||
let mut data = data.lock(); | ||
if let Err(error) = data.interval_mine() { | ||
log::error!("Unexpected error while performing interval mining: {error}"); | ||
return; | ||
} | ||
}); | ||
|
||
Self { | ||
inner: Some(Inner { | ||
cancellation_sender, | ||
handle, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
impl Drop for IntervalMiner { | ||
fn drop(&mut self) { | ||
if let Some(Inner { | ||
cancellation_sender, | ||
handle, | ||
}) = self.inner.take() | ||
{ | ||
cancellation_sender | ||
.send(()) | ||
.expect("Failed to send cancellation signal"); | ||
|
||
handle.join().expect("Failed to join interval miner thread"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters