-
Notifications
You must be signed in to change notification settings - Fork 46
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
detect and act on network change #1133
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
806261f
detect and act on network change
JssDWt c335eb0
remove network change detection propagation
JssDWt 42b591e
move tonic_wrap to sdk_common
JssDWt e4fa424
wrap breez server with connection fallbacks
JssDWt 0d44831
wrap node api with connection fallbacks
JssDWt 007cad2
wrap greenlight with connection fallbacks
JssDWt e5eb52b
wrap backup transport with connection fallbacks
JssDWt 4e1b78e
handle multiple errors in connection fallback
JssDWt 486a569
add a connection failure string
JssDWt e60aa4c
add broken connection string
JssDWt 2857569
put retry logic in a macro
JssDWt 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,82 @@ | ||
use std::{error::Error, fmt::Display, future::Future}; | ||
|
||
use log::debug; | ||
|
||
pub struct Status(pub tonic::Status); | ||
|
||
impl Display for Status { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"status: {:?}, message: {:?}, details: {:?}, metadata: {:?}, source: {:?}", | ||
self.0.code(), | ||
self.0.message(), | ||
self.0.details(), | ||
self.0.metadata(), | ||
self.0.source(), | ||
) | ||
} | ||
} | ||
|
||
pub struct TransportError(pub tonic::transport::Error); | ||
|
||
impl Display for TransportError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"description: {:?}, source: {:?}", | ||
self.0.to_string(), | ||
self.0.source(), | ||
) | ||
} | ||
} | ||
|
||
pub async fn with_connection_fallback<T, M, F>( | ||
main: M, | ||
fallback: impl FnOnce() -> F, | ||
) -> Result<T, tonic::Status> | ||
where | ||
M: Future<Output = Result<T, tonic::Status>>, | ||
F: Future<Output = Result<T, tonic::Status>>, | ||
T: std::fmt::Debug, | ||
{ | ||
let res = main.await; | ||
let status = match res { | ||
Ok(t) => return Ok(t), | ||
Err(s) => s, | ||
}; | ||
|
||
debug!( | ||
"with_connection_fallback: initial call failed with: {:?}", | ||
status | ||
); | ||
let source = match status.source() { | ||
Some(source) => source, | ||
None => return Err(status), | ||
}; | ||
|
||
let error: &tonic::transport::Error = match source.downcast_ref() { | ||
Some(error) => error, | ||
None => return Err(status), | ||
}; | ||
|
||
if error.to_string() != "transport error" { | ||
return Err(status); | ||
} | ||
|
||
let source = match error.source() { | ||
Some(source) => source, | ||
None => return Err(status), | ||
}; | ||
|
||
if !source.to_string().contains("keep-alive timed out") { | ||
return Err(status); | ||
} | ||
|
||
debug!( | ||
"with_connection_fallback: initial call failed due to keepalive | ||
timeout. Retrying fallback." | ||
); | ||
|
||
fallback().await | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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 wonder if we need the "main" future or we can use the fallback also for the first attempt.
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 tried this, if you want you can try yourself too.
I tried by passing in a client to the function and having a
Fn
that operates on the client. I got stuck on lifetimes. It would be the cleaner way though.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 think we can use a macro here so we can use it like this:
I will give it a try
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.
It seems that this macro should do it:
Then it can be used as: