This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Chain head subscription #126
+462
−105
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a516ede
Start WebSockets server.
tomusdrw af81785
Expose non-working subscription.
tomusdrw af88a13
Merge branch 'master' into td-pubsub
tomusdrw 292db18
Dummy subscription for testing.
tomusdrw 8816443
Proper implementation with event loop.
tomusdrw d7b2734
Finalized pubsub.
tomusdrw b31f0d5
Bump clap.
tomusdrw 7f3a621
Fix yml.
tomusdrw af7f683
Disable WS logs.
tomusdrw 789ea46
Merge branch 'master' into td-pubsub
gavofyork 9193b89
Remove stale TransactionHash mention
gavofyork 921d7c4
Fix build from nightly API change.
gavofyork 73106ab
Don't panic on invalid port.
tomusdrw 2da7ac0
Bind server to random port.
tomusdrw eaba689
Merge branch 'master' into td-pubsub
tomusdrw 704f755
Send only best blocks.
tomusdrw 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
Proper implementation with event loop.
commit 88164439649f63bec1added6d1bc92468ca28ef2
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
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,86 @@ | ||
// Copyright 2017-2018 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate. | ||
|
||
// Substrate is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use std::collections::HashMap; | ||
use std::sync::atomic::{self, AtomicUsize}; | ||
|
||
use jsonrpc_macros::pubsub; | ||
use jsonrpc_pubsub::SubscriptionId; | ||
use parking_lot::Mutex; | ||
use rpc::futures::sync::oneshot; | ||
use rpc::futures::{Future, future}; | ||
use tokio_core::reactor::Remote; | ||
|
||
type Id = u64; | ||
|
||
/// Subscriptions manager. | ||
/// | ||
/// Takes care of assigning unique subscription ids and | ||
/// driving the sinks into completion. | ||
#[derive(Debug)] | ||
pub struct Subscriptions { | ||
next_id: AtomicUsize, | ||
active_subscriptions: Mutex<HashMap<Id, oneshot::Sender<()>>>, | ||
event_loop: Remote, | ||
} | ||
|
||
impl Subscriptions { | ||
/// Creates new `Subscriptions` object. | ||
pub fn new(event_loop: Remote) -> Self { | ||
Subscriptions { | ||
next_id: Default::default(), | ||
active_subscriptions: Default::default(), | ||
event_loop, | ||
} | ||
} | ||
|
||
/// Creates new subscription for given subscriber. | ||
/// | ||
/// Second parameter is a function that converts Subscriber sink into a future. | ||
/// This future will be driven to completion bu underlying event loop | ||
/// or will be cancelled in case #cancel is invoked. | ||
pub fn add<T, E, G, R, F>(&self, subscriber: pubsub::Subscriber<T, E>, into_future: G) where | ||
G: FnOnce(pubsub::Sink<T, E>) -> R, | ||
R: future::IntoFuture<Future=F, Item=(), Error=()>, | ||
F: future::Future<Item=(), Error=()> + Send + 'static, | ||
{ | ||
let id = self.next_id.fetch_add(1, atomic::Ordering::AcqRel) as u64; | ||
if let Ok(sink) = subscriber.assign_id(id.into()) { | ||
let (tx, rx) = oneshot::channel(); | ||
let future = into_future(sink) | ||
.into_future() | ||
.select(rx.map_err(|e| warn!("Error timeing out: {:?}", e))) | ||
.map(|_| ()) | ||
.map_err(|_| ()); | ||
|
||
self.active_subscriptions.lock().insert(id, tx); | ||
self.event_loop.spawn(|_| future); | ||
} | ||
} | ||
|
||
/// Cancel subscription. | ||
/// | ||
/// Returns true if subscription existed or false otherwise. | ||
pub fn cancel(&self, id: SubscriptionId) -> bool { | ||
if let SubscriptionId::Number(id) = id { | ||
if let Some(tx) = self.active_subscriptions.lock().remove(&id) { | ||
let _ = tx.send(()); | ||
return true; | ||
} | ||
} | ||
false | ||
} | ||
} |
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.
Should filter by
notification.is_best