Repository navigation
generic channel: Add send_warn / send_ignore helpers - #45311
Conversation
| /// | ||
| /// In cases where channel closure is possible (because the receiver does not exist anymore), | ||
| /// this convenience method can be used to ignore the result and log the error as a warning. | ||
| fn send_warn(&self, message: T, error_context: &str) { |
There was a problem hiding this comment.
I guess this could also be send_log if we wanted to make the log-level configurable.
There was a problem hiding this comment.
Yeah that would me more evident (maybe send_with_log)
There was a problem hiding this comment.
I think I lean towards send_or_warn and send_or_log (if that's needed), rather than making all callers add a log level.
There was a problem hiding this comment.
i guess for now this is sufficient, and we can add something else later if we think something should be error or debug instead. Haven't seen anything yet where I felt that is necessary, but perhaps that would be the case when i start porting.
|
I feel like this in general is a good idea in general and I feel like having the default be warn would be wise. |
shubhamg13
left a comment
There was a problem hiding this comment.
Thumps Up 👍
I would suggest little more evident names.
| /// | ||
| /// In cases where channel closure is possible (because the receiver does not exist anymore), | ||
| /// this convenience method can be used to ignore the result and log the error as a warning. | ||
| fn send_warn(&self, message: T, error_context: &str) { |
There was a problem hiding this comment.
Yeah that would me more evident (maybe send_with_log)
| #[inline] | ||
| pub fn send_warn(&self, msg: T, error_context: &str) { | ||
| if let Err(error) = self.send(msg) { | ||
| log::warn!("{error_context}: {error}"); |
There was a problem hiding this comment.
If we use #[track_caller] we could try avoiding an error_context argument and use https://doc.rust-lang.org/stable/std/panic/struct.Location.html#method.caller to create that context instead.
There was a problem hiding this comment.
I removed the context argument for now, since the caller should be sufficient in many cases. There are some cases where we have existing error messages with more context, but i guess we can decide that when porting if its worth preserving.
Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
26d2a6a to
9e78b6a
Compare
Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
|
Since there were no objections I guess this is ready for review now. I added oneshot and buffered channel support too, but skipped LazyCallback for now (need to think about that more). |
As part of tackling servo#40744, we should reduce the amount of `unwrap()` we do on send / receive operations, since channel closures can happen (think e.g. the user closes a tab / window, while some request was still in-flight). Obviously it depends on the specific channel usage if this should be expected or not, but I believe there are many cases in servo where we currently unwrap, but shouldn't be. I'm thinking that adding helper functions like `send_warn`, might ease the transition, since it keeps the diff smaller and reduces the amount of boilerplate `if let Err(e) ....` or `.inspect_err()`. In cases where the channel being closed is fine `send_or_ignore` should probably be used, although when changing large parts of code perhaps defaulting to `send_or_warn` might be easier than figuring out if the warning message should be added or is just noise. This PR is mainly a first example, to add APIs and get consuses. Porting can be done in focused follow-up PRs, perhaps as part of a less-complex issue. Testing: This adds a helper method to ignore / log errors, which is something that can also be done manually already. Hence more of a refactor and no need for explicit tests. --------- Signed-off-by: Jonathan Schwender <[email protected]>
As part of tackling #40744, we should reduce the amount of
unwrap()we do on send / receive operations, since channel closures can happen (think e.g. the user closes a tab / window, while some request was still in-flight).Obviously it depends on the specific channel usage if this should be expected or not, but I believe there are many cases in servo where we currently unwrap, but shouldn't be.
I'm thinking that adding helper functions like
send_warn, might ease the transition, since it keeps the diff smaller and reduces the amount of boilerplateif let Err(e) ....or.inspect_err().In cases where the channel being closed is fine
send_or_ignoreshould probably be used, although when changing large parts of code perhaps defaulting tosend_or_warnmight be easier than figuring out if the warning message should be added or is just noise.This PR is mainly a first example, to add APIs and get consuses. Porting can be done in focused follow-up PRs, perhaps as part of a less-complex issue.
Testing: This adds a helper method to ignore / log errors, which is something that can also be done manually already. Hence more of a refactor and no need for explicit tests.