-
Notifications
You must be signed in to change notification settings - Fork 992
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
Refactor SyncState #3297
Refactor SyncState #3297
Conversation
Method sync_error() retrun type was simplified. update_txhashset_download() was made type safe, which eliminates a runtime enum variant's check
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.
Makes sense to me.
Question about the RwLockReadGuard
. This feels like a leaky abstraction to me.
The must be a way around this somehow. Maybe we need to reconstruct the actual error somehow from its low level contents.
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.
👍
@@ -114,37 +144,54 @@ impl SyncState { | |||
} | |||
|
|||
/// Update the syncing status | |||
pub fn update(&self, new_status: SyncStatus) { | |||
if self.status() == new_status { |
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 check is not great, we take a read lock, compare and then release the lock, so when we obtain a write lock the state may be different already.
/// Update the syncing status if predicate f is satisfied | ||
pub fn update_if<F>(&self, new_status: SyncStatus, f: F) -> bool | ||
where | ||
F: Fn(SyncStatus) -> bool, |
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's a bit awkward to have predicate here, but some variants of SyncStatus
have state attached which makes matching painful
/// Communicate sync error | ||
pub fn set_sync_error(&self, error: Error) { | ||
*self.sync_error.write() = Some(error); | ||
} | ||
|
||
/// Get sync error | ||
pub fn sync_error(&self) -> Arc<RwLock<Option<Error>>> { | ||
Arc::clone(&self.sync_error) | ||
pub fn sync_error(&self) -> Option<String> { |
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 don't need Error per se, just the fact of its existence and description, returning String makes life much easier
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 that makes sense.
Method
sync_error()
return type was simplified.update_txhashset_download()
was made type safe, which eliminates a runtime enum variant's check. As a bonus matching code is shorter and simpler when we do a partial destructuing, which happens quite often.