Skip to content

Commit dcb4f96

Browse files
Always box cause for denied connection
1 parent a269e9d commit dcb4f96

File tree

24 files changed

+105
-59
lines changed

24 files changed

+105
-59
lines changed

misc/metrics/src/swarm.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct Metrics {
3131
connections_established: Family<ConnectionEstablishedLabels, Counter>,
3232
connections_closed: Family<ConnectionClosedLabels, Counter>,
3333

34+
connections_denied: Family<AddressLabels, Counter>,
35+
3436
new_listen_addr: Family<AddressLabels, Counter>,
3537
expired_listen_addr: Family<AddressLabels, Counter>,
3638

@@ -60,6 +62,13 @@ impl Metrics {
6062
Box::new(connections_incoming_error.clone()),
6163
);
6264

65+
let connections_denied = Family::default();
66+
sub_registry.register(
67+
"connections_denied",
68+
"Number of denied connections",
69+
Box::new(connections_denied.clone()),
70+
);
71+
6372
let new_listen_addr = Family::default();
6473
sub_registry.register(
6574
"new_listen_addr",
@@ -128,6 +137,7 @@ impl Metrics {
128137
connections_incoming_error,
129138
connections_established,
130139
connections_closed,
140+
connections_denied,
131141
new_listen_addr,
132142
expired_listen_addr,
133143
listener_closed,
@@ -269,6 +279,13 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
269279
libp2p_swarm::SwarmEvent::Dialing(_) => {
270280
self.dial_attempt.inc();
271281
}
282+
libp2p_swarm::SwarmEvent::ConnectionDenied { endpoint, .. } => {
283+
self.connections_denied
284+
.get_or_create(&AddressLabels {
285+
protocols: protocol_stack::as_string(endpoint.get_remote_address()),
286+
})
287+
.inc();
288+
}
272289
}
273290
}
274291
}

protocols/autonat/src/behaviour.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use libp2p_request_response::{
3535
ProtocolSupport, RequestId, RequestResponse, RequestResponseConfig, RequestResponseEvent,
3636
RequestResponseMessage, ResponseChannel,
3737
};
38-
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
38+
use libp2p_swarm::behaviour::THandlerInEvent;
3939
use libp2p_swarm::{
4040
behaviour::{
4141
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
@@ -461,7 +461,7 @@ impl NetworkBehaviour for Behaviour {
461461
&mut self,
462462
peer: &PeerId,
463463
connected_point: &ConnectedPoint,
464-
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
464+
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
465465
self.inner.new_handler(peer, connected_point)
466466
}
467467

protocols/dcutr/src/behaviour.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ use libp2p_core::connection::{ConnectedPoint, ConnectionId};
2727
use libp2p_core::multiaddr::Protocol;
2828
use libp2p_core::{Multiaddr, PeerId};
2929
use libp2p_swarm::behaviour::{
30-
ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure, FromSwarm,
31-
THandlerInEvent,
30+
ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, THandlerInEvent,
3231
};
3332
use libp2p_swarm::dial_opts::{self, DialOpts};
3433
use libp2p_swarm::{
@@ -216,7 +215,7 @@ impl NetworkBehaviour for Behaviour {
216215
&mut self,
217216
peer: &PeerId,
218217
connected_point: &ConnectedPoint,
219-
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
218+
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
220219
match (
221220
self.awaiting_direct_inbound_connections.entry(*peer),
222221
self.awaiting_direct_outbound_connections.entry(*peer),

protocols/floodsub/src/layer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use crate::FloodsubConfig;
2727
use cuckoofilter::{CuckooError, CuckooFilter};
2828
use fnv::FnvHashSet;
2929
use libp2p_core::{connection::ConnectionId, ConnectedPoint, PeerId};
30+
use libp2p_swarm::behaviour::THandlerInEvent;
3031
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
31-
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
3232
use libp2p_swarm::ConnectionHandler;
3333
use libp2p_swarm::{
3434
dial_opts::DialOpts, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler,
@@ -340,7 +340,7 @@ impl NetworkBehaviour for Floodsub {
340340
&mut self,
341341
_: &PeerId,
342342
_: &ConnectedPoint,
343-
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
343+
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
344344
Ok(Default::default())
345345
}
346346

protocols/gossipsub/src/behaviour.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ use crate::types::{
6666
};
6767
use crate::types::{GossipsubRpc, PeerConnections, PeerKind};
6868
use crate::{rpc_proto, TopicScoreParams};
69-
use libp2p_swarm::behaviour::ConnectionDenied;
7069
use std::{cmp::Ordering::Equal, fmt::Debug};
7170
use wasm_timer::Interval;
7271

@@ -3302,7 +3301,7 @@ where
33023301
&mut self,
33033302
_: &PeerId,
33043303
_: &ConnectedPoint,
3305-
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
3304+
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
33063305
let protocol_config = ProtocolConfig::new(
33073306
self.config.protocol_id().clone(),
33083307
self.config.custom_id_version().clone(),

protocols/identify/src/behaviour.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ use libp2p_core::{
2525
connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Multiaddr, PeerId, PublicKey,
2626
};
2727
use libp2p_swarm::behaviour::{
28-
ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure, FromSwarm,
29-
THandlerInEvent,
28+
ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, THandlerInEvent,
3029
};
3130
use libp2p_swarm::{
3231
dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError,
@@ -243,7 +242,7 @@ impl NetworkBehaviour for Behaviour {
243242
&mut self,
244243
peer: &PeerId,
245244
_: &ConnectedPoint,
246-
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
245+
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
247246
Ok(Handler::new(
248247
self.config.initial_delay,
249248
self.config.interval,

protocols/kad/src/behaviour.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use fnv::{FnvHashMap, FnvHashSet};
4141
use instant::Instant;
4242
use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId};
4343
use libp2p_swarm::behaviour::{
44-
AddressChange, ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure,
45-
ExpiredListenAddr, FromSwarm, NewExternalAddr, NewListenAddr, THandlerInEvent,
44+
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredListenAddr,
45+
FromSwarm, NewExternalAddr, NewListenAddr, THandlerInEvent,
4646
};
4747
use libp2p_swarm::{
4848
dial_opts::{self, DialOpts},
@@ -1979,7 +1979,7 @@ where
19791979
&mut self,
19801980
remote_peer_id: &PeerId,
19811981
endpoint: &ConnectedPoint,
1982-
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
1982+
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
19831983
Ok(KademliaHandler::new(
19841984
KademliaHandlerConfig {
19851985
protocol_config: self.protocol_config.clone(),

protocols/mdns/src/behaviour.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::Config;
2828
use futures::Stream;
2929
use if_watch::IfEvent;
3030
use libp2p_core::{Multiaddr, PeerId};
31-
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionDenied, FromSwarm};
31+
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
3232
use libp2p_swarm::{
3333
dummy, ConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
3434
};
@@ -172,7 +172,7 @@ where
172172
&mut self,
173173
_: &PeerId,
174174
_: &libp2p_core::ConnectedPoint,
175-
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
175+
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
176176
Ok(dummy::ConnectionHandler)
177177
}
178178

protocols/ping/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mod protocol;
4848
use handler::Handler;
4949
pub use handler::{Config, Failure, Success};
5050
use libp2p_core::{connection::ConnectionId, ConnectedPoint, PeerId};
51-
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
51+
use libp2p_swarm::behaviour::THandlerInEvent;
5252
use libp2p_swarm::{
5353
behaviour::FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
5454
};
@@ -124,7 +124,8 @@ impl NetworkBehaviour for Behaviour {
124124
&mut self,
125125
_: &PeerId,
126126
_: &ConnectedPoint,
127-
) -> std::result::Result<Self::ConnectionHandler, ConnectionDenied> {
127+
) -> std::result::Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>>
128+
{
128129
Ok(Handler::new(self.config.clone()))
129130
}
130131

protocols/relay/src/v2/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use futures::ready;
3535
use futures::stream::StreamExt;
3636
use libp2p_core::connection::ConnectionId;
3737
use libp2p_core::{ConnectedPoint, PeerId};
38+
use libp2p_swarm::behaviour::THandlerInEvent;
3839
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
39-
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
4040
use libp2p_swarm::dial_opts::DialOpts;
4141
use libp2p_swarm::{dummy, ConnectionHandler};
4242
use libp2p_swarm::{
@@ -159,7 +159,7 @@ impl NetworkBehaviour for Client {
159159
&mut self,
160160
peer: &PeerId,
161161
connected_point: &ConnectedPoint,
162-
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
162+
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
163163
if connected_point.is_relayed() {
164164
if let Some(event) = self.initial_events.remove(peer) {
165165
log::debug!(

0 commit comments

Comments
 (0)