Back to Site
Loading...
Searching...
No Matches
reconnection.h File Reference

Keeps connections to a set of peer addresses alive with backoff. More...

#include "librats/util/rats_export.h"
#include "librats/node/peer_network.h"
#include "librats/peer/peer.h"
#include "librats/core/address.h"
#include "librats/peer/peer_id.h"
#include "librats/peer/peer_book.h"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <functional>
#include <memory>
#include <optional>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
Include dependency graph for reconnection.h:

Go to the source code of this file.

Classes

class  librats::ReconnectionService
struct  librats::ReconnectionService::Config

Namespaces

namespace  librats

Detailed Description

Keeps connections to a set of peer addresses alive with backoff.

A Subsystem that remembers peer addresses (optionally persisted via PeerBook) and re-dials them when they drop, using exponential backoff. Built only on PeerNetwork: it dials via connect() and, each tick, reconciles its targets against the set of currently-connected peers (PeerNetwork::peers(), whose addresses include the dialable endpoints learned via identify). A target whose address is served by a live peer — over ANY route, inbound or outbound — is left alone; only the rest are dialed. Reconciling against this snapshot, rather than trusting a flag toggled on the connect event, is what stops us from endlessly re-dialing a peer already connected over a link we did not initiate (an inbound link, or a cross-connect the directory resolved to the other side).

A target is dropped when the app calls remove(), or — if Config::max_attempts is set — after that many consecutive failed dials without ever connecting (it "gives up", freeing memory and pruning the store). A successful connection resets the attempt counter, so only persistently-dead addresses are reaped.

Lost peers (Config::lost_for): a connection closed by neither side — the peer went quiet past the transport's idle deadline, or stopped answering — was not ended, so its peer is dialed again, at the addresses it was last known at and the endpoint its packets came from, for lost_for, until it is connected again over any route (it may be the one to dial: both sides of a lost link try) or given up on (on_given_up()). Where the host's network changes those dials start over at once: what was tried before went out on the network that went. A peer that left — it closed, or this node did — is not dialed again.

Definition in file reconnection.h.