-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new type of invalidation task, more statistics for monitoring, As…
…yncExecutor to process async tasks and minor improvements
- Loading branch information
1 parent
c8b99a8
commit 74e0be2
Showing
15 changed files
with
642 additions
and
169 deletions.
There are no files selected for viewing
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,75 @@ | ||
use std::future::Future; | ||
|
||
use crate::types::GenericError; | ||
|
||
pub trait AsyncExecutor: Send + 'static { | ||
type Task: AsyncTask; | ||
fn execute<F>(&self, exp_time: u128, future: F) -> Self::Task | ||
where | ||
F: Future<Output=Result<(), GenericError>> + Send + 'static; | ||
} | ||
|
||
|
||
pub trait AsyncTask: Send { | ||
fn is_finished(&self) -> bool; | ||
fn is_expired(&self) -> bool; | ||
fn abort(&self); | ||
} | ||
|
||
|
||
#[cfg(feature = "tokio")] | ||
pub mod tokio { | ||
use std::future::Future; | ||
use tokio::runtime::Handle; | ||
use tokio::task::JoinHandle; | ||
use crate::async_executor::{AsyncExecutor, AsyncTask}; | ||
use crate::types::GenericError; | ||
use crate::utils::is_now_after; | ||
|
||
pub struct TokioAsyncExecutor { | ||
handle: Handle, | ||
} | ||
|
||
impl TokioAsyncExecutor { | ||
pub fn new(handle: Handle) -> Self { | ||
Self { handle } | ||
} | ||
} | ||
|
||
impl AsyncExecutor for TokioAsyncExecutor { | ||
type Task = TokioAsyncTask; | ||
fn execute<F>(&self, exp_time: u128, future: F) -> Self::Task | ||
where | ||
F: Future<Output=Result<(), GenericError>> + Send + 'static, | ||
{ | ||
let task = self.handle.spawn(future); | ||
TokioAsyncTask::new(task, exp_time) | ||
} | ||
} | ||
|
||
pub struct TokioAsyncTask { | ||
delegate: JoinHandle<Result<(), GenericError>>, | ||
exp_time: u128, | ||
} | ||
|
||
impl TokioAsyncTask { | ||
pub fn new(delegate: JoinHandle<Result<(), GenericError>>, exp_time: u128) -> Self { | ||
Self { delegate, exp_time } | ||
} | ||
} | ||
|
||
impl AsyncTask for TokioAsyncTask { | ||
fn is_finished(&self) -> bool { | ||
self.delegate.is_finished() | ||
} | ||
|
||
fn is_expired(&self) -> bool { | ||
is_now_after(self.exp_time) | ||
} | ||
|
||
fn abort(&self) { | ||
self.delegate.abort(); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.