33#include "librats/util/rats_export.h"
38#include "librats/peer/peer_book.h"
42#include <condition_variable>
49#include <unordered_map>
65 std::chrono::milliseconds
tick{1000};
73 static bool lost(CloseReason reason)
noexcept {
74 return reason == CloseReason::IdleTimeout || reason == CloseReason::PeerReset;
113 bool dialing =
false;
115 std::chrono::steady_clock::time_point next_attempt;
116 std::chrono::steady_clock::time_point dial_deadline;
117 std::optional<PeerId>
lost;
124 void mark_connected(std::chrono::steady_clock::time_point now) {
133 std::chrono::steady_clock::time_point until;
136 void on_connected(
const Peer& peer);
137 void on_disconnected(
const PeerId&
id, CloseReason reason);
138 void on_network_changed();
140 void forget_lost(
const PeerId&
id);
141 void on_dial_failed(
const Address& address);
143 std::chrono::milliseconds backoff_for(
int attempts)
const;
146 PeerNetwork* network_ =
nullptr;
151 std::unique_ptr<PeerBook> book_;
154 std::atomic<bool> running_{
false};
155 std::mutex wait_mutex_;
156 std::condition_variable wake_;
158 mutable std::mutex mutex_;
159 std::unordered_map<Address, Target> targets_;
162 std::unordered_map<PeerId, std::vector<Address>, PeerId::Hash> known_;
163 std::unordered_map<PeerId, Lost, PeerId::Hash> lost_;
164 std::function<void(
const PeerId&)> given_up_;
A dialable transport endpoint: a numeric IP + port.
ReconnectionService(Config config)
std::vector< Address > known_peers(size_t n) const
The passive reserve pool: up to n best-known peer addresses from the book (history of everyone we hav...
static bool lost(CloseReason reason) noexcept
Whether a connection that closed for reason was lost rather than ended: neither side closed it — the ...
size_t target_count() const
void add(const Address &address)
Register an address to keep connected. Persists it if a store is configured.
void remove(const Address &address)
Stop reconnecting to an address: drops it as a target and from the store.
std::vector< PeerId > lost_peers() const
The peers lost and being dialed again (Config::lost_for), until each is back or given up on.
void on_given_up(std::function< void(const PeerId &)> handler)
Called for a lost peer not back within Config::lost_for, on the service's thread.
~ReconnectionService() override
void attach(NodeContext &ctx) override
A pluggable network subsystem.
A lightweight handle to a connected peer.
Self-certifying peer identity.
The narrow contract a subsystem needs from the node — and nothing more.
size_t max_targets
cap on ACTIVE re-dial targets (bounds memory + dial fan-out)
size_t startup_targets
on start, actively re-dial this many best peers from the book
std::chrono::milliseconds base_backoff
std::chrono::milliseconds lost_for
dial a lost peer again for this long (see above); 0 = never
std::chrono::milliseconds max_backoff
size_t max_attempts
give up actively dialing a target after this many consecutive failures; 0 = retry forever
std::chrono::milliseconds tick
bool persist_discovered
remember dialed peers automatically
std::chrono::milliseconds dial_timeout
assume an in-flight dial is dead after this if no connect/fail event arrives (backstop only)
size_t archive_max
cap on the persistent peer book (history of everyone we met)
std::string store_path
persist the peer book here (empty = memory only)
std::chrono::seconds archive_max_age
forget peers unseen this long (30 days)