Skip to content
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

Merged
merged 6 commits into from
Nov 30, 2023
Merged

feat(client): add disconnect_reason API #1246

merged 6 commits into from
Nov 30, 2023

Conversation

niklasad1
Copy link
Member

This commit adds an API to get the error reason why the backend was disconnected.

Close #1196

This commit adds an API to get the error reason why the backend
was disconnected.

Close #1196
@niklasad1 niklasad1 requested a review from a team as a code owner November 27, 2023 11:36
@@ -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>),
Copy link
Member Author

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..

@niklasad1 niklasad1 closed this Nov 27, 2023
@niklasad1 niklasad1 reopened this Nov 27, 2023
/// # Cancel-safety
///
/// This method is not cancel-safe
pub async fn disconnect_reason(&self) -> Error {
Copy link
Member Author

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......

arc_err
}
ReadErrorOnce::Read(arc_err) => {
*write = ReadErrorOnce::Read(arc_err.clone());
Copy link
Member Author

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"

Copy link
Contributor

@tadeohepperle tadeohepperle left a 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 :)

core/src/client/async_client/mod.rs Outdated Show resolved Hide resolved
core/src/client/async_client/mod.rs Outdated Show resolved Hide resolved
@@ -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() {
Copy link
Contributor

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

Copy link
Member Author

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

};

let mut write = self.0.write().await;
let state = std::mem::replace(&mut *write, ReadErrorOnce::Unreachable);
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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

Copy link
Contributor

@lexnv lexnv left a 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 👍

@niklasad1 niklasad1 merged commit b50a7ca into master Nov 30, 2023
11 checks passed
@niklasad1 niklasad1 deleted the fix-1196 branch November 30, 2023 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to get disconnect reason?
3 participants