Skip to content
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

seed command #83

Merged
merged 15 commits into from
Nov 13, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions zebrad/src/commands/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use crate::{config::ZebradConfig, prelude::*};
/// Whether our `SeedService` is poll_ready or not.
#[derive(Debug)]
enum SeederState {
// This is kinda gross but ¯\_(ツ)_/¯
TempState,
/// Waiting for the address book to be shared with us via the oneshot channel.
AwaitingAddressBook(oneshot::Receiver<Arc<Mutex<AddressBook>>>),
/// Address book received, ready to service requests.
Expand All @@ -39,37 +37,27 @@ impl Service<Request> for SeedService {
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
debug!("SeedService.state: {:?}", self.state);

let mut poll_result = Poll::Pending;

// We want to be able to consume the state, but it's behind a mutable
// reference, so we can't move it out of self without swapping in a
// placeholder, even if we immediately overwrite the placeholder.
let tmp_state = std::mem::replace(&mut self.state, SeederState::TempState);

self.state = match tmp_state {
SeederState::AwaitingAddressBook(mut rx) => match rx.try_recv() {
match self.state {
SeederState::Ready(_) => return Poll::Ready(Ok(())),
SeederState::AwaitingAddressBook(ref mut rx) => match rx.try_recv() {
Err(e) => {
error!("SeedService oneshot sender dropped: {:?}", e);
return Poll::Ready(Err(e.into()));
}
Ok(None) => {
debug!("SeedService got a message with `None` in it. 🤔");
dconnolly marked this conversation as resolved.
Show resolved Hide resolved
return Poll::Pending;
}
Ok(Some(address_book)) => {
info!(
"SeedService received address_book via oneshot {:?}",
address_book
);
poll_result = Poll::Ready(Ok(()));
SeederState::Ready(address_book)
self.state = SeederState::Ready(address_book);
return Poll::Ready(Ok(()));
}
// Sets self.state to a new instance of what it
// already was; we can't just return `tmp_state`
// because we've plucked it apart via `rx` and moved
// parts around already in this block.
_ => SeederState::AwaitingAddressBook(rx),
},
SeederState::Ready(_) => {
poll_result = Poll::Ready(Ok(()));
tmp_state
}
SeederState::TempState => tmp_state,
};

return poll_result;
}
}

fn call(&mut self, req: Request) -> Self::Future {
Expand Down