|
26 | 26 |
|
27 | 27 | #include <algorithm>
|
28 | 28 | #include <cassert>
|
| 29 | +#include <cstdint> |
29 | 30 | #include <cstdlib>
|
30 | 31 | #include <deque>
|
31 | 32 | #include <functional>
|
@@ -60,6 +61,8 @@ static const std::string TOR_SAFE_CLIENTKEY = "Tor safe cookie authentication co
|
60 | 61 | static const float RECONNECT_TIMEOUT_START = 1.0;
|
61 | 62 | /** Exponential backoff configuration - growth factor */
|
62 | 63 | static const float RECONNECT_TIMEOUT_EXP = 1.5;
|
| 64 | +/** Maximum reconnect timeout in seconds to prevent excessive delays */ |
| 65 | +static const float RECONNECT_TIMEOUT_MAX = 600.0; |
63 | 66 | /** Maximum length for lines received on TorControlConnection.
|
64 | 67 | * tor-control-spec.txt mentions that there is explicitly no limit defined to line length,
|
65 | 68 | * this is belt-and-suspenders sanity limit to prevent memory exhaustion.
|
@@ -631,13 +634,15 @@ void TorController::disconnected_cb(TorControlConnection& _conn)
|
631 | 634 | if (!reconnect)
|
632 | 635 | return;
|
633 | 636 |
|
634 |
| - LogDebug(BCLog::TOR, "Not connected to Tor control port %s, trying to reconnect\n", m_tor_control_center); |
| 637 | + LogDebug(BCLog::TOR, "Not connected to Tor control port %s, retrying in %.2f s\n", |
| 638 | + m_tor_control_center, reconnect_timeout); |
635 | 639 |
|
636 |
| - // Single-shot timer for reconnect. Use exponential backoff. |
| 640 | + // Single-shot timer for reconnect. Use exponential backoff with a maximum. |
637 | 641 | struct timeval time = MillisToTimeval(int64_t(reconnect_timeout * 1000.0));
|
638 | 642 | if (reconnect_ev)
|
639 | 643 | event_add(reconnect_ev, &time);
|
640 |
| - reconnect_timeout *= RECONNECT_TIMEOUT_EXP; |
| 644 | + |
| 645 | + reconnect_timeout = std::min(reconnect_timeout * RECONNECT_TIMEOUT_EXP, RECONNECT_TIMEOUT_MAX); |
641 | 646 | }
|
642 | 647 |
|
643 | 648 | void TorController::Reconnect()
|
|
0 commit comments