Properly shut down protocols handler on useless timeout (#892)

This commit is contained in:
Pierre Krieger
2019-01-25 15:56:32 +01:00
committed by GitHub
parent 18db0e806b
commit 30c082dfe5

View File

@ -289,50 +289,55 @@ where
// Poll the handler at the end so that we see the consequences of the method calls on // Poll the handler at the end so that we see the consequences of the method calls on
// `self.handler`. // `self.handler`.
let poll_result = self.handler.poll()?; loop {
let poll_result = self.handler.poll()?;
if self.handler.connection_keep_alive() { if self.handler.connection_keep_alive() {
self.connection_shutdown = None; self.connection_shutdown = None;
} else if self.connection_shutdown.is_none() { } else if self.connection_shutdown.is_none() {
self.connection_shutdown = Some(Delay::new(Instant::now() + self.useless_timeout)); self.connection_shutdown = Some(Delay::new(Instant::now() + self.useless_timeout));
}
match poll_result {
Async::Ready(ProtocolsHandlerEvent::Custom(event)) => {
return Ok(Async::Ready(NodeHandlerEvent::Custom(event)));
} }
Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
upgrade,
info,
}) => {
let id = self.unique_dial_upgrade_id;
self.unique_dial_upgrade_id += 1;
self.queued_dial_upgrades.push((id, upgrade));
return Ok(Async::Ready(
NodeHandlerEvent::OutboundSubstreamRequest((id, info)),
));
}
Async::Ready(ProtocolsHandlerEvent::Shutdown) => {
return Ok(Async::Ready(NodeHandlerEvent::Shutdown))
},
Async::NotReady => (),
};
// Check the `connection_shutdown`. match poll_result {
if let Some(mut connection_shutdown) = self.connection_shutdown.take() { Async::Ready(ProtocolsHandlerEvent::Custom(event)) => {
// If we're negotiating substreams, let's delay the closing. return Ok(Async::Ready(NodeHandlerEvent::Custom(event)));
if self.negotiating_in.is_empty() && self.negotiating_out.is_empty() { }
match connection_shutdown.poll() { Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
Ok(Async::Ready(_)) | Err(_) => { upgrade,
return Ok(Async::Ready(NodeHandlerEvent::Shutdown)) info,
}, }) => {
Ok(Async::NotReady) => { let id = self.unique_dial_upgrade_id;
self.connection_shutdown = Some(connection_shutdown); self.unique_dial_upgrade_id += 1;
} self.queued_dial_upgrades.push((id, upgrade));
return Ok(Async::Ready(
NodeHandlerEvent::OutboundSubstreamRequest((id, info)),
));
}
Async::Ready(ProtocolsHandlerEvent::Shutdown) => {
return Ok(Async::Ready(NodeHandlerEvent::Shutdown))
},
Async::NotReady => (),
};
// Check the `connection_shutdown`.
if let Some(mut connection_shutdown) = self.connection_shutdown.take() {
// If we're negotiating substreams, let's delay the closing.
if self.negotiating_in.is_empty() && self.negotiating_out.is_empty() {
match connection_shutdown.poll() {
Ok(Async::Ready(_)) | Err(_) => {
self.shutdown();
continue; // We need to poll the handler again.
},
Ok(Async::NotReady) => {
self.connection_shutdown = Some(connection_shutdown);
}
}
} else {
self.connection_shutdown = Some(connection_shutdown);
} }
} else {
self.connection_shutdown = Some(connection_shutdown);
} }
break;
} }
Ok(Async::NotReady) Ok(Async::NotReady)