-
Notifications
You must be signed in to change notification settings - Fork 173
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
feat(client): add disconnect_reason
API
#1246
Conversation
This commit adds an API to get the error reason why the backend was disconnected. Close #1196
@@ -57,7 +57,7 @@ pub enum Error { | |||
InvalidResponse(Mismatch<String>), | |||
/// The background task has been terminated. | |||
#[error("The background task been terminated because: {0}; restart required")] | |||
RestartNeeded(String), | |||
RestartNeeded(Arc<Error>), |
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.
This is awkward to match on but definitely better than a String I reckon..
/// # Cancel-safety | ||
/// | ||
/// This method is not cancel-safe | ||
pub async fn disconnect_reason(&self) -> Error { |
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 a little worried to expose this in the public API but changed this to a RwLock and it should only require the write lock
once.
This entire thing is to be &self
and not &mut self
and I'm tempted to move this tokio watch/broadcast but it doesn't work because of these APIs requires &mut self
as well......
core/src/client/async_client/mod.rs
Outdated
arc_err | ||
} | ||
ReadErrorOnce::Read(arc_err) => { | ||
*write = ReadErrorOnce::Read(arc_err.clone()); |
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.
state
is moved on the match and must be "initialized again here"
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.
Looks good to me :)
client/ws-client/src/tests.rs
Outdated
@@ -68,8 +68,12 @@ async fn method_call_with_wrong_id_kind() { | |||
let client = | |||
WsClientBuilder::default().id_format(IdKind::String).build(&uri).with_default_timeout().await.unwrap().unwrap(); | |||
|
|||
let err: Result<String, Error> = client.request("o", rpc_params![]).with_default_timeout().await.unwrap(); | |||
assert!(matches!(err, Err(Error::RestartNeeded(e)) if e == "request ID=0 is not a pending call")); | |||
match client.request::<String, _>("o", rpc_params![]).with_default_timeout().await.unwrap() { |
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.
nit: Maybe assert_matches!
cleans this up a bit, but since its part of testing I wouldn't worry too much about 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 wanted to show that working with Error::RestartNeeded(Arc) can be awkward :P
core/src/client/async_client/mod.rs
Outdated
}; | ||
|
||
let mut write = self.0.write().await; | ||
let state = std::mem::replace(&mut *write, ReadErrorOnce::Unreachable); |
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.
We expect this to be called exactly once right?
Otherwise, we'll get to the unreachable
below, would something like AsyncRwLock<Option<RreadErrorOnce>>
get rid of the ReadErrorOnce::Unreachable
?
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.
yeah fair enough,
Ideally we would remove this AsyncRwLock with something like https://docs.rs/async-once-cell/0.5.3/async_once_cell/struct.Lazy.html# but it requires a bunch of unstable features but would be nice to get rid of for sure.
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.
Looks clean as it is, no need to rely on unstable
features for now :D
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.
Solid work! I only had a few small nits that you can ignore if they don't make sense to you 👍
This commit adds an API to get the error reason why the backend was disconnected.
Close #1196