Clean up SeedService.poll_ready with a 'ref mut'

Co-Authored-By: Henry de Valence <hdevalence@hdevalence.ca>
This commit is contained in:
Deirdre Connolly 2019-11-12 15:00:56 -05:00 committed by Deirdre Connolly
parent 9d8e32d05f
commit 0f20ff59c7
1 changed files with 14 additions and 26 deletions

View File

@ -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.
@ -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. 🤔");
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 {