Skip to content

Commit c3e6cce

Browse files
authored
dial order (#286)
Signed-off-by: turuslan <[email protected]>
1 parent b183f88 commit c3e6cce

37 files changed

+227
-310
lines changed

include/libp2p/host/basic_host/basic_host.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,15 @@ namespace libp2p::host {
4848
Connectedness connectedness(const peer::PeerInfo &p) const override;
4949

5050
void connect(const peer::PeerInfo &peer_info,
51-
const ConnectionResultHandler &handler,
52-
std::chrono::milliseconds timeout) override;
51+
const ConnectionResultHandler &handler) override;
5352

5453
void disconnect(const peer::PeerId &peer_id) override;
5554

5655
void setProtocolHandler(StreamProtocols protocols,
5756
StreamAndProtocolCb cb,
5857
ProtocolPredicate predicate) override;
5958

60-
void newStream(const peer::PeerInfo &peer_info,
61-
StreamProtocols protocols,
62-
StreamAndProtocolOrErrorCb cb,
63-
std::chrono::milliseconds timeout = {}) override;
64-
65-
void newStream(const peer::PeerId &peer_id,
59+
void newStream(const PeerInfo &peer_id,
6660
StreamProtocols protocols,
6761
StreamAndProtocolOrErrorCb cb) override;
6862

include/libp2p/host/host.hpp

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,18 @@ namespace libp2p {
125125
* @brief Initiates connection to the peer {@param peer_info}.
126126
* @param peer_info peer to connect.
127127
* @param handler callback, will be executed on success or fail
128-
* @param timeout in milliseconds
129128
*/
130129
virtual void connect(const peer::PeerInfo &peer_info,
131-
const ConnectionResultHandler &handler,
132-
std::chrono::milliseconds timeout) = 0;
133-
134-
/**
135-
* @brief Initiates connection to the peer {@param peer_info}.
136-
* @param peer_info peer to connect.
137-
* @param handler callback, will be executed on success or fail
138-
*/
139-
inline void connect(const peer::PeerInfo &peer_info,
140-
const ConnectionResultHandler &handler) {
141-
connect(peer_info, handler, std::chrono::milliseconds::zero());
142-
};
130+
const ConnectionResultHandler &handler) = 0;
143131

144132
/**
145133
* @brief Initiates connection to the peer {@param peer_info}. If connection
146134
* exists, does nothing.
147135
* @param peer_info peer to connect.
148136
*/
149137
inline void connect(const peer::PeerInfo &peer_info) {
150-
connect(
151-
peer_info, [](auto &&) {}, std::chrono::milliseconds::zero());
152-
};
138+
connect(peer_info, [](ConnectionResult &&) {});
139+
}
153140

154141
/**
155142
* Closes all connections (outbound and inbound) to given {@param peer_id}
@@ -158,16 +145,14 @@ namespace libp2p {
158145

159146
/**
160147
* @brief Open new stream to the peer {@param peer_info} with protocol
161-
* {@param protocol} with a specific timeout.
148+
* {@param protocol}
162149
* @param peer_info stream will be opened to this peer
163150
* @param protocols "speak" using first supported protocol
164151
* @param cb callback, will be executed on success or fail
165-
* @param timeout in milliseconds
166152
*/
167153
virtual void newStream(const peer::PeerInfo &peer_info,
168154
StreamProtocols protocols,
169-
StreamAndProtocolOrErrorCb cb,
170-
std::chrono::milliseconds timeout = {}) = 0;
155+
StreamAndProtocolOrErrorCb cb) = 0;
171156

172157
/**
173158
* @brief Open new stream to the peer {@param peer} with protocol
@@ -176,9 +161,11 @@ namespace libp2p {
176161
* @param protocols "speak" using first supported protocol
177162
* @param cb callback, will be executed on success or fail
178163
*/
179-
virtual void newStream(const peer::PeerId &peer_id,
180-
StreamProtocols protocols,
181-
StreamAndProtocolOrErrorCb cb) = 0;
164+
void newStream(const PeerId &peer_id,
165+
StreamProtocols protocols,
166+
StreamAndProtocolOrErrorCb cb) {
167+
newStream(PeerInfo{.id = peer_id}, std::move(protocols), std::move(cb));
168+
}
182169

183170
/**
184171
* @brief Create listener on given multiaddress.

include/libp2p/injector/host_injector.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// implementations
1212
#include <libp2p/host/basic_host.hpp>
13-
#include <libp2p/peer/address_repository/inmem_address_repository.hpp>
1413
#include <libp2p/peer/impl/peer_repository_impl.hpp>
1514
#include <libp2p/peer/key_repository/inmem_key_repository.hpp>
1615
#include <libp2p/peer/protocol_repository/inmem_protocol_repository.hpp>
@@ -31,7 +30,6 @@ namespace libp2p::injector {
3130

3231
// repositories
3332
di::bind<peer::PeerRepository>.to<peer::PeerRepositoryImpl>(),
34-
di::bind<peer::AddressRepository>.to<peer::InmemAddressRepository>(),
3533
di::bind<peer::KeyRepository>.to<peer::InmemKeyRepository>(),
3634
di::bind<peer::ProtocolRepository>.to<peer::InmemProtocolRepository>(),
3735

include/libp2p/injector/network_injector.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <libp2p/network/impl/network_impl.hpp>
3232
#include <libp2p/network/impl/router_impl.hpp>
3333
#include <libp2p/network/impl/transport_manager_impl.hpp>
34+
#include <libp2p/peer/address_repository/inmem_address_repository.hpp>
3435
#include <libp2p/peer/impl/identity_manager_impl.hpp>
3536
#include <libp2p/protocol_muxer/multiselect.hpp>
3637
#include <libp2p/security/noise.hpp>
@@ -347,6 +348,8 @@ namespace libp2p::injector {
347348
di::bind<muxer::MuxerAdaptor *[]>().to<muxer::Yamux, muxer::Mplex>(), // NOLINT
348349
di::bind<transport::TransportAdaptor *[]>().to<transport::TcpTransport, transport::QuicTransport>(), // NOLINT
349350

351+
di::bind<peer::AddressRepository>.to<peer::InmemAddressRepository>(),
352+
350353
// user-defined overrides...
351354
std::forward<decltype(args)>(args)...
352355
);

include/libp2p/muxer/muxed_connection_config.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@ namespace libp2p::muxer {
3232
static constexpr std::chrono::milliseconds kDefaultNoStreamsInterval =
3333
std::chrono::milliseconds(120000);
3434
std::chrono::milliseconds no_streams_interval = kDefaultNoStreamsInterval;
35+
36+
/// Dial timeout for outgoing connection
37+
static constexpr std::chrono::seconds kDefaultDialTimeout{10};
38+
std::chrono::milliseconds dial_timeout = kDefaultDialTimeout;
3539
};
3640
} // namespace libp2p::muxer

include/libp2p/network/dialer.hpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,24 @@ namespace libp2p::network {
2828
outcome::result<std::shared_ptr<connection::CapableConnection>>;
2929
using DialResultFunc = std::function<void(DialResult)>;
3030

31-
/**
32-
* Establishes a connection or returns existing one to a given peer with a
33-
* specific timeout
34-
*/
35-
virtual void dial(const peer::PeerInfo &p,
36-
DialResultFunc cb,
37-
std::chrono::milliseconds timeout) = 0;
38-
3931
/**
4032
* Establishes a connection or returns existing one to a given peer
4133
*/
42-
inline void dial(const peer::PeerInfo &p, DialResultFunc cb) {
43-
dial(p, std::move(cb), std::chrono::milliseconds::zero());
44-
}
45-
46-
/**
47-
* NewStream returns a new stream to given peer p with a specific timeout.
48-
* If there is no connection to p, attempts to create one.
49-
*/
50-
virtual void newStream(const peer::PeerInfo &peer_info,
51-
StreamProtocols protocols,
52-
StreamAndProtocolOrErrorCb cb,
53-
std::chrono::milliseconds timeout = {}) = 0;
34+
virtual void dial(const PeerInfo &p, DialResultFunc cb) = 0;
5435

5536
/**
5637
* NewStream returns a new stream to given peer p.
5738
* If there is no connection to p, returns error.
5839
*/
59-
virtual void newStream(const peer::PeerId &peer_id,
40+
virtual void newStream(const PeerInfo &peer_id,
6041
StreamProtocols protocols,
6142
StreamAndProtocolOrErrorCb cb) = 0;
43+
44+
void newStream(const PeerId &peer_id,
45+
StreamProtocols protocols,
46+
StreamAndProtocolOrErrorCb cb) {
47+
newStream(PeerInfo{.id = peer_id}, std::move(protocols), std::move(cb));
48+
}
6249
};
6350

6451
} // namespace libp2p::network

include/libp2p/network/impl/dialer_impl.hpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
#pragma once
88

9-
#include <set>
9+
#include <deque>
1010
#include <unordered_map>
11+
#include <unordered_set>
1112

1213
#include <libp2p/basic/scheduler.hpp>
1314
#include <libp2p/network/connection_manager.hpp>
1415
#include <libp2p/network/dialer.hpp>
1516
#include <libp2p/network/listener_manager.hpp>
1617
#include <libp2p/network/transport_manager.hpp>
18+
#include <libp2p/peer/address_repository.hpp>
1719
#include <libp2p/protocol_muxer/protocol_muxer.hpp>
1820

1921
namespace libp2p::network {
@@ -27,36 +29,25 @@ namespace libp2p::network {
2729
std::shared_ptr<TransportManager> tmgr,
2830
std::shared_ptr<ConnectionManager> cmgr,
2931
std::shared_ptr<ListenerManager> listener,
32+
std::shared_ptr<peer::AddressRepository> addr_repo,
3033
std::shared_ptr<basic::Scheduler> scheduler);
3134

3235
// Establishes a connection to a given peer
33-
void dial(const peer::PeerInfo &p,
34-
DialResultFunc cb,
35-
std::chrono::milliseconds timeout) override;
36+
void dial(const PeerInfo &p, DialResultFunc cb) override;
3637

37-
// NewStream returns a new stream to given peer p.
38-
// If there is no connection to p, attempts to create one.
39-
void newStream(const peer::PeerInfo &p,
40-
StreamProtocols protocols,
41-
StreamAndProtocolOrErrorCb cb,
42-
std::chrono::milliseconds timeout = {}) override;
43-
44-
void newStream(const peer::PeerId &peer_id,
38+
void newStream(const PeerInfo &peer_id,
4539
StreamProtocols protocols,
4640
StreamAndProtocolOrErrorCb cb) override;
4741

4842
private:
4943
// A context to handle an intermediary state of the peer we are dialing to
5044
// but the connection is not yet established
5145
struct DialCtx {
52-
/// Known and scheduled addresses to try to dial via
53-
std::set<multi::Multiaddress> addresses;
54-
55-
/// Timeout for a single connection attempt
56-
std::chrono::milliseconds timeout;
46+
/// Queue of addresses to try connect to
47+
std::deque<Multiaddress> addr_queue;
5748

58-
/// Addresses we already tried, but no connection was established
59-
std::set<multi::Multiaddress> tried_addresses;
49+
/// Tracks addresses added to `addr_queue`
50+
std::unordered_set<Multiaddress> addr_seen;
6051

6152
/// Callbacks for all who requested a connection to the peer
6253
std::vector<Dialer::DialResultFunc> callbacks;
@@ -86,6 +77,7 @@ namespace libp2p::network {
8677
std::shared_ptr<TransportManager> tmgr_;
8778
std::shared_ptr<ConnectionManager> cmgr_;
8879
std::shared_ptr<ListenerManager> listener_;
80+
std::shared_ptr<peer::AddressRepository> addr_repo_;
8981
std::shared_ptr<basic::Scheduler> scheduler_;
9082
log::Logger log_;
9183

include/libp2p/peer/address_repository.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ namespace libp2p::peer {
123123
virtual outcome::result<void> updateAddresses(const PeerId &p,
124124
Milliseconds ttl) = 0;
125125

126+
/**
127+
* Move failed address to back.
128+
* That way dialer will try other addresses first.
129+
*/
130+
virtual void dialFailed(const PeerId &peer_id,
131+
const Multiaddress &addr) = 0;
132+
126133
/**
127134
* @brief Get all addresses associated with this Peer {@param p}. May
128135
* contain duplicates.

include/libp2p/peer/address_repository/inmem_address_repository.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ namespace libp2p::peer {
5050
outcome::result<void> updateAddresses(const PeerId &p,
5151
Milliseconds ttl) override;
5252

53+
void dialFailed(const PeerId &peer_id, const Multiaddress &addr) override;
54+
5355
outcome::result<std::vector<multi::Multiaddress>> getAddresses(
5456
const PeerId &p) const override;
5557

@@ -60,13 +62,15 @@ namespace libp2p::peer {
6062
std::unordered_set<PeerId> getPeers() const override;
6163

6264
private:
63-
using ttlmap = std::unordered_map<multi::Multiaddress, Clock::time_point>;
64-
using ttlmap_ptr = std::shared_ptr<ttlmap>;
65-
using peer_db = std::unordered_map<PeerId, ttlmap_ptr>;
65+
struct Peer {
66+
std::unordered_map<Multiaddress, Clock::time_point> expires;
67+
std::vector<Multiaddress> order;
6668

67-
bool isNewDnsAddr(const multi::Multiaddress &ma);
69+
bool eraseOrder(const Multiaddress &addr);
70+
};
71+
using peer_db = std::unordered_map<PeerId, Peer>;
6872

69-
peer_db::iterator findOrInsert(const PeerId &p);
73+
bool isNewDnsAddr(const multi::Multiaddress &ma);
7074

7175
std::shared_ptr<network::DnsaddrResolver> dnsaddr_resolver_;
7276
peer_db db_;

include/libp2p/peer/peer_info.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ namespace libp2p::peer {
4040

4141
} // namespace libp2p::peer
4242

43+
namespace libp2p {
44+
using peer::PeerInfo;
45+
} // namespace libp2p
46+
4347
namespace std {
4448
template <>
4549
struct hash<libp2p::peer::PeerInfo> {

0 commit comments

Comments
 (0)