1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
// Copyright 2023 litep2p developers
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! Request-response protocol implementation.
use crate::{
error::{Error, NegotiationError, SubstreamError},
multistream_select::NegotiationError::Failed as MultistreamFailed,
protocol::{
request_response::handle::{InnerRequestResponseEvent, RequestResponseCommand},
Direction, TransportEvent, TransportService,
},
substream::{Substream, SubstreamSet},
types::{protocol::ProtocolName, RequestId, SubstreamId},
PeerId,
};
use bytes::BytesMut;
use futures::{channel, future::BoxFuture, stream::FuturesUnordered, StreamExt};
use tokio::{
sync::{
mpsc::{Receiver, Sender},
oneshot,
},
time::sleep,
};
use std::{
collections::{hash_map::Entry, HashMap, HashSet},
io::ErrorKind,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
time::Duration,
};
pub use config::{Config, ConfigBuilder};
pub use handle::{
DialOptions, RejectReason, RequestResponseError, RequestResponseEvent, RequestResponseHandle,
};
mod config;
mod handle;
#[cfg(test)]
mod tests;
// TODO: add ability to specify limit for inbound requests?
// TODO: convert inbound/outbound substreams to use `oneshot:Sender<()>` for sending/rejecting
// responses. this way, the code dealing with rejecting/responding doesn't have to block.
/// Logging target for the file.
const LOG_TARGET: &str = "litep2p::request-response::protocol";
/// Default request timeout.
const REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
/// Pending request.
type PendingRequest = (
PeerId,
RequestId,
Option<ProtocolName>,
Result<Vec<u8>, RequestResponseError>,
);
/// Request context.
struct RequestContext {
/// Peer ID.
peer: PeerId,
/// Request ID.
request_id: RequestId,
/// Request.
request: Vec<u8>,
/// Fallback request.
fallback: Option<(ProtocolName, Vec<u8>)>,
}
impl RequestContext {
/// Create new [`RequestContext`].
fn new(
peer: PeerId,
request_id: RequestId,
request: Vec<u8>,
fallback: Option<(ProtocolName, Vec<u8>)>,
) -> Self {
Self {
peer,
request_id,
request,
fallback,
}
}
}
/// Peer context.
struct PeerContext {
/// Active requests.
active: HashSet<RequestId>,
/// Active inbound requests and their fallback names.
active_inbound: HashMap<RequestId, Option<ProtocolName>>,
}
impl PeerContext {
/// Create new [`PeerContext`].
fn new() -> Self {
Self {
active: HashSet::new(),
active_inbound: HashMap::new(),
}
}
}
/// Request-response protocol.
pub(crate) struct RequestResponseProtocol {
/// Transport service.
service: TransportService,
/// Protocol.
protocol: ProtocolName,
/// Connected peers.
peers: HashMap<PeerId, PeerContext>,
/// Pending outbound substreams, mapped from `SubstreamId` to `RequestId`.
pending_outbound: HashMap<SubstreamId, RequestContext>,
/// Pending outbound responses.
///
/// The future listens to a `oneshot::Sender` which is given to `RequestResponseHandle`.
/// If the request is accepted by the local node, the response is sent over the channel to the
/// the future which sends it to remote peer and closes the substream.
///
/// If the substream is rejected by the local node, the `oneshot::Sender` is dropped which
/// notifies the future that the request should be rejected by closing the substream.
pending_outbound_responses: FuturesUnordered<BoxFuture<'static, ()>>,
/// Pending inbound responses.
pending_inbound: FuturesUnordered<BoxFuture<'static, PendingRequest>>,
/// Pending outbound cancellation handles.
pending_outbound_cancels: HashMap<RequestId, oneshot::Sender<()>>,
/// Pending inbound requests.
pending_inbound_requests: SubstreamSet<(PeerId, RequestId), Substream>,
/// Pending dials for outbound requests.
pending_dials: HashMap<PeerId, RequestContext>,
/// TX channel for sending events to the user protocol.
event_tx: Sender<InnerRequestResponseEvent>,
/// RX channel for receive commands from the `RequestResponseHandle`.
command_rx: Receiver<RequestResponseCommand>,
/// Next request ID.
///
/// Inbound requests are assigned an ephemeral ID TODO: finish
next_request_id: Arc<AtomicUsize>,
/// Timeout for outbound requests.
timeout: Duration,
/// Maximum concurrent inbound requests, if specified.
max_concurrent_inbound_requests: Option<usize>,
}
impl RequestResponseProtocol {
/// Create new [`RequestResponseProtocol`].
pub(crate) fn new(service: TransportService, config: Config) -> Self {
Self {
service,
peers: HashMap::new(),
timeout: config.timeout,
next_request_id: config.next_request_id,
event_tx: config.event_tx,
command_rx: config.command_rx,
protocol: config.protocol_name,
pending_dials: HashMap::new(),
pending_outbound: HashMap::new(),
pending_inbound: FuturesUnordered::new(),
pending_outbound_cancels: HashMap::new(),
pending_inbound_requests: SubstreamSet::new(),
pending_outbound_responses: FuturesUnordered::new(),
max_concurrent_inbound_requests: config.max_concurrent_inbound_request,
}
}
/// Get next ephemeral request ID.
fn next_request_id(&mut self) -> RequestId {
RequestId::from(self.next_request_id.fetch_add(1usize, Ordering::Relaxed))
}
/// Connection established to remote peer.
async fn on_connection_established(&mut self, peer: PeerId) -> crate::Result<()> {
tracing::debug!(target: LOG_TARGET, ?peer, protocol = %self.protocol, "connection established");
let Entry::Vacant(entry) = self.peers.entry(peer) else {
tracing::error!(
target: LOG_TARGET,
?peer,
"state mismatch: peer already exists",
);
debug_assert!(false);
return Err(Error::PeerAlreadyExists(peer));
};
match self.pending_dials.remove(&peer) {
None => {
entry.insert(PeerContext::new());
}
Some(context) => match self.service.open_substream(peer) {
Ok(substream_id) => {
tracing::trace!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
request_id = ?context.request_id,
?substream_id,
"dial succeeded, open substream",
);
entry.insert(PeerContext {
active: HashSet::from_iter([context.request_id]),
active_inbound: HashMap::new(),
});
self.pending_outbound.insert(
substream_id,
RequestContext::new(
peer,
context.request_id,
context.request,
context.fallback,
),
);
}
// only reason the substream would fail to open would be that the connection
// would've been reported to the protocol with enough delay that the keep-alive
// timeout had expired and no other protocol had opened a substream to it, causing
// the connection to be closed
Err(error) => {
tracing::warn!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
request_id = ?context.request_id,
?error,
"failed to open substream",
);
return self
.report_request_failure(
peer,
context.request_id,
RequestResponseError::Rejected(error.into()),
)
.await;
}
},
}
Ok(())
}
/// Connection closed to remote peer.
async fn on_connection_closed(&mut self, peer: PeerId) {
tracing::debug!(target: LOG_TARGET, ?peer, protocol = %self.protocol, "connection closed");
let Some(context) = self.peers.remove(&peer) else {
tracing::error!(
target: LOG_TARGET,
?peer,
"state mismatch: peer doesn't exist",
);
debug_assert!(false);
return;
};
// sent failure events for all pending outbound requests
for request_id in context.active {
let _ = self
.event_tx
.send(InnerRequestResponseEvent::RequestFailed {
peer,
request_id,
error: RequestResponseError::Rejected(RejectReason::ConnectionClosed),
})
.await;
}
// remove all pending inbound requests
for (request_id, _) in context.active_inbound {
self.pending_inbound_requests.remove(&(peer, request_id));
}
}
/// Local node opened a substream to remote node.
async fn on_outbound_substream(
&mut self,
peer: PeerId,
substream_id: SubstreamId,
mut substream: Substream,
fallback_protocol: Option<ProtocolName>,
) -> crate::Result<()> {
let Some(RequestContext {
request_id,
request,
fallback,
..
}) = self.pending_outbound.remove(&substream_id)
else {
tracing::error!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?substream_id,
"pending outbound request does not exist",
);
debug_assert!(false);
return Err(Error::InvalidState);
};
tracing::trace!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?substream_id,
?request_id,
"substream opened, send request",
);
let request = match (&fallback_protocol, fallback) {
(Some(protocol), Some((fallback_protocol, fallback_request)))
if protocol == &fallback_protocol =>
fallback_request,
_ => request,
};
let request_timeout = self.timeout;
let protocol = self.protocol.clone();
let (tx, rx) = oneshot::channel();
self.pending_outbound_cancels.insert(request_id, tx);
self.pending_inbound.push(Box::pin(async move {
match tokio::time::timeout(request_timeout, substream.send_framed(request.into())).await
{
Err(_) => (
peer,
request_id,
fallback_protocol,
Err(RequestResponseError::Timeout),
),
Ok(Err(SubstreamError::IoError(ErrorKind::PermissionDenied))) => {
tracing::warn!(
target: LOG_TARGET,
?peer,
%protocol,
"tried to send too large request",
);
(
peer,
request_id,
fallback_protocol,
Err(RequestResponseError::TooLargePayload),
)
}
Ok(Err(error)) => (
peer,
request_id,
fallback_protocol,
Err(RequestResponseError::Rejected(error.into())),
),
Ok(Ok(_)) => {
tokio::select! {
_ = rx => {
tracing::debug!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
"request canceled",
);
let _ = substream.close().await;
(
peer,
request_id,
fallback_protocol,
Err(RequestResponseError::Canceled))
}
_ = sleep(request_timeout) => {
tracing::debug!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
"request timed out",
);
let _ = substream.close().await;
(peer, request_id, fallback_protocol, Err(RequestResponseError::Timeout))
}
event = substream.next() => match event {
Some(Ok(response)) => {
(peer, request_id, fallback_protocol, Ok(response.freeze().into()))
},
Some(Err(error)) => {
(peer, request_id, fallback_protocol, Err(RequestResponseError::Rejected(error.into())))
},
None => {
tracing::debug!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
"substream closed",
);
(peer, request_id, fallback_protocol, Err(RequestResponseError::Rejected(RejectReason::SubstreamClosed)))
}
}
}
}
}
}));
Ok(())
}
/// Handle pending inbound response.
async fn on_inbound_request(
&mut self,
peer: PeerId,
request_id: RequestId,
request: Result<BytesMut, SubstreamError>,
) -> crate::Result<()> {
let fallback = self
.peers
.get_mut(&peer)
.ok_or(Error::PeerDoesntExist(peer))?
.active_inbound
.remove(&request_id)
.ok_or_else(|| {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
"no active inbound request",
);
Error::InvalidState
})?;
let mut substream =
self.pending_inbound_requests.remove(&(peer, request_id)).ok_or_else(|| {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
"request doesn't exist in pending requests",
);
Error::InvalidState
})?;
let protocol = self.protocol.clone();
tracing::trace!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
"inbound request",
);
let Ok(request) = request else {
tracing::debug!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
?request,
"failed to read request from substream",
);
return Err(Error::InvalidData);
};
// once the request has been read from the substream, start a future which waits
// for an input from the user.
//
// the input is either a response (succes) or rejection (failure) which is communicated
// by sending the response over the `oneshot::Sender` or closing it, respectively.
let timeout = self.timeout;
let (response_tx, rx): (
oneshot::Sender<(Vec<u8>, Option<channel::oneshot::Sender<()>>)>,
_,
) = oneshot::channel();
self.pending_outbound_responses.push(Box::pin(async move {
match rx.await {
Err(_) => {
tracing::debug!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
"request rejected",
);
let _ = substream.close().await;
}
Ok((response, mut feedback)) => {
tracing::trace!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
"send response",
);
match tokio::time::timeout(timeout, substream.send_framed(response.into()))
.await
{
Err(_) => tracing::debug!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
"timed out while sending response",
),
Ok(Ok(_)) => feedback.take().map_or((), |feedback| {
let _ = feedback.send(());
}),
Ok(Err(error)) => tracing::trace!(
target: LOG_TARGET,
?peer,
%protocol,
?request_id,
?error,
"failed to send request to peer",
),
}
}
}
}));
self.event_tx
.send(InnerRequestResponseEvent::RequestReceived {
peer,
fallback,
request_id,
request: request.freeze().into(),
response_tx,
})
.await
.map_err(From::from)
}
/// Remote opened a substream to local node.
async fn on_inbound_substream(
&mut self,
peer: PeerId,
fallback: Option<ProtocolName>,
substream: Substream,
) -> crate::Result<()> {
tracing::trace!(target: LOG_TARGET, ?peer, protocol = %self.protocol, "handle inbound substream");
if let Some(max_requests) = self.max_concurrent_inbound_requests {
let num_inbound_requests =
self.pending_inbound_requests.len() + self.pending_outbound_responses.len();
if max_requests <= num_inbound_requests {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?fallback,
?max_requests,
"rejecting request as already at maximum",
);
let _ = substream.close().await;
return Ok(());
}
}
// allocate ephemeral id for the inbound request and return it to the user protocol
//
// when user responds to the request, this is used to associate the response with the
// correct substream.
let request_id = self.next_request_id();
self.peers
.get_mut(&peer)
.ok_or(Error::PeerDoesntExist(peer))?
.active_inbound
.insert(request_id, fallback);
self.pending_inbound_requests.insert((peer, request_id), substream);
Ok(())
}
async fn on_dial_failure(&mut self, peer: PeerId) {
if let Some(context) = self.pending_dials.remove(&peer) {
tracing::debug!(target: LOG_TARGET, ?peer, protocol = %self.protocol, "failed to dial peer");
let _ = self
.peers
.get_mut(&peer)
.map(|peer_context| peer_context.active.remove(&context.request_id));
let _ = self
.report_request_failure(
peer,
context.request_id,
RequestResponseError::Rejected(RejectReason::DialFailed(None)),
)
.await;
}
}
/// Failed to open substream to remote peer.
async fn on_substream_open_failure(
&mut self,
substream: SubstreamId,
error: SubstreamError,
) -> crate::Result<()> {
let Some(RequestContext {
request_id, peer, ..
}) = self.pending_outbound.remove(&substream)
else {
tracing::error!(
target: LOG_TARGET,
protocol = %self.protocol,
?substream,
"pending outbound request does not exist",
);
debug_assert!(false);
return Err(Error::InvalidState);
};
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
?substream,
?error,
"failed to open substream",
);
let _ = self
.peers
.get_mut(&peer)
.map(|peer_context| peer_context.active.remove(&request_id));
self.event_tx
.send(InnerRequestResponseEvent::RequestFailed {
peer,
request_id,
error: match error {
SubstreamError::NegotiationError(NegotiationError::MultistreamSelectError(
MultistreamFailed,
)) => RequestResponseError::UnsupportedProtocol,
_ => RequestResponseError::Rejected(error.into()),
},
})
.await
.map_err(From::from)
}
/// Report request send failure to user.
async fn report_request_failure(
&mut self,
peer: PeerId,
request_id: RequestId,
error: RequestResponseError,
) -> crate::Result<()> {
self.event_tx
.send(InnerRequestResponseEvent::RequestFailed {
peer,
request_id,
error,
})
.await
.map_err(From::from)
}
/// Send request to remote peer.
async fn on_send_request(
&mut self,
peer: PeerId,
request_id: RequestId,
request: Vec<u8>,
dial_options: DialOptions,
fallback: Option<(ProtocolName, Vec<u8>)>,
) -> crate::Result<()> {
tracing::trace!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
?dial_options,
"send request to remote peer",
);
let Some(context) = self.peers.get_mut(&peer) else {
match dial_options {
DialOptions::Reject => {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
?dial_options,
"peer not connected and should not dial",
);
return self
.report_request_failure(
peer,
request_id,
RequestResponseError::NotConnected,
)
.await;
}
DialOptions::Dial => match self.service.dial(&peer) {
Ok(_) => {
tracing::trace!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
"started dialing peer",
);
self.pending_dials.insert(
peer,
RequestContext::new(peer, request_id, request, fallback),
);
return Ok(());
}
Err(error) => {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?error,
"failed to dial peer"
);
return self
.report_request_failure(
peer,
request_id,
RequestResponseError::Rejected(RejectReason::DialFailed(Some(
error,
))),
)
.await;
}
},
}
};
// open substream and push it pending outbound substreams
// once the substream is opened, send the request.
match self.service.open_substream(peer) {
Ok(substream_id) => {
let unique_request_id = context.active.insert(request_id);
debug_assert!(unique_request_id);
self.pending_outbound.insert(
substream_id,
RequestContext::new(peer, request_id, request, fallback),
);
Ok(())
}
Err(error) => {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
?error,
"failed to open substream",
);
self.report_request_failure(
peer,
request_id,
RequestResponseError::Rejected(error.into()),
)
.await
}
}
}
/// Handle substream event.
async fn on_substream_event(
&mut self,
peer: PeerId,
request_id: RequestId,
fallback: Option<ProtocolName>,
message: Result<Vec<u8>, RequestResponseError>,
) -> crate::Result<()> {
if !self
.peers
.get_mut(&peer)
.ok_or(Error::PeerDoesntExist(peer))?
.active
.remove(&request_id)
{
tracing::warn!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
"invalid state: received substream event but no active substream",
);
return Err(Error::InvalidState);
}
let event = match message {
Ok(response) => InnerRequestResponseEvent::ResponseReceived {
peer,
request_id,
response,
fallback,
},
Err(error) => match error {
RequestResponseError::Canceled => {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
"request canceled by local node",
);
return Ok(());
}
error => InnerRequestResponseEvent::RequestFailed {
peer,
request_id,
error,
},
},
};
self.event_tx.send(event).await.map_err(From::from)
}
/// Cancel outbound request.
async fn on_cancel_request(&mut self, request_id: RequestId) -> crate::Result<()> {
tracing::trace!(target: LOG_TARGET, protocol = %self.protocol, ?request_id, "cancel outbound request");
match self.pending_outbound_cancels.remove(&request_id) {
Some(tx) => tx.send(()).map_err(|_| Error::SubstreamDoesntExist),
None => {
tracing::debug!(
target: LOG_TARGET,
protocol = %self.protocol,
?request_id,
"tried to cancel request which doesn't exist",
);
Ok(())
}
}
}
/// Start [`RequestResponseProtocol`] event loop.
pub async fn run(mut self) {
tracing::debug!(target: LOG_TARGET, "starting request-response event loop");
loop {
tokio::select! {
// events coming from the network have higher priority than user commands as all user commands are
// responses to network behaviour so ensure that the commands operate on the most up to date information.
biased;
event = self.service.next() => match event {
Some(TransportEvent::ConnectionEstablished { peer, .. }) => {
let _ = self.on_connection_established(peer).await;
}
Some(TransportEvent::ConnectionClosed { peer }) => {
self.on_connection_closed(peer).await;
}
Some(TransportEvent::SubstreamOpened {
peer,
substream,
direction,
fallback,
..
}) => match direction {
Direction::Inbound => {
if let Err(error) = self.on_inbound_substream(peer, fallback, substream).await {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?error,
"failed to handle inbound substream",
);
}
}
Direction::Outbound(substream_id) => {
let _ = self.on_outbound_substream(peer, substream_id, substream, fallback).await;
}
},
Some(TransportEvent::SubstreamOpenFailure { substream, error }) => {
if let Err(error) = self.on_substream_open_failure(substream, error).await {
tracing::warn!(
target: LOG_TARGET,
protocol = %self.protocol,
?error,
"failed to handle substream open failure",
);
}
}
Some(TransportEvent::DialFailure { peer, .. }) => self.on_dial_failure(peer).await,
None => return,
},
event = self.pending_inbound.select_next_some(), if !self.pending_inbound.is_empty() => {
let (peer, request_id, fallback, event) = event;
if let Err(error) = self.on_substream_event(peer, request_id, fallback, event).await {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
?error,
"failed to handle substream event",
);
}
self.pending_outbound_cancels.remove(&request_id);
}
_ = self.pending_outbound_responses.next(), if !self.pending_outbound_responses.is_empty() => {}
event = self.pending_inbound_requests.next() => match event {
Some(((peer, request_id), message)) => {
if let Err(error) = self.on_inbound_request(peer, request_id, message).await {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
?error,
"failed to handle inbound request",
);
}
}
None => return,
},
command = self.command_rx.recv() => match command {
None => {
tracing::debug!(target: LOG_TARGET, protocol = %self.protocol, "user protocol has exited, exiting");
return
}
Some(command) => match command {
RequestResponseCommand::SendRequest { peer, request_id, request, dial_options } => {
if let Err(error) = self.on_send_request(peer, request_id, request, dial_options, None).await {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
?error,
"failed to send request",
);
}
}
RequestResponseCommand::CancelRequest { request_id } => {
if let Err(error) = self.on_cancel_request(request_id).await {
tracing::debug!(
target: LOG_TARGET,
protocol = %self.protocol,
?request_id,
?error,
"failed to cancel reqeuest",
);
}
}
RequestResponseCommand::SendRequestWithFallback { peer, request_id, request, fallback, dial_options } => {
if let Err(error) = self.on_send_request(peer, request_id, request, dial_options, Some(fallback)).await {
tracing::debug!(
target: LOG_TARGET,
?peer,
protocol = %self.protocol,
?request_id,
?error,
"failed to send request",
);
}
}
}
},
}
}
}
}