-
Notifications
You must be signed in to change notification settings - Fork 168
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
register_blocking_method
#523
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
70e08dd
register_blocking_method
maciejhirsz 4378a5b
Support `blocking` flag in `#[method]`
maciejhirsz a8f6b76
Macro support and tests
maciejhirsz fcce365
MacOS CI fails if there are too many blocking tasks
maciejhirsz 37d1733
conn_id is never used in async callbacks
maciejhirsz 84473cd
Dry-run blocking callbacks to maybe satisfy MacOS CI
maciejhirsz a2a34ab
Make tokio optional behind server feature flag
maciejhirsz 7d8b972
Document `blocking` flag use in `#[method]`
maciejhirsz ca62a8d
Comment why the test is disabled on mac
maciejhirsz 3b04b57
Fix warnings on macos tests
maciejhirsz 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
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
2 changes: 1 addition & 1 deletion
2
proc-macros/tests/ui/incorrect/method/method_unexpected_field.stderr
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 |
---|---|---|
|
@@ -466,6 +466,43 @@ impl<Context: Send + Sync + 'static> RpcModule<Context> { | |
Ok(MethodResourcesBuilder { build: ResourceVec::new(), callback }) | ||
} | ||
|
||
/// Register a new **blocking** synchronous RPC method, which computes the response with the given callback. | ||
/// Unlike the regular [`register_method`](RpcModule::register_method), this method can block its thread and perform expensive computations. | ||
pub fn register_blocking_method<R, F>( | ||
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. Soon we'll be at a point where we should have a macro for all these |
||
&mut self, | ||
method_name: &'static str, | ||
callback: F, | ||
) -> Result<MethodResourcesBuilder, Error> | ||
where | ||
Context: Send + Sync + 'static, | ||
R: Serialize, | ||
F: Fn(Params, Arc<Context>) -> Result<R, Error> + Copy + Send + Sync + 'static, | ||
{ | ||
let ctx = self.ctx.clone(); | ||
let callback = self.methods.verify_and_insert( | ||
method_name, | ||
MethodCallback::new_async(Arc::new(move |id, params, tx, _conn_id, claimed| { | ||
let ctx = ctx.clone(); | ||
|
||
tokio::task::spawn_blocking(move || { | ||
match callback(params, ctx) { | ||
Ok(res) => send_response(id, &tx, res), | ||
Err(err) => send_call_error(id, &tx, err), | ||
}; | ||
|
||
// Release claimed resources | ||
drop(claimed); | ||
}) | ||
.map(|err| { | ||
log::error!("Join error for blocking RPC method: {:?}", err); | ||
}) | ||
.boxed() | ||
})), | ||
)?; | ||
|
||
Ok(MethodResourcesBuilder { build: ResourceVec::new(), callback }) | ||
} | ||
|
||
/// Register a new RPC subscription that invokes callback on every subscription request. | ||
/// The callback itself takes three parameters: | ||
/// - [`Params`]: JSONRPC parameters in the subscription request. | ||
|
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.
I'm hard pressed to come up with something better than this. It'll have to make do.
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.
Right now on MacOS CI 50ms + 50ms in parallel = 160ms though :D