From 1e7248b330bde4c70528ccf91535391e19294dc3 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 7 Nov 2026 16:54:36 +0100 Subject: Add a knob to help reducing memory consumption. This time: empty torrents may expire sooner --- man5/opentracker.conf.5 | 7 +++++++ opentracker.conf.sample | 7 +++++++ src/ot_clean.c | 4 +++- src/ot_config.c | 7 +++++++ src/trackerlogic.h | 5 +++++ tests/integration/test_config.py | 21 +++++++++++++++++++++ tests/t_clean.c | 36 ++++++++++++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 1 deletion(-) diff --git a/man5/opentracker.conf.5 b/man5/opentracker.conf.5 index 472d83b..cc4c942 100644 --- a/man5/opentracker.conf.5 +++ b/man5/opentracker.conf.5 @@ -108,6 +108,13 @@ Specifies the user opentracker will setuid to after binding to potentially privi .Pp .It tracker.redirect_url Ar URL Specifies the URL opentracker will redirect to in response to a "GET / HTTP" request. +.Pp +.It tracker.empty_torrent_timeout Ar minutes +Specifies how long a torrent that has no peers left in either family is held for +its download count alone. +Defaults to and is limited to 1440 minutes. Lower values save memory at the cost +of less realiable download counts. A value of zero immediately destroys empty +torrents. .El .Sh TUNING A worker is a thread that blocks in a read until a datagram arrives. Where the diff --git a/opentracker.conf.sample b/opentracker.conf.sample index ba8497f..da0b601 100644 --- a/opentracker.conf.sample +++ b/opentracker.conf.sample @@ -186,3 +186,10 @@ # redirect to another location (shell option -r). # # tracker.redirect_url https://your.tracker.local/ + +# VII) How long a torrent that has fallen empty is kept for its download count +# alone, in minutes. Default and maximum is 1440 minutes (24 hours). +# Trade-off: lower value less memory usage at the cost of less reliable +# download counts. +# +# tracker.empty_torrent_timeout 60 diff --git a/src/ot_clean.c b/src/ot_clean.c index 77cb4ba..9e2186f 100644 --- a/src/ot_clean.c +++ b/src/ot_clean.c @@ -16,6 +16,8 @@ #include "ot_vector.h" #include "trackerlogic.h" +ot_time g_empty_torrent_timeout = OT_TORRENT_TIMEOUT; + /* Returns amount of removed peers */ static ssize_t clean_single_bucket(ot_peer *peers, size_t peer_count, size_t peer_size, time_t timedout, size_t *removed_seeders) { ot_peer *last_peer, *insert_point; @@ -106,7 +108,7 @@ int clean_single_torrent(ot_torrent *torrent) { /* Already empty and nothing arrived before the peer timeout: keep it only while a down count is worth remembering */ if (timedout > OT_PEER_TIMEOUT && !torrent->peer_list6 && !torrent->peer_list4) - return torrent->down_count ? 0 : 1; + return (torrent->down_count && timedout <= g_empty_torrent_timeout) ? 0 : 1; /* Peers past the peer timeout are gone regardless, which caps how far the lists are aged in this pass */ diff --git a/src/ot_config.c b/src/ot_config.c index 9c5e8f0..8af6cfc 100644 --- a/src/ot_config.c +++ b/src/ot_config.c @@ -192,6 +192,7 @@ typedef enum { CONFIG_UNKNOWN, CONFIG_TRACKER_ROOTDIR, CONFIG_TRACKER_USER, + CONFIG_TRACKER_EMPTY_TIMEOUT, CONFIG_LISTEN_TCP_UDP, CONFIG_LISTEN_TCP, CONFIG_LISTEN_UDP, @@ -223,6 +224,7 @@ typedef struct { static const config_key config_keys[] = { CONFIG_KEY("tracker.rootdir", CONFIG_TRACKER_ROOTDIR), CONFIG_KEY("tracker.user", CONFIG_TRACKER_USER), + CONFIG_KEY("tracker.empty_torrent_timeout", CONFIG_TRACKER_EMPTY_TIMEOUT), CONFIG_KEY("listen.tcp_udp", CONFIG_LISTEN_TCP_UDP), CONFIG_KEY("listen.tcp", CONFIG_LISTEN_TCP), CONFIG_KEY("listen.udp", CONFIG_LISTEN_UDP), @@ -316,6 +318,11 @@ static int parse_configfile(char *config_filename) { case CONFIG_TRACKER_USER: set_config_option(&g_serveruser, p); break; + case CONFIG_TRACKER_EMPTY_TIMEOUT: + if (!scan_uint(p, &value, UINT_MAX)) + goto parse_error; + g_empty_torrent_timeout = (ot_time)value; + break; case CONFIG_LISTEN_TCP_UDP: case CONFIG_LISTEN_TCP: case CONFIG_LISTEN_UDP: { diff --git a/src/trackerlogic.h b/src/trackerlogic.h index 6b53406..c4b7774 100644 --- a/src/trackerlogic.h +++ b/src/trackerlogic.h @@ -44,6 +44,11 @@ typedef struct { #define OT_TORRENT_TIMEOUT_HOURS 24 #define OT_TORRENT_TIMEOUT (60 * OT_TORRENT_TIMEOUT_HOURS) +/* How long a torrent kept only for its download count, with no peers in either + family, is held, in minutes. Defaults to OT_TORRENT_TIMEOUT and is lowered by + tracker.empty_torrent_timeout. */ +extern ot_time g_empty_torrent_timeout; + #define OT_CLIENT_REQUEST_INTERVAL_RANDOM \ (OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION / 2 + (int)(nrand48(ws->rand48_state) % OT_CLIENT_REQUEST_VARIATION)) #define OT_CLIENT_REJECT_INTERVAL_RANDOM \ diff --git a/tests/integration/test_config.py b/tests/integration/test_config.py index 2f6bdc4..fe5cf9d 100644 --- a/tests/integration/test_config.py +++ b/tests/integration/test_config.py @@ -49,3 +49,24 @@ class AListenLineThatDoesNotParse(unittest.TestCase): def test_so_does_an_option_it_does_not_know(self): self.assertIn("want an address", self.refuses("listen.tcp 127.0.0.1:7999 wokers=2\n")) + + +class EmptyTorrentTimeout(TrackerTestCase): + config = "tracker.empty_torrent_timeout 60\n" + + def test_the_tracker_accepts_the_option_and_serves(self): + self.assertEqual(self.tracker.announce(self.hash_for("emptyto"))[b"complete"], 1) + self.assertNotIn("Unhandled line", self.tracker.output()) + self.assertNotIn("Parse error", self.tracker.output()) + + +class EmptyTorrentTimeoutThatDoesNotParse(unittest.TestCase): + def test_a_non_numeric_value_warns_but_the_tracker_serves(self): + """A scalar that will not parse warns and is ignored, unlike a listen + line, which stops the tracker.""" + tracker = Tracker("tracker.empty_torrent_timeout later\n") + try: + self.assertEqual(tracker.announce(b"emptytobad\0\0\0\0\0\0\0\0\0\0")[b"complete"], 1) + self.assertIn("Parse error", tracker.output()) + finally: + tracker._cleanup() diff --git a/tests/t_clean.c b/tests/t_clean.c index 2fadd03..0237cc6 100644 --- a/tests/t_clean.c +++ b/tests/t_clean.c @@ -122,6 +122,41 @@ static void case_an_idle_swarm_still_ages_out(void) { workstruct_deinit(&ws); } +static void case_empty_torrent_timeout_bounds_a_counter_only_torrent(void) { + struct ot_workstruct ws; + ot_hash hash; + ot_torrent *torrent; + ot_time saved = g_empty_torrent_timeout; + + test_case("the empty torrent timeout bounds a counter-only torrent's life"); + memset(hash, 0x33, sizeof(hash)); + CHECK_INT(workstruct_init(&ws, G_INBUF_SIZE), 0); + + announce(&ws, &hash, 7, 1); + torrent = torrent_of(&hash); + CHECK(torrent != NULL); + torrent->down_count = 1; /* one completed download to remember it by */ + + /* Age the only peer out: the torrent falls empty but keeps its count */ + step_clock_back(torrent, -OT_PEER_TIMEOUT); + CHECK_INT(clean_single_torrent(torrent), 0); + CHECK_INT(peers_in(&hash), 0); + + /* Carry it a little past a short empty-torrent timeout */ + step_clock_back(torrent, -10); + + /* The default, matching the torrent timeout, still holds it for the count */ + g_empty_torrent_timeout = OT_TORRENT_TIMEOUT; + CHECK_INT(clean_single_torrent(torrent), 0); + + /* A timeout it has now exceeded lets the next sweep drop it */ + g_empty_torrent_timeout = OT_PEER_TIMEOUT + 5; + CHECK_INT(clean_single_torrent(torrent), 1); + + g_empty_torrent_timeout = saved; + workstruct_deinit(&ws); +} + void test_clean(void) { sigset_t hup; @@ -134,5 +169,6 @@ void test_clean(void) { trackerlogic_init(); case_a_clock_that_went_back_keeps_the_swarm(); case_an_idle_swarm_still_ages_out(); + case_empty_torrent_timeout_bounds_a_counter_only_torrent(); trackerlogic_deinit(); } -- cgit v1.3