Skip to content

Commit f495152

Browse files
committed
Only use a timeout for the first inbound kad message
1 parent 0a48130 commit f495152

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

protocols/kad/src/handler.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ struct ProtocolStatus {
109109
enum InboundSubstreamState {
110110
/// Waiting for a request from the remote.
111111
WaitingMessage {
112-
/// Whether it is the first message to be awaited on this stream.
113-
first: bool,
112+
/// How long before we give up on waiting for the first request on this stream.
113+
/// `None` if there has already been a request on this stream, or it has timed out and can
114+
/// be re-used.
115+
first_request_timeout: Option<Delay>,
114116
connection_id: UniqueConnecId,
115117
substream: KadInStreamSink<Stream>,
116-
/// How long before we give up on waiting for a request.
117-
request_timeout: Delay,
118118
},
119119
/// Waiting for the behaviour to send a [`HandlerIn`] event containing the response.
120120
WaitingBehaviour(
@@ -198,10 +198,9 @@ impl InboundSubstreamState {
198198
},
199199
) {
200200
InboundSubstreamState::WaitingMessage {
201+
first_request_timeout: _,
201202
substream,
202-
first: _,
203203
connection_id: _,
204-
request_timeout: _,
205204
}
206205
| InboundSubstreamState::WaitingBehaviour(_, substream, _, _)
207206
| InboundSubstreamState::PendingSend(_, substream, _, _)
@@ -560,7 +559,10 @@ impl Handler {
560559
matches!(
561560
s,
562561
// An inbound substream waiting to be reused.
563-
InboundSubstreamState::WaitingMessage { first: false, .. }
562+
InboundSubstreamState::WaitingMessage {
563+
first_request_timeout: None,
564+
..
565+
}
564566
)
565567
}) {
566568
*s = InboundSubstreamState::Cancelled;
@@ -583,10 +585,9 @@ impl Handler {
583585
self.next_connec_unique_id.0 += 1;
584586
self.inbound_substreams
585587
.push(InboundSubstreamState::WaitingMessage {
586-
first: true,
588+
first_request_timeout: Some(Delay::new(SUBSTREAM_TIMEOUT)),
587589
connection_id: connec_unique_id,
588590
substream: protocol,
589-
request_timeout: Delay::new(SUBSTREAM_TIMEOUT),
590591
});
591592
}
592593

@@ -916,13 +917,12 @@ impl futures::Stream for InboundSubstreamState {
916917
},
917918
) {
918919
InboundSubstreamState::WaitingMessage {
919-
first,
920+
mut first_request_timeout,
920921
connection_id,
921922
mut substream,
922-
mut request_timeout,
923923
} => match (
924924
substream.poll_next_unpin(cx),
925-
request_timeout.poll_unpin(cx),
925+
first_request_timeout.as_mut().map(|t| t.poll_unpin(cx)),
926926
) {
927927
// Prefer ready requests over ready timeouts
928928
(Poll::Ready(Some(Ok(KadRequestMsg::Ping))), _) => {
@@ -966,13 +966,11 @@ impl futures::Stream for InboundSubstreamState {
966966
)));
967967
}
968968
(Poll::Ready(Some(Ok(KadRequestMsg::AddProvider { key, provider }))), _) => {
969-
// The request has finished, so renew the request timeout
970-
let request_timeout = Delay::new(SUBSTREAM_TIMEOUT);
969+
// This request type requires no response
971970
*this = InboundSubstreamState::WaitingMessage {
972-
first: false,
971+
first_request_timeout: None,
973972
connection_id,
974973
substream,
975-
request_timeout,
976974
};
977975
return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour(
978976
HandlerEvent::AddProvider { key, provider },
@@ -1012,30 +1010,28 @@ impl futures::Stream for InboundSubstreamState {
10121010
},
10131011
)));
10141012
}
1015-
(Poll::Pending, Poll::Pending) => {
1013+
(Poll::Pending, Some(Poll::Pending)) | (Poll::Pending, None) => {
10161014
// Keep the original request timeout
10171015
*this = InboundSubstreamState::WaitingMessage {
1018-
first,
1016+
first_request_timeout,
10191017
connection_id,
10201018
substream,
1021-
request_timeout,
10221019
};
10231020
return Poll::Pending;
10241021
}
1025-
(Poll::Pending, Poll::Ready(())) => {
1022+
(Poll::Pending, Some(Poll::Ready(()))) => {
10261023
tracing::debug!(
1027-
?first,
1024+
first = ?first_request_timeout.is_some(),
10281025
?connection_id,
10291026
"Inbound substream timed out waiting for request",
10301027
);
10311028

1032-
// Renew the request timeout, but mark this substream as available for re-use
1033-
let request_timeout = Delay::new(SUBSTREAM_TIMEOUT);
1029+
// Drop the first request timeout, and mark this substream as available for
1030+
// re-use
10341031
*this = InboundSubstreamState::WaitingMessage {
1035-
first: false,
1032+
first_request_timeout: None,
10361033
connection_id,
10371034
substream,
1038-
request_timeout,
10391035
};
10401036
return Poll::Pending;
10411037
}
@@ -1130,10 +1126,9 @@ impl futures::Stream for InboundSubstreamState {
11301126
) {
11311127
(Poll::Ready(Ok(())), _) => {
11321128
*this = InboundSubstreamState::WaitingMessage {
1133-
first: false,
1129+
first_request_timeout: None,
11341130
connection_id: id,
11351131
substream,
1136-
request_timeout: Delay::new(SUBSTREAM_TIMEOUT),
11371132
};
11381133
}
11391134
(Poll::Pending, Poll::Pending) => {

0 commit comments

Comments
 (0)