Back to Site
Loading...
Searching...
No Matches
reconnection.h
Go to the documentation of this file.
1#pragma once
2
32
33#include "librats/util/rats_export.h"
35#include "librats/peer/peer.h"
38#include "librats/peer/peer_book.h"
39
40#include <atomic>
41#include <chrono>
42#include <condition_variable>
43#include <functional>
44#include <memory>
45#include <optional>
46#include <mutex>
47#include <string>
48#include <thread>
49#include <unordered_map>
50
51namespace librats {
52
53class RATS_API ReconnectionService final : public Subsystem {
54public:
55 struct Config {
56 std::string store_path = "";
57 bool persist_discovered = true;
58 size_t max_targets = 1024;
59 size_t max_attempts = 0;
60 size_t startup_targets = 32;
61 size_t archive_max = 4096;
62 std::chrono::seconds archive_max_age{std::chrono::hours(24 * 30)};
63 std::chrono::milliseconds base_backoff{1000};
64 std::chrono::milliseconds max_backoff{60000};
65 std::chrono::milliseconds tick{1000};
66 std::chrono::milliseconds dial_timeout{15000};
67 std::chrono::milliseconds lost_for{0};
68 };
69
73 static bool lost(CloseReason reason) noexcept {
74 return reason == CloseReason::IdleTimeout || reason == CloseReason::PeerReset;
75 }
76
78 explicit ReconnectionService(Config config);
80
82 void add(const Address& address);
83
88 void remove(const Address& address);
89
90 size_t target_count() const;
91
96 std::vector<Address> known_peers(size_t n) const;
97
100 std::vector<PeerId> lost_peers() const;
101
104 void on_given_up(std::function<void(const PeerId&)> handler);
105
106 void attach(NodeContext& ctx) override;
107 void start() override;
108 void stop() override;
109
110private:
111 struct Target {
112 Address address;
113 bool dialing = false;
114 int attempts = 0;
115 std::chrono::steady_clock::time_point next_attempt;
116 std::chrono::steady_clock::time_point dial_deadline;
117 std::optional<PeerId> lost;
118
124 void mark_connected(std::chrono::steady_clock::time_point now) {
125 dialing = false;
126 attempts = 0;
127 next_attempt = now;
128 }
129 };
130
132 struct Lost {
133 std::chrono::steady_clock::time_point until;
134 };
135
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);
142 void loop();
143 std::chrono::milliseconds backoff_for(int attempts) const;
144
145 Config config_;
146 PeerNetwork* network_ = nullptr;
147 // Built once in the constructor (when store_path is set) and never reassigned,
148 // so the pointer is safe to read from any thread; PeerBook is itself internally
149 // synchronized. (Creating it in start() raced reads from on_connected, which can
150 // fire on a reactor thread before this subsystem's start() returns.)
151 std::unique_ptr<PeerBook> book_;
152
153 std::thread thread_;
154 std::atomic<bool> running_{false};
155 std::mutex wait_mutex_;
156 std::condition_variable wake_;
157
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_;
165};
166
167} // namespace librats
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 ...
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.
void attach(NodeContext &ctx) override
A pluggable network subsystem.
Definition node.h:75
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)