-
Notifications
You must be signed in to change notification settings - Fork 205
feat(core): replace callbacks in RpcModule
and Methods
#1387
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
Open
prestwich
wants to merge
10
commits into
paritytech:master
Choose a base branch
from
prestwich:prestwich/replacing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c32c96b
feature: replacing functions in RpcModule
prestwich 56b3632
feature: remove_method
prestwich 744282e
feat: add replace_if and merge_replacing_if
prestwich 915feca
doc: slight updates
prestwich 627941f
doc: more detail on conditional replacing
prestwich a4ab816
chore: rename to merge_replace
prestwich 49483b4
chore: more renamings
prestwich 8a5ef84
chore: comment on discarded unsubscribe
prestwich e0b2acf
refactor: weak callback system
prestwich c1f2ba9
chore: remove non-functional code
prestwich 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 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 |
---|---|---|
|
@@ -243,6 +243,19 @@ impl Methods { | |
} | ||
} | ||
|
||
/// Remove a method by name. | ||
pub fn remove(&mut self, name: &'static str) -> Option<MethodCallback> { | ||
self.mut_callbacks().remove(name) | ||
} | ||
|
||
/// Inserts the method callback for a given name, replacing any existing | ||
/// method with the same name. | ||
pub fn insert_replacing(&mut self, name: &'static str, callback: MethodCallback) -> Option<MethodCallback> { | ||
prestwich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
let prev = self.remove(name); | ||
let _ = self.verify_and_insert(name, callback); | ||
prev | ||
} | ||
|
||
/// Helper for obtaining a mut ref to the callbacks HashMap. | ||
fn mut_callbacks(&mut self) -> &mut FxHashMap<&'static str, MethodCallback> { | ||
Arc::make_mut(&mut self.callbacks) | ||
|
@@ -266,6 +279,23 @@ impl Methods { | |
Ok(()) | ||
} | ||
|
||
/// Merge two [`Methods`]'s by adding all [`MethodCallback`]s from `other` | ||
/// into `self`, removing and returning any existing methods with the same | ||
/// name. | ||
pub fn merge_replacing(&mut self, other: impl Into<Methods>) -> Vec<(&'static str, MethodCallback)> { | ||
niklasad1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
prestwich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
let mut other = other.into(); | ||
let callbacks = self.mut_callbacks(); | ||
|
||
let mut removed = Vec::with_capacity(other.callbacks.len()); | ||
for (name, callback) in other.mut_callbacks().drain() { | ||
if let Some(prev) = callbacks.remove(name) { | ||
removed.push((name, prev)); | ||
} | ||
callbacks.insert(name, callback); | ||
} | ||
removed | ||
} | ||
|
||
/// Returns the method callback. | ||
pub fn method(&self, method_name: &str) -> Option<&MethodCallback> { | ||
self.callbacks.get(method_name) | ||
|
@@ -521,6 +551,16 @@ impl<Context> From<RpcModule<Context>> for Methods { | |
} | ||
|
||
impl<Context: Send + Sync + 'static> RpcModule<Context> { | ||
/// Return a mutable reference to the currently registered methods. | ||
pub fn methods_mut(&mut self) -> &mut Methods { | ||
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. We don't want to expose this, can you remove it or make it private? It's not used currently. |
||
&mut self.methods | ||
} | ||
|
||
/// Remove a method by name. | ||
pub fn remove_method(&mut self, method_name: &'static str) -> Option<MethodCallback> { | ||
self.methods.remove(method_name) | ||
} | ||
|
||
/// Register a new synchronous RPC method, which computes the response with the given callback. | ||
/// | ||
/// ## Examples | ||
|
@@ -551,6 +591,20 @@ impl<Context: Send + Sync + 'static> RpcModule<Context> { | |
) | ||
} | ||
|
||
/// As [`Self::register_method`], but replaces and returns the method if | ||
/// it already exists. | ||
pub fn replace_method<R, F>(&mut self, method_name: &'static str, callback: F) -> Option<MethodCallback> | ||
where | ||
R: IntoResponse + 'static, | ||
F: Fn(Params, &Context, &Extensions) -> R + Send + Sync + 'static, | ||
{ | ||
let prev = self.remove_method(method_name); | ||
// Errors here can be ignored, as we know the method is not already | ||
// registered (we just removed it). | ||
let _ = self.register_method(method_name, callback); | ||
prev | ||
} | ||
|
||
/// Register a new asynchronous RPC method, which computes the response with the given callback. | ||
/// | ||
/// ## Examples | ||
|
@@ -589,6 +643,25 @@ impl<Context: Send + Sync + 'static> RpcModule<Context> { | |
) | ||
} | ||
|
||
/// As [`Self::register_async_method`], but replaces and returns the method | ||
/// if it already exists. | ||
pub fn replace_async_method<R, Fun, Fut>( | ||
&mut self, | ||
method_name: &'static str, | ||
callback: Fun, | ||
) -> Option<MethodCallback> | ||
where | ||
R: IntoResponse + 'static, | ||
Fut: Future<Output = R> + Send, | ||
Fun: (Fn(Params<'static>, Arc<Context>, Extensions) -> Fut) + Clone + Send + Sync + 'static, | ||
{ | ||
let prev = self.remove_method(method_name); | ||
// Errors here can be ignored, as we know the method is not already | ||
// registered (we just removed it). | ||
let _ = self.register_async_method(method_name, callback); | ||
prev | ||
} | ||
|
||
/// 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. | ||
|
@@ -632,6 +705,19 @@ impl<Context: Send + Sync + 'static> RpcModule<Context> { | |
Ok(callback) | ||
} | ||
|
||
/// As [`Self::register_blocking_method`], but replaces and returns the method if it already exists. | ||
pub fn replace_blocking_method<R, F>(&mut self, method_name: &'static str, callback: F) -> Option<MethodCallback> | ||
where | ||
R: IntoResponse + 'static, | ||
F: Fn(Params, Arc<Context>, &Extensions) -> R + Clone + Send + Sync + 'static, | ||
{ | ||
let prev = self.remove_method(method_name); | ||
// Errors here can be ignored, as we know the method is not already | ||
// registered (we just removed it). | ||
let _ = self.register_blocking_method(method_name, callback); | ||
prev | ||
} | ||
|
||
/// Register a new publish/subscribe interface using JSON-RPC notifications. | ||
/// | ||
/// It implements the [ethereum pubsub specification](https://geth.ethereum.org/docs/rpc/pubsub) | ||
|
@@ -820,6 +906,34 @@ impl<Context: Send + Sync + 'static> RpcModule<Context> { | |
Ok(callback) | ||
} | ||
|
||
/// As [`Self::register_subscription`] but replaces and returns the method | ||
/// if it already exists. | ||
pub fn replace_subscription<R, F, Fut>( | ||
&mut self, | ||
subscribe_method_name: &'static str, | ||
notif_method_name: &'static str, | ||
unsubscribe_method_name: &'static str, | ||
callback: F, | ||
) -> Option<MethodCallback> | ||
where | ||
Context: Send + Sync + 'static, | ||
F: (Fn(Params<'static>, PendingSubscriptionSink, Arc<Context>, Extensions) -> Fut) | ||
+ Send | ||
+ Sync | ||
+ Clone | ||
+ 'static, | ||
Fut: Future<Output = R> + Send + 'static, | ||
R: IntoSubscriptionCloseResponse + Send, | ||
{ | ||
let prev = self.methods.remove(subscribe_method_name); | ||
self.methods.remove(unsubscribe_method_name); | ||
prestwich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Errors here can be ignored, as we know the method is not already | ||
// registered (we just removed it). | ||
let _ = self.register_subscription(subscribe_method_name, notif_method_name, unsubscribe_method_name, callback); | ||
prev | ||
} | ||
|
||
/// Similar to [`RpcModule::register_subscription`] but a little lower-level API | ||
/// where handling the subscription is managed the user i.e, polling the subscription | ||
/// such as spawning a separate task to do so. | ||
|
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.