Back to Site
Loading...
Searching...
No Matches
reconnection.h
Go to the documentation of this file.
1#pragma once
2
24#include "util/rats_export.h"
25#include "node/peer_network.h"
26#include "peer/peer.h"
27#include "core/address.h"
28#include "peer/peer_id.h"
29#include "peer/peer_book.h"
30
31#include <atomic>
32#include <chrono>
33#include <condition_variable>
34#include <memory>
35#include <mutex>
36#include <string>
37#include <thread>
38#include <unordered_map>
39
40namespace librats {
41
42class RATS_API ReconnectionService final : public Subsystem {
43public:
44 struct Config {
45 std::string store_path = "";
46 bool persist_discovered = true;
47 size_t max_targets = 1024;
48 size_t max_attempts = 0;
49 size_t startup_targets = 32;
50 size_t archive_max = 4096;
51 std::chrono::seconds archive_max_age{std::chrono::hours(24 * 30)};
52 std::chrono::milliseconds base_backoff{1000};
53 std::chrono::milliseconds max_backoff{60000};
54 std::chrono::milliseconds tick{1000};
55 std::chrono::milliseconds dial_timeout{15000};
56 };
57
59 explicit ReconnectionService(Config config);
61
63 void add(const Address& address);
64
69 void remove(const Address& address);
70
71 size_t target_count() const;
72
77 std::vector<Address> known_peers(size_t n) const;
78
79 void attach(NodeContext& ctx) override;
80 void start() override;
81 void stop() override;
82
83private:
84 struct Target {
85 Address address;
86 bool dialing = false;
87 int attempts = 0;
88 std::chrono::steady_clock::time_point next_attempt;
89 std::chrono::steady_clock::time_point dial_deadline;
90
96 void mark_connected(std::chrono::steady_clock::time_point now) {
97 dialing = false;
98 attempts = 0;
99 next_attempt = now;
100 }
101 };
102
103 void on_connected(const Peer& peer);
104 void on_disconnected(const PeerId& id);
105 void on_dial_failed(const Address& address);
106 void loop();
107 std::chrono::milliseconds backoff_for(int attempts) const;
108
109 Config config_;
110 PeerNetwork* network_ = nullptr;
111 // Built once in the constructor (when store_path is set) and never reassigned,
112 // so the pointer is safe to read from any thread; PeerBook is itself internally
113 // synchronized. (Creating it in start() raced reads from on_connected, which can
114 // fire on a reactor thread before this subsystem's start() returns.)
115 std::unique_ptr<PeerBook> book_;
116
117 std::thread thread_;
118 std::atomic<bool> running_{false};
119 std::mutex wait_mutex_;
120 std::condition_variable wake_;
121
122 mutable std::mutex mutex_;
123 std::unordered_map<Address, Target> targets_;
124};
125
126} // 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...
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.
void attach(NodeContext &ctx) override
A pluggable network subsystem.
Definition node.h:66
A lightweight handle to a connected peer.
Self-certifying peer identity.
The narrow contract a subsystem needs from the node — and nothing more.