-
Notifications
You must be signed in to change notification settings - Fork 41
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
Add a worker pool to the client to limit parallel requests #1107
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2efb24c
initial changes
e468295
fix wasm
21f8898
Merge branch 'develop' into feat/worker-pool
b616faa
add rand dep to client
9de3261
Merge branch 'develop' into feat/worker-pool
a3777ff
Merge branch 'develop' into feat/worker-pool
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 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 |
---|---|---|
|
@@ -13,6 +13,10 @@ use { | |
tokio::sync::watch::{Receiver as WatchReceiver, Sender as WatchSender}, | ||
}; | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
pub use super::worker::TaskPriority; | ||
#[cfg(not(target_family = "wasm"))] | ||
use super::worker::WorkerPool; | ||
#[cfg(target_family = "wasm")] | ||
use crate::client::constants::CACHE_NETWORK_INFO_TIMEOUT_IN_SECONDS; | ||
use crate::{ | ||
|
@@ -56,6 +60,8 @@ pub struct ClientInner { | |
pub(crate) mqtt: MqttInner, | ||
#[cfg(target_family = "wasm")] | ||
pub(crate) last_sync: tokio::sync::Mutex<Option<u32>>, | ||
#[cfg(not(target_family = "wasm"))] | ||
pub(crate) worker_pool: WorkerPool, | ||
} | ||
|
||
#[derive(Default)] | ||
|
@@ -83,10 +89,13 @@ pub(crate) struct MqttInner { | |
impl std::fmt::Debug for Client { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let mut d = f.debug_struct("Client"); | ||
d.field("node_manager", &self.inner.node_manager); | ||
d.field("node_manager", &self.node_manager); | ||
#[cfg(feature = "mqtt")] | ||
d.field("broker_options", &self.inner.mqtt.broker_options); | ||
d.field("network_info", &self.inner.network_info).finish() | ||
d.field("broker_options", &self.mqtt.broker_options); | ||
d.field("network_info", &self.network_info); | ||
#[cfg(not(target_family = "wasm"))] | ||
d.field("worker_pool", &self.worker_pool); | ||
d.finish() | ||
} | ||
} | ||
|
||
|
@@ -95,9 +104,33 @@ impl Client { | |
pub fn builder() -> ClientBuilder { | ||
ClientBuilder::new() | ||
} | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
pub async fn rate_limit<F, Fut>(&self, f: F) -> Fut::Output | ||
where | ||
F: 'static + Send + Sync + FnOnce(Self) -> Fut, | ||
Fut: futures::Future + Send, | ||
Fut::Output: Send, | ||
{ | ||
self.prioritized_rate_limit(TaskPriority::Medium, f).await | ||
} | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
pub async fn prioritized_rate_limit<F, Fut>(&self, priority: TaskPriority, f: F) -> Fut::Output | ||
where | ||
F: 'static + Send + Sync + FnOnce(Self) -> Fut, | ||
Fut: futures::Future + Send, | ||
Fut::Output: Send, | ||
{ | ||
let client = self.clone(); | ||
self.worker_pool | ||
.process_task(priority, async move { f(client).await }) | ||
.await | ||
.unwrap() // TODO | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo 👀 |
||
} | ||
} | ||
|
||
impl ClientInner { | ||
impl Client { | ||
/// Gets the network related information such as network_id and min_pow_score | ||
/// and if it's the default one, sync it first and set the NetworkInfo. | ||
pub async fn get_network_info(&self) -> Result<NetworkInfo> { | ||
|
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just making sure, this is not breaking in any way since Client derefs to ClientInner, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But wait,
ClientInner
is public, so this is breaking change?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmm, yes I suppose so. Technically. But if you used the client this way you probably deserve to have your code break 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It this change (Client/ClientInner) really required for this PR? Otherwise we just do it in 2.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it is most definitely required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will think about alternative impls though. I'm not 100% with this one.