Bob's keep alive

Initially set to 5 seconds.
In poll fn upon new swap event (and opening substream) set to Yes.
In poll fn upon completing the future of the swap setup set to No.
This commit is contained in:
Daniel Karzel 2021-06-25 10:48:42 +10:00
parent abdf272178
commit 668c912033
No known key found for this signature in database
GPG Key ID: 30C3FC2E438ADB6E

View File

@ -19,7 +19,7 @@ use libp2p::{Multiaddr, PeerId};
use std::collections::VecDeque; use std::collections::VecDeque;
use std::sync::Arc; use std::sync::Arc;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use std::time::Duration; use std::time::{Duration, Instant};
use uuid::Uuid; use uuid::Uuid;
use void::Void; use void::Void;
@ -105,6 +105,7 @@ pub struct Handler {
timeout: Duration, timeout: Duration,
new_swaps: VecDeque<NewSwap>, new_swaps: VecDeque<NewSwap>,
bitcoin_wallet: Arc<bitcoin::Wallet>, bitcoin_wallet: Arc<bitcoin::Wallet>,
keep_alive: KeepAlive,
} }
impl Handler { impl Handler {
@ -115,6 +116,7 @@ impl Handler {
timeout: Duration::from_secs(60), timeout: Duration::from_secs(60),
new_swaps: VecDeque::default(), new_swaps: VecDeque::default(),
bitcoin_wallet, bitcoin_wallet,
keep_alive: KeepAlive::Until(Instant::now() + Duration::from_secs(5)),
} }
} }
} }
@ -216,7 +218,7 @@ impl ProtocolsHandler for Handler {
} }
fn connection_keep_alive(&self) -> KeepAlive { fn connection_keep_alive(&self) -> KeepAlive {
todo!() self.keep_alive
} }
fn poll( fn poll(
@ -231,12 +233,14 @@ impl ProtocolsHandler for Handler {
>, >,
> { > {
if let Some(new_swap) = self.new_swaps.pop_front() { if let Some(new_swap) = self.new_swaps.pop_front() {
self.keep_alive = KeepAlive::Yes;
return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(protocol::new(), new_swap), protocol: SubstreamProtocol::new(protocol::new(), new_swap),
}); });
} }
if let Some(result) = futures::ready!(self.outbound_stream.poll_unpin(cx)) { if let Some(result) = futures::ready!(self.outbound_stream.poll_unpin(cx)) {
self.keep_alive = KeepAlive::No;
return Poll::Ready(ProtocolsHandlerEvent::Custom(Completed(result))); return Poll::Ready(ProtocolsHandlerEvent::Custom(Completed(result)));
} }