-
Notifications
You must be signed in to change notification settings - Fork 15
Add Anchor Processor #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c77a832
add draft of processor
dknopik 070dd27
add two experiments for fully modular processor
dknopik 671b9c5
cargo fmt
dknopik 3d5c58f
Merge branch 'unstable' into processor
dknopik 566d0f1
remove processor experiments
dknopik 575f753
add a permitless queue for async and fast tasks
dknopik 35aee42
add work expiry
dknopik d19c2dd
add processor state (for sending to QBFT instances etc.)
dknopik 7dfc670
docs
dknopik 2e450d5
fix docs
dknopik 13e812c
add test and slightly improve api
dknopik 758c682
Merge branch 'unstable' into processor
dknopik c7a21b8
cargo fmt
dknopik 70a0055
mark senders as unused for now
dknopik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,12 @@ | ||
[package] | ||
name = "processor" | ||
version = "0.1.0" | ||
authors = ["Sigma Prime <[email protected]"] | ||
edition = { workspace = true } | ||
|
||
[dependencies] | ||
tokio = { workspace = true, features = ["sync"] } | ||
tracing = { workspace = true } | ||
task_executor = { workspace = true } | ||
serde = { workspace = true } | ||
num_cpus = { workspace = true } |
This file contains hidden or 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,145 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::Arc; | ||
use task_executor::TaskExecutor; | ||
use tokio::select; | ||
use tokio::sync::mpsc::error::TrySendError; | ||
use tokio::sync::{mpsc, Semaphore}; | ||
use tracing::{error, warn}; | ||
|
||
#[derive(Clone, Serialize, Deserialize)] | ||
pub struct Config { | ||
pub max_workers: usize, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Self { | ||
max_workers: num_cpus::get(), | ||
} | ||
} | ||
} | ||
|
||
pub struct Sender { | ||
name: &'static str, | ||
tx: mpsc::Sender<WorkItem>, | ||
} | ||
|
||
impl Sender { | ||
fn new(name: &'static str, tx: mpsc::Sender<WorkItem>) -> Self { | ||
Self { name, tx } | ||
} | ||
|
||
pub fn send_async(&mut self, future: AsyncFn) { | ||
self.send_work_item(WorkItem::new_async(self.name, future)); | ||
} | ||
|
||
pub fn send_blocking(&mut self, func: BlockingFn) { | ||
self.send_work_item(WorkItem::new_blocking(self.name, func)); | ||
} | ||
|
||
pub fn send_work_item(&mut self, item: WorkItem) { | ||
if let Err(err) = self.tx.try_send(item) { | ||
match err { | ||
TrySendError::Full(item) => { | ||
warn!(task = item.name, "Processor queue full") | ||
} | ||
TrySendError::Closed(_) => { | ||
error!("Processor queue closed unexpectedly") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct Senders { | ||
example_tx: Sender, | ||
// todo add all the needed queues here | ||
} | ||
|
||
struct Receivers { | ||
example_rx: mpsc::Receiver<WorkItem>, | ||
// todo add all the needed queues here | ||
} | ||
|
||
pub type AsyncFn = Pin<Box<dyn Future<Output = ()> + Send + Sync>>; | ||
pub type BlockingFn = Box<dyn FnOnce() + Send + Sync>; | ||
|
||
enum AsyncOrBlocking { | ||
Async(AsyncFn), | ||
Blocking(BlockingFn), | ||
} | ||
pub struct WorkItem { | ||
name: &'static str, | ||
func: AsyncOrBlocking, | ||
} | ||
|
||
impl WorkItem { | ||
pub fn new_async(name: &'static str, func: AsyncFn) -> Self { | ||
Self { | ||
name, | ||
func: AsyncOrBlocking::Async(func), | ||
} | ||
} | ||
|
||
pub fn new_blocking(name: &'static str, func: BlockingFn) -> Self { | ||
Self { | ||
name, | ||
func: AsyncOrBlocking::Blocking(func), | ||
} | ||
} | ||
} | ||
|
||
pub async fn spawn(config: Config, executor: TaskExecutor) -> Senders { | ||
// todo macro? just specifying name and capacity? | ||
let (example_tx, example_rx) = mpsc::channel(1000); | ||
|
||
let senders = Senders { | ||
example_tx: Sender::new("example", example_tx), | ||
}; | ||
let receivers = Receivers { example_rx }; | ||
|
||
executor.spawn(processor(config, receivers, executor.clone()), "processor"); | ||
senders | ||
} | ||
|
||
async fn processor(config: Config, mut receivers: Receivers, executor: TaskExecutor) { | ||
// TODO: consider having separate limits for blocking and async? | ||
let semaphore = Arc::new(Semaphore::new(config.max_workers)); | ||
|
||
loop { | ||
let Ok(permit) = semaphore.clone().acquire_owned().await else { | ||
dknopik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
error!("Processor semaphore closed unexpectedly"); | ||
break; | ||
}; | ||
|
||
let work_item = select! { | ||
biased; | ||
Some(w) = receivers.example_rx.recv() => w, | ||
else => { | ||
error!("Processor queues closed unexpectedly"); | ||
break; | ||
} | ||
}; | ||
|
||
match work_item.func { | ||
AsyncOrBlocking::Async(async_fn) => executor.spawn( | ||
async move { | ||
async_fn.await; | ||
drop(permit); | ||
}, | ||
work_item.name, | ||
), | ||
AsyncOrBlocking::Blocking(blocking_fn) => { | ||
executor.spawn_blocking( | ||
move || { | ||
blocking_fn(); | ||
drop(permit); | ||
}, | ||
work_item.name, | ||
); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.