Previously, a connection would be shut down immediately as soon as its `ConnectionHandler` reports `KeepAlive::No`. As we have gained experience with libp2p, it turned out that this isn't ideal. For one, tests often need to keep connections alive longer than the configured protocols require. Plus, some usecases require connections to be kept alive in general. Both of these needs are currently served by the `keep_alive::Behaviour`. That one does essentially nothing other than statically returning `KeepAlive::Yes` from its `ConnectionHandler`. It makes much more sense to deprecate `keep_alive::Behaviour` and instead allow users to globally configure an `idle_conncetion_timeout` on the `Swarm`. This timeout comes into effect once a `ConnectionHandler` reports `KeepAlive::No`. To start with, this timeout is 0. Together with https://github.com/libp2p/rust-libp2p/issues/3844, this will allow us to move towards a much more aggressive closing of idle connections, together with a more ergonomic way of opting out of this behaviour. Fixes #4121. Pull-Request: #4161.
Description
The example showcases how to run a p2p network with libp2p and collect metrics using libp2p-metrics
.
It sets up multiple nodes in the network and measures various metrics, such as libp2p_ping
, to evaluate the network's performance.
Usage
To run the example, follow these steps:
-
Run the following command to start the first node:
cargo run
-
Open a second terminal and run the following command to start a second node:
cargo run -- <listen-addr-of-first-node>
Replace
<listen-addr-of-first-node>
with the listen address of the first node reported in the first terminal. Look for the line that saysNewListenAddr
to find the address. -
Open a third terminal and run the following command to retrieve the metrics from either the first or second node:
curl localhost:<metrics-port-of-first-or-second-node>/metrics
Replace
<metrics-port-of-first-or-second-node>
with the listen port of the metrics server of either the first or second node. Look for the line that saystide::server Server listening on
to find the port.After executing the command, you should see a long list of metrics printed to the terminal. Make sure to check the
libp2p_ping
metrics, which should have a value greater than zero (>0
).
Conclusion
This example demonstrates how to utilize the libp2p-metrics
crate to collect and analyze metrics in a libp2p network.
By running multiple nodes and examining the metrics, users can gain insights into the network's performance and behavior.