Skip to content

Commit 8053eb9

Browse files
committed
Only use a timeout for the first inbound kad message
1 parent 782a4ce commit 8053eb9

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

protocols/kad/src/handler.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ struct ProtocolStatus {
103103
enum InboundSubstreamState {
104104
/// Waiting for a request from the remote.
105105
WaitingMessage {
106-
/// Whether it is the first message to be awaited on this stream.
107-
first: bool,
106+
/// How long before we give up on waiting for the first request on this stream.
107+
/// `None` if there has already been a request on this stream, or it has timed out and can
108+
/// be re-used.
109+
first_request_timeout: Option<Delay>,
108110
connection_id: UniqueConnecId,
109111
substream: KadInStreamSink<Stream>,
110-
/// How long before we give up on waiting for a request.
111-
request_timeout: Delay,
112112
},
113113
/// Waiting for the behaviour to send a [`HandlerIn`] event containing the response.
114114
WaitingBehaviour(
@@ -191,10 +191,9 @@ impl InboundSubstreamState {
191191
},
192192
) {
193193
InboundSubstreamState::WaitingMessage {
194+
first_request_timeout: _,
194195
substream,
195-
first: _,
196196
connection_id: _,
197-
request_timeout: _,
198197
}
199198
| InboundSubstreamState::WaitingBehaviour(_, substream, _, _)
200199
| InboundSubstreamState::PendingSend(_, substream, _, _)
@@ -559,7 +558,10 @@ impl Handler {
559558
matches!(
560559
s,
561560
// An inbound substream waiting to be reused.
562-
InboundSubstreamState::WaitingMessage { first: false, .. }
561+
InboundSubstreamState::WaitingMessage {
562+
first_request_timeout: None,
563+
..
564+
}
563565
)
564566
}) {
565567
*s = InboundSubstreamState::Cancelled;
@@ -582,10 +584,9 @@ impl Handler {
582584
self.next_connec_unique_id.0 += 1;
583585
self.inbound_substreams
584586
.push(InboundSubstreamState::WaitingMessage {
585-
first: true,
587+
first_request_timeout: Some(Delay::new(SUBSTREAM_TIMEOUT)),
586588
connection_id: connec_unique_id,
587589
substream: protocol,
588-
request_timeout: Delay::new(SUBSTREAM_TIMEOUT),
589590
});
590591
}
591592

@@ -921,13 +922,12 @@ impl futures::Stream for InboundSubstreamState {
921922
},
922923
) {
923924
InboundSubstreamState::WaitingMessage {
924-
first,
925+
mut first_request_timeout,
925926
connection_id,
926927
mut substream,
927-
mut request_timeout,
928928
} => match (
929929
substream.poll_next_unpin(cx),
930-
request_timeout.poll_unpin(cx),
930+
first_request_timeout.as_mut().map(|t| t.poll_unpin(cx)),
931931
) {
932932
// Prefer ready requests over ready timeouts
933933
(Poll::Ready(Some(Ok(KadRequestMsg::Ping))), _) => {
@@ -971,13 +971,11 @@ impl futures::Stream for InboundSubstreamState {
971971
)));
972972
}
973973
(Poll::Ready(Some(Ok(KadRequestMsg::AddProvider { key, provider }))), _) => {
974-
// The request has finished, so renew the request timeout
975-
let request_timeout = Delay::new(SUBSTREAM_TIMEOUT);
974+
// This request type requires no response
976975
*this = InboundSubstreamState::WaitingMessage {
977-
first: false,
976+
first_request_timeout: None,
978977
connection_id,
979978
substream,
980-
request_timeout,
981979
};
982980
return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour(
983981
HandlerEvent::AddProvider { key, provider },
@@ -1017,30 +1015,28 @@ impl futures::Stream for InboundSubstreamState {
10171015
},
10181016
)));
10191017
}
1020-
(Poll::Pending, Poll::Pending) => {
1018+
(Poll::Pending, Some(Poll::Pending)) | (Poll::Pending, None) => {
10211019
// Keep the original request timeout
10221020
*this = InboundSubstreamState::WaitingMessage {
1023-
first,
1021+
first_request_timeout,
10241022
connection_id,
10251023
substream,
1026-
request_timeout,
10271024
};
10281025
return Poll::Pending;
10291026
}
1030-
(Poll::Pending, Poll::Ready(())) => {
1027+
(Poll::Pending, Some(Poll::Ready(()))) => {
10311028
tracing::debug!(
1032-
?first,
1029+
first = ?first_request_timeout.is_some(),
10331030
?connection_id,
10341031
"Inbound substream timed out waiting for request",
10351032
);
10361033

1037-
// Renew the request timeout, but mark this substream as available for re-use
1038-
let request_timeout = Delay::new(SUBSTREAM_TIMEOUT);
1034+
// Drop the first request timeout, and mark this substream as available for
1035+
// re-use
10391036
*this = InboundSubstreamState::WaitingMessage {
1040-
first: false,
1037+
first_request_timeout: None,
10411038
connection_id,
10421039
substream,
1043-
request_timeout,
10441040
};
10451041
return Poll::Pending;
10461042
}
@@ -1086,7 +1082,12 @@ impl futures::Stream for InboundSubstreamState {
10861082
return Poll::Pending;
10871083
}
10881084
},
1089-
InboundSubstreamState::PendingSend(id, mut substream, msg, mut response_timeout) => {
1085+
InboundSubstreamState::PendingSend(
1086+
id,
1087+
mut substream,
1088+
msg,
1089+
mut response_timeout,
1090+
) => {
10901091
match (
10911092
substream.poll_ready_unpin(cx),
10921093
response_timeout.poll_unpin(cx),
@@ -1130,10 +1131,9 @@ impl futures::Stream for InboundSubstreamState {
11301131
) {
11311132
(Poll::Ready(Ok(())), _) => {
11321133
*this = InboundSubstreamState::WaitingMessage {
1133-
first: false,
1134+
first_request_timeout: None,
11341135
connection_id: id,
11351136
substream,
1136-
request_timeout: Delay::new(SUBSTREAM_TIMEOUT),
11371137
};
11381138
}
11391139
(Poll::Pending, Poll::Pending) => {

0 commit comments

Comments
 (0)