-
Notifications
You must be signed in to change notification settings - Fork 51
Periodic snapshot #1869
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
Merged
remagpie
merged 3 commits into
CodeChain-io:feature/snapshot
from
foriequal0:snapshot/periodic
Nov 18, 2019
Merged
Periodic snapshot #1869
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| use ctypes::BlockHash; | ||
|
|
||
| use parking_lot::RwLock; | ||
| use std::sync::mpsc::{sync_channel, Receiver, RecvError, SyncSender}; | ||
| use std::sync::{Arc, Weak}; | ||
|
|
||
| pub fn create() -> (NotifySender, NotifyReceiverSource) { | ||
| let (tx, rx) = sync_channel(1); | ||
| let tx = Arc::new(RwLock::new(Some(tx))); | ||
| let tx_weak = Arc::downgrade(&tx); | ||
| ( | ||
| NotifySender { | ||
| tx, | ||
| }, | ||
| NotifyReceiverSource( | ||
| ReceiverCanceller { | ||
| tx: tx_weak, | ||
| }, | ||
| NotifyReceiver { | ||
| rx, | ||
| }, | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| pub struct NotifySender { | ||
| tx: Arc<RwLock<Option<SyncSender<BlockHash>>>>, | ||
| } | ||
|
|
||
| impl NotifySender { | ||
| pub fn notify(&self, block_hash: BlockHash) { | ||
| let guard = self.tx.read(); | ||
| if let Some(tx) = guard.as_ref() { | ||
| // TODO: Ignore the error. Receiver thread might be terminated or congested. | ||
| let _ = tx.try_send(block_hash); | ||
| } else { | ||
| // ReceiverCanceller is dropped. | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct NotifyReceiverSource(pub ReceiverCanceller, pub NotifyReceiver); | ||
|
|
||
| /// Dropping this makes the receiver stopped. | ||
| /// | ||
| /// `recv()` method of the `Receiver` will stop and return `RecvError` when corresponding `Sender` is dropped. | ||
| /// This is an inherited behaviour of `std::sync::mpsc::{Sender, Receiver}`. | ||
| /// However, we need another way to stop the `Receiver`, since `Sender` is usually shared throughout our codes. | ||
| /// We can't collect them all and destory one by one. We need a kill switch. | ||
| /// | ||
| /// `ReceiverCanceller` holds weak reference to the `Sender`, so it doesn't prohibit the default behaviour. | ||
| /// Then, we can upgrade the weak reference and get the shared reference to `Sender` itself, and manually drop it with this. | ||
| pub struct ReceiverCanceller { | ||
| tx: Weak<RwLock<Option<SyncSender<BlockHash>>>>, | ||
| } | ||
|
|
||
| impl Drop for ReceiverCanceller { | ||
| fn drop(&mut self) { | ||
| if let Some(tx) = self.tx.upgrade() { | ||
| let mut guard = tx.write(); | ||
| if let Some(sender) = guard.take() { | ||
| drop(sender) | ||
| } | ||
| } else { | ||
| // All NotifySender is dropped. No droppable Sender. | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Receiver is dropped when | ||
| /// 1. There are no NotifySenders out there. | ||
| /// 2. ReceiverCanceller is dropped. See the comment of `ReceiverCanceller`. | ||
| pub struct NotifyReceiver { | ||
| rx: Receiver<BlockHash>, | ||
| } | ||
|
|
||
| impl NotifyReceiver { | ||
| pub fn recv(&self) -> Result<BlockHash, RecvError> { | ||
| self.rx.recv() | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.