Back to Site
Loading...
Searching...
No Matches
relay.h
Go to the documentation of this file.
1#pragma once
2
87
88#include "librats/util/rats_export.h"
93#include "librats/transport/relay_link.h" // Circuit::kDefaultWindow
94
95#include <atomic>
96#include <chrono>
97#include <condition_variable>
98#include <cstdint>
99#include <memory>
100#include <mutex>
101#include <thread>
102
103namespace librats {
104
105class RATS_API Relay final : public Subsystem, public RelayService {
106public:
107 struct Config {
108 // ── Using relays to reach peers we cannot dial ──────────────────────
109
111 bool enable_client = true;
112
116 size_t max_probes = 3;
117
121
125 std::chrono::milliseconds open_timeout{8000};
126
129 std::chrono::milliseconds cooldown{60000};
130
131 // ── Accepting circuits opened to us ─────────────────────────────────
132
135 bool accept_inbound = true;
136
139
140 // ── Carrying other peers' traffic ───────────────────────────────────
141
145 bool serve = false;
146
148 size_t max_circuits = 32;
149
153
162 uint64_t max_circuit_rate_bps = 10'000'000;
163
165 std::chrono::milliseconds circuit_burst{2000};
166
170 std::chrono::seconds max_circuit_duration{3600};
171
173 size_t request_budget = 8;
174 std::chrono::milliseconds request_window{1000};
175
176 // ── Both ends ───────────────────────────────────────────────────────
177
182 uint32_t window = Circuit::kDefaultWindow;
183
188
190 std::chrono::milliseconds tick{250};
191 };
192
194 explicit Relay(Config config);
195 ~Relay() override;
196
197 void attach(NodeContext& ctx) override;
198 void start() override;
199 void stop() override;
200
202 bool connect_via_relay(const PeerId& target) override;
204 bool serves(const PeerId& peer) const override;
205
206 // — diagnostics and tests —
208 size_t circuits() const;
210 size_t carried_circuits() const;
212 size_t forwarded_circuits() const;
214 uint64_t carried_bytes() const;
216 size_t attempts() const;
217
218private:
222 struct State;
223
224 Config config_;
225 std::shared_ptr<State> state_;
228 ServiceRegistry* services_ = nullptr;
229
230 std::thread worker_;
231 std::atomic<bool> running_{false};
232 std::mutex worker_mutex_;
233 std::condition_variable worker_cv_;
234
235 void loop();
236};
237
238} // namespace librats
size_t carried_circuits() const
Circuits this node is carrying on behalf of other peers.
size_t attempts() const
Attempts to reach a peer through a relay that are currently running.
bool connect_via_relay(const PeerId &target) override
Try to reach target through a node both ends are connected to.
size_t forwarded_circuits() const
Of those, the ones whose datagrams it forwards rather than carrying a stream.
~Relay() override
Relay(Config config)
void start() override
uint64_t carried_bytes() const
Bytes forwarded on behalf of other peers.
void stop() override
size_t circuits() const
Circuits this node terminates (relayed peers, live or still being set up).
bool serves(const PeerId &peer) const override
Whether peer said it carries circuits for others — a node to try first for anything relayed,...
void attach(NodeContext &ctx) override
A pluggable network subsystem.
Definition node.h:75
Self-certifying peer identity.
The narrow contract a subsystem needs from the node — and nothing more.
Capability that lets a sibling module ask for a peer to be reached through a third node,...
Interface-keyed service lookup for targeted, synchronous module calls.
std::chrono::milliseconds request_window
Definition relay.h:174
std::chrono::milliseconds tick
Bookkeeping cadence — attempt timeouts, cooldowns, circuit expiry.
Definition relay.h:190
uint64_t max_circuit_rate_bps
Bits per second one circuit may move each way, on average over circuit_burst: bursts up to that much ...
Definition relay.h:162
std::chrono::milliseconds open_timeout
How long an attempt may take from the first probe to a live connection.
Definition relay.h:125
std::chrono::milliseconds circuit_burst
What the rate is averaged over.
Definition relay.h:165
uint32_t window
Bytes this node is willing to have in flight per byte-stream circuit — the credit window it advertise...
Definition relay.h:182
std::chrono::milliseconds cooldown
How long after a failed attempt before the same target may be tried again.
Definition relay.h:129
bool enable_client
Open circuits through other peers. Off makes this a relay-only node.
Definition relay.h:111
size_t max_outbound_circuits
Circuits this node may hold open as a client at once.
Definition relay.h:120
bool upgrade_with_hole_punch
When a relayed peer connects, ask HolePunch to try it directly, so the circuit is replaced by a direc...
Definition relay.h:187
bool accept_inbound
Accept circuits other peers open toward us.
Definition relay.h:135
bool serve
Serve as a relay.
Definition relay.h:145
size_t max_probes
Peers asked whether they hold a target, per attempt.
Definition relay.h:116
size_t max_circuits_per_peer
Circuits one peer may have us carrying, so a single peer cannot take the whole budget.
Definition relay.h:152
size_t max_circuits
Circuits this node will carry for others at once.
Definition relay.h:148
size_t request_budget
Requests (probes and circuit openings) one peer may make per window.
Definition relay.h:173
std::chrono::seconds max_circuit_duration
How long one circuit may live.
Definition relay.h:170
size_t max_inbound_circuits
Circuits opened TO us that may be live at once.
Definition relay.h:138