-
Notifications
You must be signed in to change notification settings - Fork 976
/
connection.rs
1404 lines (1222 loc) · 48.4 KB
/
connection.rs
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
// Copyright 2020 Parity Technologies (UK) Ltd.
//
// 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.
mod error;
pub(crate) mod pool;
mod supported_protocols;
pub use error::ConnectionError;
pub(crate) use error::{
PendingConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError,
};
pub use supported_protocols::SupportedProtocols;
use crate::handler::{
AddressChange, ConnectionEvent, ConnectionHandler, DialUpgradeError, FullyNegotiatedInbound,
FullyNegotiatedOutbound, ListenUpgradeError, ProtocolSupport, ProtocolsAdded, ProtocolsChange,
UpgradeInfoSend,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
use crate::{
ConnectionHandlerEvent, KeepAlive, Stream, StreamProtocol, StreamUpgradeError,
SubstreamProtocol,
};
use futures::future::BoxFuture;
use futures::stream::FuturesUnordered;
use futures::FutureExt;
use futures::StreamExt;
use futures_timer::Delay;
use instant::Instant;
use libp2p_core::connection::ConnectedPoint;
use libp2p_core::multiaddr::Multiaddr;
use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerEvent, StreamMuxerExt, SubstreamBox};
use libp2p_core::upgrade;
use libp2p_core::upgrade::{NegotiationError, ProtocolError};
use libp2p_core::Endpoint;
use libp2p_identity::PeerId;
use std::cmp::max;
use std::collections::HashSet;
use std::fmt::{Display, Formatter};
use std::future::Future;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::Waker;
use std::time::Duration;
use std::{fmt, io, mem, pin::Pin, task::Context, task::Poll};
static NEXT_CONNECTION_ID: AtomicUsize = AtomicUsize::new(1);
/// Connection identifier.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConnectionId(usize);
impl ConnectionId {
/// Creates an _unchecked_ [`ConnectionId`].
///
/// [`Swarm`](crate::Swarm) enforces that [`ConnectionId`]s are unique and not reused.
/// This constructor does not, hence the _unchecked_.
///
/// It is primarily meant for allowing manual tests of [`NetworkBehaviour`](crate::NetworkBehaviour)s.
pub fn new_unchecked(id: usize) -> Self {
Self(id)
}
/// Returns the next available [`ConnectionId`].
pub(crate) fn next() -> Self {
Self(NEXT_CONNECTION_ID.fetch_add(1, Ordering::SeqCst))
}
}
impl Display for ConnectionId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
/// Information about a successfully established connection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Connected {
/// The connected endpoint, including network address information.
pub(crate) endpoint: ConnectedPoint,
/// Information obtained from the transport.
pub(crate) peer_id: PeerId,
}
/// Event generated by a [`Connection`].
#[derive(Debug, Clone)]
pub(crate) enum Event<T> {
/// Event generated by the [`ConnectionHandler`].
Handler(T),
/// Address of the remote has changed.
AddressChange(Multiaddr),
}
/// A multiplexed connection to a peer with an associated [`ConnectionHandler`].
pub(crate) struct Connection<THandler>
where
THandler: ConnectionHandler,
{
/// Node that handles the muxing.
muxing: StreamMuxerBox,
/// The underlying handler.
handler: THandler,
/// Futures that upgrade incoming substreams.
negotiating_in: FuturesUnordered<
StreamUpgrade<
THandler::InboundOpenInfo,
<THandler::InboundProtocol as InboundUpgradeSend>::Output,
<THandler::InboundProtocol as InboundUpgradeSend>::Error,
>,
>,
/// Futures that upgrade outgoing substreams.
negotiating_out: FuturesUnordered<
StreamUpgrade<
THandler::OutboundOpenInfo,
<THandler::OutboundProtocol as OutboundUpgradeSend>::Output,
<THandler::OutboundProtocol as OutboundUpgradeSend>::Error,
>,
>,
/// The currently planned connection & handler shutdown.
shutdown: Shutdown,
/// The substream upgrade protocol override, if any.
substream_upgrade_protocol_override: Option<upgrade::Version>,
/// The maximum number of inbound streams concurrently negotiating on a
/// connection. New inbound streams exceeding the limit are dropped and thus
/// reset.
///
/// Note: This only enforces a limit on the number of concurrently
/// negotiating inbound streams. The total number of inbound streams on a
/// connection is the sum of negotiating and negotiated streams. A limit on
/// the total number of streams can be enforced at the
/// [`StreamMuxerBox`](libp2p_core::muxing::StreamMuxerBox) level.
max_negotiating_inbound_streams: usize,
/// Contains all upgrades that are waiting for a new outbound substream.
///
/// The upgrade timeout is already ticking here so this may fail in case the remote is not quick
/// enough in providing us with a new stream.
requested_substreams: FuturesUnordered<
SubstreamRequested<THandler::OutboundOpenInfo, THandler::OutboundProtocol>,
>,
local_supported_protocols: HashSet<StreamProtocol>,
remote_supported_protocols: HashSet<StreamProtocol>,
idle_timeout: Duration,
}
impl<THandler> fmt::Debug for Connection<THandler>
where
THandler: ConnectionHandler + fmt::Debug,
THandler::OutboundOpenInfo: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Connection")
.field("handler", &self.handler)
.finish()
}
}
impl<THandler> Unpin for Connection<THandler> where THandler: ConnectionHandler {}
impl<THandler> Connection<THandler>
where
THandler: ConnectionHandler,
{
/// Builds a new `Connection` from the given substream multiplexer
/// and connection handler.
pub(crate) fn new(
muxer: StreamMuxerBox,
mut handler: THandler,
substream_upgrade_protocol_override: Option<upgrade::Version>,
max_negotiating_inbound_streams: usize,
idle_timeout: Duration,
) -> Self {
let initial_protocols = gather_supported_protocols(&handler);
if !initial_protocols.is_empty() {
handler.on_connection_event(ConnectionEvent::LocalProtocolsChange(
ProtocolsChange::Added(ProtocolsAdded::from_set(&initial_protocols)),
));
}
Connection {
muxing: muxer,
handler,
negotiating_in: Default::default(),
negotiating_out: Default::default(),
shutdown: Shutdown::None,
substream_upgrade_protocol_override,
max_negotiating_inbound_streams,
requested_substreams: Default::default(),
local_supported_protocols: initial_protocols,
remote_supported_protocols: Default::default(),
idle_timeout,
}
}
/// Notifies the connection handler of an event.
pub(crate) fn on_behaviour_event(&mut self, event: THandler::FromBehaviour) {
self.handler.on_behaviour_event(event);
}
/// Begins an orderly shutdown of the connection, returning the connection
/// handler and a `Future` that resolves when connection shutdown is complete.
pub(crate) fn close(self) -> (THandler, impl Future<Output = io::Result<()>>) {
(self.handler, self.muxing.close())
}
/// Polls the handler and the substream, forwarding events from the former to the latter and
/// vice versa.
pub(crate) fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Event<THandler::ToBehaviour>, ConnectionError<THandler::Error>>> {
let Self {
requested_substreams,
muxing,
handler,
negotiating_out,
negotiating_in,
shutdown,
max_negotiating_inbound_streams,
substream_upgrade_protocol_override,
local_supported_protocols: supported_protocols,
remote_supported_protocols,
idle_timeout,
} = self.get_mut();
loop {
match requested_substreams.poll_next_unpin(cx) {
Poll::Ready(Some(Ok(()))) => continue,
Poll::Ready(Some(Err(info))) => {
handler.on_connection_event(ConnectionEvent::DialUpgradeError(
DialUpgradeError {
info,
error: StreamUpgradeError::Timeout,
},
));
continue;
}
Poll::Ready(None) | Poll::Pending => {}
}
// Poll the [`ConnectionHandler`].
match handler.poll(cx) {
Poll::Pending => {}
Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }) => {
let timeout = *protocol.timeout();
let (upgrade, user_data) = protocol.into_upgrade();
requested_substreams.push(SubstreamRequested::new(user_data, timeout, upgrade));
continue; // Poll handler until exhausted.
}
Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => {
return Poll::Ready(Ok(Event::Handler(event)));
}
Poll::Ready(ConnectionHandlerEvent::Close(err)) => {
return Poll::Ready(Err(ConnectionError::Handler(err)));
}
Poll::Ready(ConnectionHandlerEvent::ReportRemoteProtocols(
ProtocolSupport::Added(protocols),
)) => {
if let Some(added) =
ProtocolsChange::add(remote_supported_protocols, &protocols)
{
handler.on_connection_event(ConnectionEvent::RemoteProtocolsChange(added));
remote_supported_protocols.extend(protocols);
}
continue;
}
Poll::Ready(ConnectionHandlerEvent::ReportRemoteProtocols(
ProtocolSupport::Removed(protocols),
)) => {
if let Some(removed) =
ProtocolsChange::remove(remote_supported_protocols, &protocols)
{
handler
.on_connection_event(ConnectionEvent::RemoteProtocolsChange(removed));
remote_supported_protocols.retain(|p| !protocols.contains(p));
}
continue;
}
}
// In case the [`ConnectionHandler`] can not make any more progress, poll the negotiating outbound streams.
match negotiating_out.poll_next_unpin(cx) {
Poll::Pending | Poll::Ready(None) => {}
Poll::Ready(Some((info, Ok(protocol)))) => {
handler.on_connection_event(ConnectionEvent::FullyNegotiatedOutbound(
FullyNegotiatedOutbound { protocol, info },
));
continue;
}
Poll::Ready(Some((info, Err(error)))) => {
handler.on_connection_event(ConnectionEvent::DialUpgradeError(
DialUpgradeError { info, error },
));
continue;
}
}
// In case both the [`ConnectionHandler`] and the negotiating outbound streams can not
// make any more progress, poll the negotiating inbound streams.
match negotiating_in.poll_next_unpin(cx) {
Poll::Pending | Poll::Ready(None) => {}
Poll::Ready(Some((info, Ok(protocol)))) => {
handler.on_connection_event(ConnectionEvent::FullyNegotiatedInbound(
FullyNegotiatedInbound { protocol, info },
));
continue;
}
Poll::Ready(Some((info, Err(StreamUpgradeError::Apply(error))))) => {
handler.on_connection_event(ConnectionEvent::ListenUpgradeError(
ListenUpgradeError { info, error },
));
continue;
}
Poll::Ready(Some((_, Err(StreamUpgradeError::Io(e))))) => {
log::debug!("failed to upgrade inbound stream: {e}");
continue;
}
Poll::Ready(Some((_, Err(StreamUpgradeError::NegotiationFailed)))) => {
log::debug!("no protocol could be agreed upon for inbound stream");
continue;
}
Poll::Ready(Some((_, Err(StreamUpgradeError::Timeout)))) => {
log::debug!("inbound stream upgrade timed out");
continue;
}
}
// Ask the handler whether it wants the connection (and the handler itself)
// to be kept alive, which determines the planned shutdown, if any.
let keep_alive = handler.connection_keep_alive();
match (&mut *shutdown, keep_alive) {
(Shutdown::Later(timer, deadline), KeepAlive::Until(t)) => {
if *deadline != t {
*deadline = t;
if let Some(new_duration) = deadline.checked_duration_since(Instant::now())
{
let effective_keep_alive = max(new_duration, *idle_timeout);
timer.reset(effective_keep_alive)
}
}
}
(_, KeepAlive::Until(earliest_shutdown)) => {
let now = Instant::now();
if let Some(requested) = earliest_shutdown.checked_duration_since(now) {
let effective_keep_alive = max(requested, *idle_timeout);
let safe_keep_alive = checked_add_fraction(now, effective_keep_alive);
// Important: We store the _original_ `Instant` given by the `ConnectionHandler` in the `Later` instance to ensure we can compare it in the above branch.
// This is quite subtle but will hopefully become simpler soon once `KeepAlive::Until` is fully deprecated. See <https://github.com/libp2p/rust-libp2p/issues/3844>/
*shutdown = Shutdown::Later(Delay::new(safe_keep_alive), earliest_shutdown)
}
}
(_, KeepAlive::No) if idle_timeout == &Duration::ZERO => {
*shutdown = Shutdown::Asap;
}
(Shutdown::Later(_, _), KeepAlive::No) => {
// Do nothing, i.e. let the shutdown timer continue to tick.
}
(_, KeepAlive::No) => {
let now = Instant::now();
let safe_keep_alive = checked_add_fraction(now, *idle_timeout);
*shutdown = Shutdown::Later(Delay::new(safe_keep_alive), now + safe_keep_alive);
}
(_, KeepAlive::Yes) => *shutdown = Shutdown::None,
};
// Check if the connection (and handler) should be shut down.
// As long as we're still negotiating substreams, shutdown is always postponed.
if negotiating_in.is_empty()
&& negotiating_out.is_empty()
&& requested_substreams.is_empty()
{
match shutdown {
Shutdown::None => {}
Shutdown::Asap => return Poll::Ready(Err(ConnectionError::KeepAliveTimeout)),
Shutdown::Later(delay, _) => match Future::poll(Pin::new(delay), cx) {
Poll::Ready(_) => {
return Poll::Ready(Err(ConnectionError::KeepAliveTimeout))
}
Poll::Pending => {}
},
}
}
match muxing.poll_unpin(cx)? {
Poll::Pending => {}
Poll::Ready(StreamMuxerEvent::AddressChange(address)) => {
handler.on_connection_event(ConnectionEvent::AddressChange(AddressChange {
new_address: &address,
}));
return Poll::Ready(Ok(Event::AddressChange(address)));
}
}
if let Some(requested_substream) = requested_substreams.iter_mut().next() {
match muxing.poll_outbound_unpin(cx)? {
Poll::Pending => {}
Poll::Ready(substream) => {
let (user_data, timeout, upgrade) = requested_substream.extract();
negotiating_out.push(StreamUpgrade::new_outbound(
substream,
user_data,
timeout,
upgrade,
*substream_upgrade_protocol_override,
));
continue; // Go back to the top, handler can potentially make progress again.
}
}
}
if negotiating_in.len() < *max_negotiating_inbound_streams {
match muxing.poll_inbound_unpin(cx)? {
Poll::Pending => {}
Poll::Ready(substream) => {
let protocol = handler.listen_protocol();
negotiating_in.push(StreamUpgrade::new_inbound(substream, protocol));
continue; // Go back to the top, handler can potentially make progress again.
}
}
}
let new_protocols = gather_supported_protocols(handler);
let changes = ProtocolsChange::from_full_sets(supported_protocols, &new_protocols);
if !changes.is_empty() {
for change in changes {
handler.on_connection_event(ConnectionEvent::LocalProtocolsChange(change));
}
*supported_protocols = new_protocols;
continue; // Go back to the top, handler can potentially make progress again.
}
return Poll::Pending; // Nothing can make progress, return `Pending`.
}
}
#[cfg(test)]
fn poll_noop_waker(
&mut self,
) -> Poll<Result<Event<THandler::ToBehaviour>, ConnectionError<THandler::Error>>> {
Pin::new(self).poll(&mut Context::from_waker(futures::task::noop_waker_ref()))
}
}
fn gather_supported_protocols(handler: &impl ConnectionHandler) -> HashSet<StreamProtocol> {
handler
.listen_protocol()
.upgrade()
.protocol_info()
.filter_map(|i| StreamProtocol::try_from_owned(i.as_ref().to_owned()).ok())
.collect()
}
/// Repeatedly halves and adds the [`Duration`] to the [`Instant`] until [`Instant::checked_add`] succeeds.
///
/// [`Instant`] depends on the underlying platform and has a limit of which points in time it can represent.
/// The [`Duration`] computed by the this function may not be the longest possible that we can add to `now` but it will work.
fn checked_add_fraction(start: Instant, mut duration: Duration) -> Duration {
while start.checked_add(duration).is_none() {
log::debug!("{start:?} + {duration:?} cannot be presented, halving duration");
duration /= 2;
}
duration
}
/// Borrowed information about an incoming connection currently being negotiated.
#[derive(Debug, Copy, Clone)]
pub(crate) struct IncomingInfo<'a> {
/// Local connection address.
pub(crate) local_addr: &'a Multiaddr,
/// Address used to send back data to the remote.
pub(crate) send_back_addr: &'a Multiaddr,
}
impl<'a> IncomingInfo<'a> {
/// Builds the [`ConnectedPoint`] corresponding to the incoming connection.
pub(crate) fn create_connected_point(&self) -> ConnectedPoint {
ConnectedPoint::Listener {
local_addr: self.local_addr.clone(),
send_back_addr: self.send_back_addr.clone(),
}
}
}
struct StreamUpgrade<UserData, TOk, TErr> {
user_data: Option<UserData>,
timeout: Delay,
upgrade: BoxFuture<'static, Result<TOk, StreamUpgradeError<TErr>>>,
}
impl<UserData, TOk, TErr> StreamUpgrade<UserData, TOk, TErr> {
fn new_outbound<Upgrade>(
substream: SubstreamBox,
user_data: UserData,
timeout: Delay,
upgrade: Upgrade,
version_override: Option<upgrade::Version>,
) -> Self
where
Upgrade: OutboundUpgradeSend<Output = TOk, Error = TErr>,
{
let effective_version = match version_override {
Some(version_override) if version_override != upgrade::Version::default() => {
log::debug!(
"Substream upgrade protocol override: {:?} -> {:?}",
upgrade::Version::default(),
version_override
);
version_override
}
_ => upgrade::Version::default(),
};
let protocols = upgrade.protocol_info();
Self {
user_data: Some(user_data),
timeout,
upgrade: Box::pin(async move {
let (info, stream) = multistream_select::dialer_select_proto(
substream,
protocols,
effective_version,
)
.await
.map_err(to_stream_upgrade_error)?;
let output = upgrade
.upgrade_outbound(Stream::new(stream), info)
.await
.map_err(StreamUpgradeError::Apply)?;
Ok(output)
}),
}
}
}
impl<UserData, TOk, TErr> StreamUpgrade<UserData, TOk, TErr> {
fn new_inbound<Upgrade>(
substream: SubstreamBox,
protocol: SubstreamProtocol<Upgrade, UserData>,
) -> Self
where
Upgrade: InboundUpgradeSend<Output = TOk, Error = TErr>,
{
let timeout = *protocol.timeout();
let (upgrade, open_info) = protocol.into_upgrade();
let protocols = upgrade.protocol_info();
Self {
user_data: Some(open_info),
timeout: Delay::new(timeout),
upgrade: Box::pin(async move {
let (info, stream) =
multistream_select::listener_select_proto(substream, protocols)
.await
.map_err(to_stream_upgrade_error)?;
let output = upgrade
.upgrade_inbound(Stream::new(stream), info)
.await
.map_err(StreamUpgradeError::Apply)?;
Ok(output)
}),
}
}
}
fn to_stream_upgrade_error<T>(e: NegotiationError) -> StreamUpgradeError<T> {
match e {
NegotiationError::Failed => StreamUpgradeError::NegotiationFailed,
NegotiationError::ProtocolError(ProtocolError::IoError(e)) => StreamUpgradeError::Io(e),
NegotiationError::ProtocolError(other) => {
StreamUpgradeError::Io(io::Error::new(io::ErrorKind::Other, other))
}
}
}
impl<UserData, TOk, TErr> Unpin for StreamUpgrade<UserData, TOk, TErr> {}
impl<UserData, TOk, TErr> Future for StreamUpgrade<UserData, TOk, TErr> {
type Output = (UserData, Result<TOk, StreamUpgradeError<TErr>>);
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
match self.timeout.poll_unpin(cx) {
Poll::Ready(()) => {
return Poll::Ready((
self.user_data
.take()
.expect("Future not to be polled again once ready."),
Err(StreamUpgradeError::Timeout),
))
}
Poll::Pending => {}
}
let result = futures::ready!(self.upgrade.poll_unpin(cx));
let user_data = self
.user_data
.take()
.expect("Future not to be polled again once ready.");
Poll::Ready((user_data, result))
}
}
enum SubstreamRequested<UserData, Upgrade> {
Waiting {
user_data: UserData,
timeout: Delay,
upgrade: Upgrade,
/// A waker to notify our [`FuturesUnordered`] that we have extracted the data.
///
/// This will ensure that we will get polled again in the next iteration which allows us to
/// resolve with `Ok(())` and be removed from the [`FuturesUnordered`].
extracted_waker: Option<Waker>,
},
Done,
}
impl<UserData, Upgrade> SubstreamRequested<UserData, Upgrade> {
fn new(user_data: UserData, timeout: Duration, upgrade: Upgrade) -> Self {
Self::Waiting {
user_data,
timeout: Delay::new(timeout),
upgrade,
extracted_waker: None,
}
}
fn extract(&mut self) -> (UserData, Delay, Upgrade) {
match mem::replace(self, Self::Done) {
SubstreamRequested::Waiting {
user_data,
timeout,
upgrade,
extracted_waker: waker,
} => {
if let Some(waker) = waker {
waker.wake();
}
(user_data, timeout, upgrade)
}
SubstreamRequested::Done => panic!("cannot extract twice"),
}
}
}
impl<UserData, Upgrade> Unpin for SubstreamRequested<UserData, Upgrade> {}
impl<UserData, Upgrade> Future for SubstreamRequested<UserData, Upgrade> {
type Output = Result<(), UserData>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
match mem::replace(this, Self::Done) {
SubstreamRequested::Waiting {
user_data,
upgrade,
mut timeout,
..
} => match timeout.poll_unpin(cx) {
Poll::Ready(()) => Poll::Ready(Err(user_data)),
Poll::Pending => {
*this = Self::Waiting {
user_data,
upgrade,
timeout,
extracted_waker: Some(cx.waker().clone()),
};
Poll::Pending
}
},
SubstreamRequested::Done => Poll::Ready(Ok(())),
}
}
}
/// The options for a planned connection & handler shutdown.
///
/// A shutdown is planned anew based on the the return value of
/// [`ConnectionHandler::connection_keep_alive`] of the underlying handler
/// after every invocation of [`ConnectionHandler::poll`].
///
/// A planned shutdown is always postponed for as long as there are ingoing
/// or outgoing substreams being negotiated, i.e. it is a graceful, "idle"
/// shutdown.
#[derive(Debug)]
enum Shutdown {
/// No shutdown is planned.
None,
/// A shut down is planned as soon as possible.
Asap,
/// A shut down is planned for when a `Delay` has elapsed.
Later(Delay, Instant),
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dummy;
use futures::future;
use futures::AsyncRead;
use futures::AsyncWrite;
use libp2p_core::upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use libp2p_core::StreamMuxer;
use quickcheck::*;
use std::sync::{Arc, Weak};
use std::time::Instant;
use void::Void;
#[test]
fn max_negotiating_inbound_streams() {
fn prop(max_negotiating_inbound_streams: u8) {
let max_negotiating_inbound_streams: usize = max_negotiating_inbound_streams.into();
let alive_substream_counter = Arc::new(());
let mut connection = Connection::new(
StreamMuxerBox::new(DummyStreamMuxer {
counter: alive_substream_counter.clone(),
}),
MockConnectionHandler::new(Duration::ZERO),
None,
max_negotiating_inbound_streams,
Duration::ZERO,
);
let result = connection.poll_noop_waker();
assert!(result.is_pending());
assert_eq!(
Arc::weak_count(&alive_substream_counter),
max_negotiating_inbound_streams,
"Expect no more than the maximum number of allowed streams"
);
}
QuickCheck::new().quickcheck(prop as fn(_));
}
#[test]
fn outbound_stream_timeout_starts_on_request() {
let upgrade_timeout = Duration::from_secs(1);
let mut connection = Connection::new(
StreamMuxerBox::new(PendingStreamMuxer),
MockConnectionHandler::new(upgrade_timeout),
None,
2,
Duration::ZERO,
);
connection.handler.open_new_outbound();
let _ = connection.poll_noop_waker();
std::thread::sleep(upgrade_timeout + Duration::from_secs(1));
let _ = connection.poll_noop_waker();
assert!(matches!(
connection.handler.error.unwrap(),
StreamUpgradeError::Timeout
))
}
#[test]
fn propagates_changes_to_supported_inbound_protocols() {
let mut connection = Connection::new(
StreamMuxerBox::new(PendingStreamMuxer),
ConfigurableProtocolConnectionHandler::default(),
None,
0,
Duration::ZERO,
);
// First, start listening on a single protocol.
connection.handler.listen_on(&["/foo"]);
let _ = connection.poll_noop_waker();
assert_eq!(connection.handler.local_added, vec![vec!["/foo"]]);
assert!(connection.handler.local_removed.is_empty());
// Second, listen on two protocols.
connection.handler.listen_on(&["/foo", "/bar"]);
let _ = connection.poll_noop_waker();
assert_eq!(
connection.handler.local_added,
vec![vec!["/foo"], vec!["/bar"]],
"expect to only receive an event for the newly added protocols"
);
assert!(connection.handler.local_removed.is_empty());
// Third, stop listening on the first protocol.
connection.handler.listen_on(&["/bar"]);
let _ = connection.poll_noop_waker();
assert_eq!(
connection.handler.local_added,
vec![vec!["/foo"], vec!["/bar"]]
);
assert_eq!(connection.handler.local_removed, vec![vec!["/foo"]]);
}
#[test]
fn only_propagtes_actual_changes_to_remote_protocols_to_handler() {
let mut connection = Connection::new(
StreamMuxerBox::new(PendingStreamMuxer),
ConfigurableProtocolConnectionHandler::default(),
None,
0,
Duration::ZERO,
);
// First, remote supports a single protocol.
connection.handler.remote_adds_support_for(&["/foo"]);
let _ = connection.poll_noop_waker();
assert_eq!(connection.handler.remote_added, vec![vec!["/foo"]]);
assert!(connection.handler.remote_removed.is_empty());
// Second, it adds a protocol but also still includes the first one.
connection
.handler
.remote_adds_support_for(&["/foo", "/bar"]);
let _ = connection.poll_noop_waker();
assert_eq!(
connection.handler.remote_added,
vec![vec!["/foo"], vec!["/bar"]],
"expect to only receive an event for the newly added protocol"
);
assert!(connection.handler.remote_removed.is_empty());
// Third, stop listening on a protocol it never advertised (we can't control what handlers do so this needs to be handled gracefully).
connection.handler.remote_removes_support_for(&["/baz"]);
let _ = connection.poll_noop_waker();
assert_eq!(
connection.handler.remote_added,
vec![vec!["/foo"], vec!["/bar"]]
);
assert!(&connection.handler.remote_removed.is_empty());
// Fourth, stop listening on a protocol that was previously supported
connection.handler.remote_removes_support_for(&["/bar"]);
let _ = connection.poll_noop_waker();
assert_eq!(
connection.handler.remote_added,
vec![vec!["/foo"], vec!["/bar"]]
);
assert_eq!(connection.handler.remote_removed, vec![vec!["/bar"]]);
}
#[tokio::test]
async fn idle_timeout_with_keep_alive_no() {
let idle_timeout = Duration::from_millis(100);
let mut connection = Connection::new(
StreamMuxerBox::new(PendingStreamMuxer),
dummy::ConnectionHandler,
None,
0,
idle_timeout,
);
assert!(connection.poll_noop_waker().is_pending());
tokio::time::sleep(idle_timeout).await;
assert!(matches!(
connection.poll_noop_waker(),
Poll::Ready(Err(ConnectionError::KeepAliveTimeout))
));
}
#[tokio::test]
async fn idle_timeout_with_keep_alive_until_greater_than_idle_timeout() {
let idle_timeout = Duration::from_millis(100);
let mut connection = Connection::new(
StreamMuxerBox::new(PendingStreamMuxer),
KeepAliveUntilConnectionHandler {
until: Instant::now() + idle_timeout * 2,
},
None,
0,
idle_timeout,
);
assert!(connection.poll_noop_waker().is_pending());
tokio::time::sleep(idle_timeout).await;
assert!(
connection.poll_noop_waker().is_pending(),
"`KeepAlive::Until` is greater than idle-timeout, continue sleeping"
);
tokio::time::sleep(idle_timeout).await;
assert!(matches!(
connection.poll_noop_waker(),
Poll::Ready(Err(ConnectionError::KeepAliveTimeout))
));
}
#[tokio::test]
async fn idle_timeout_with_keep_alive_until_less_than_idle_timeout() {
let idle_timeout = Duration::from_millis(100);
let mut connection = Connection::new(
StreamMuxerBox::new(PendingStreamMuxer),
KeepAliveUntilConnectionHandler {
until: Instant::now() + idle_timeout / 2,
},
None,
0,
idle_timeout,
);
assert!(connection.poll_noop_waker().is_pending());
tokio::time::sleep(idle_timeout / 2).await;
assert!(
connection.poll_noop_waker().is_pending(),
"`KeepAlive::Until` is less than idle-timeout, honor idle-timeout"
);
tokio::time::sleep(idle_timeout / 2).await;
assert!(matches!(
connection.poll_noop_waker(),
Poll::Ready(Err(ConnectionError::KeepAliveTimeout))
));
}
#[test]
fn checked_add_fraction_can_add_u64_max() {
let _ = env_logger::try_init();
let start = Instant::now();
let duration = checked_add_fraction(start, Duration::from_secs(u64::MAX));
assert!(start.checked_add(duration).is_some())
}
struct KeepAliveUntilConnectionHandler {
until: Instant,
}
impl ConnectionHandler for KeepAliveUntilConnectionHandler {
type FromBehaviour = Void;
type ToBehaviour = Void;
type Error = Void;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = DeniedUpgrade;
type InboundOpenInfo = ();
type OutboundOpenInfo = Void;
fn listen_protocol(