-
Notifications
You must be signed in to change notification settings - Fork 172
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 setter for middleware #577
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d3ead84
Fix try-build tests
dvdplm 5acc639
Add a middleware setter and an example
dvdplm 0a2afa6
Actually add the example
dvdplm 7b8105b
Grumbles
dvdplm 61499d9
Use an atomic
dvdplm 479db74
Set middleware with a constructor instead
dvdplm 89d4e32
Resolve a todo
dvdplm ecff633
Update ws-server/src/server.rs
dvdplm 8d12564
Update ws-server/src/server.rs
dvdplm 50c3cba
Update ws-server/src/server.rs
dvdplm 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2019-2021 Parity Technologies (UK) Ltd. | ||
// | ||
// Permission is hereby granted, free of charge, to any | ||
// person obtaining a copy of this software and associated | ||
// documentation files (the "Software"), to deal in the | ||
// Software without restriction, including without | ||
// limitation the rights to use, copy, modify, merge, | ||
// publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software | ||
// is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice | ||
// shall be included in all copies or substantial portions | ||
// of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
use jsonrpsee::{ | ||
types::traits::Client, | ||
utils::server::middleware, | ||
ws_client::WsClientBuilder, | ||
ws_server::{RpcModule, WsServerBuilder}, | ||
}; | ||
use std::net::SocketAddr; | ||
use std::sync::atomic; | ||
|
||
#[derive(Default)] | ||
struct ManInTheMiddle { | ||
when: atomic::AtomicU64, | ||
} | ||
|
||
impl Clone for ManInTheMiddle { | ||
fn clone(&self) -> Self { | ||
ManInTheMiddle { when: atomic::AtomicU64::new(self.when.load(atomic::Ordering::SeqCst)) } | ||
} | ||
} | ||
|
||
impl middleware::Middleware for ManInTheMiddle { | ||
type Instant = u64; | ||
fn on_request(&self) -> Self::Instant { | ||
self.when.fetch_add(1, atomic::Ordering::SeqCst) | ||
} | ||
|
||
fn on_call(&self, name: &str) { | ||
println!("They called '{}'", name); | ||
} | ||
|
||
fn on_result(&self, name: &str, succeess: bool, started_at: Self::Instant) { | ||
println!("call={}, worked? {}, when? {}", name, succeess, started_at); | ||
} | ||
|
||
fn on_response(&self, started_at: Self::Instant) { | ||
println!("Response started_at={}", started_at); | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
tracing_subscriber::FmtSubscriber::builder() | ||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
.try_init() | ||
.expect("setting default subscriber failed"); | ||
|
||
let addr = run_server().await?; | ||
let url = format!("ws://{}", addr); | ||
|
||
let client = WsClientBuilder::default().build(&url).await?; | ||
let response: String = client.request("say_hello", None).await?; | ||
tracing::info!("response: {:?}", response); | ||
// TODO: This prints `They called 'blabla'` but nothing more. I expected the `on_response` callback to be called too? | ||
let _response: Result<String, _> = client.request("blabla", None).await; | ||
let _ = client.request::<String>("say_hello", None).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn run_server() -> anyhow::Result<SocketAddr> { | ||
let m = ManInTheMiddle::default(); | ||
let server = WsServerBuilder::with_middleware(m).build("127.0.0.1:0").await?; | ||
let mut module = RpcModule::new(()); | ||
module.register_method("say_hello", |_, _| Ok("lo"))?; | ||
let addr = server.local_addr()?; | ||
server.start(module)?; | ||
Ok(addr) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: Unknown argument `magic`, expected one of: `aliases`, `blocking`, `name`, `param_kind`, `resources` | ||
--> tests/ui/incorrect/method/method_unexpected_field.rs:6:25 | ||
--> $DIR/method_unexpected_field.rs:6:25 | ||
| | ||
6 | #[method(name = "foo", magic = false)] | ||
| ^^^^^ |
2 changes: 1 addition & 1 deletion
2
proc-macros/tests/ui/incorrect/sub/sub_dup_name_override.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: "override" is already defined | ||
--> tests/ui/incorrect/sub/sub_dup_name_override.rs:9:5 | ||
--> $DIR/sub_dup_name_override.rs:9:5 | ||
| | ||
9 | fn two(&self) -> RpcResult<()>; | ||
| ^^^ |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: "one" is already defined | ||
--> tests/ui/incorrect/sub/sub_name_override.rs:7:5 | ||
--> $DIR/sub_name_override.rs:7:5 | ||
| | ||
7 | fn one(&self) -> RpcResult<()>; | ||
| ^^^ |
2 changes: 1 addition & 1 deletion
2
proc-macros/tests/ui/incorrect/sub/sub_unsupported_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: Unknown argument `magic`, expected one of: `aliases`, `item`, `name`, `param_kind`, `unsubscribe_aliases` | ||
--> tests/ui/incorrect/sub/sub_unsupported_field.rs:6:42 | ||
--> $DIR/sub_unsupported_field.rs:6:42 | ||
| | ||
6 | #[subscription(name = "sub", item = u8, magic = true)] | ||
| ^^^^^ |
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
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.
Why remove the default?
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.
Was trying to make
Builder::default()
work and this seemed to be in the way, but I don't think this was the problem. OTOH: do we need it?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'd keep it, all it does is if you don't use middleware you can just write
WsServer
instead ofWsServer<()>
.