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 ping example showcases how to create a network of nodes that establish connections, negotiate the ping protocol, and ping each other.
Usage
To run the example, follow these steps:
-
In a first terminal window, run the following command:
cargo run
This command starts a node and prints the
PeerId
and the listening addresses, such asListening on "/ip4/0.0.0.0/tcp/24915"
. -
In a second terminal window, start a new instance of the example with the following command:
cargo run -- /ip4/127.0.0.1/tcp/24915
Replace
/ip4/127.0.0.1/tcp/24915
with the listen address of the first node obtained from the first terminal window. -
The two nodes will establish a connection, negotiate the ping protocol, and begin pinging each other.
Conclusion
The ping example demonstrates the basic usage of libp2p to create a simple p2p network and implement a ping protocol. By running multiple nodes and observing the ping behavior, users can gain insights into how libp2p facilitates communication and interaction between peers.