diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-31 15:59:41 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-31 15:59:41 +0100 |
| commit | 4d030871c004c917458b5e1c296edcfc689a2e66 (patch) | |
| tree | c6c057e47a17d8b03ea28a611ff029d950acfa8c /tests/integration/test_tcpworkers.py | |
| parent | 440cfeee71829308fa1cc9b5aef63beb7ab756dc (diff) | |
Make tcp worker multithreaded, too
This shamelessly steals the model established in the UDP workers, i.e.
if the kernel is able to bind multiple listening sockets to the same
endpoint, we will share them across threads. If it can't, all threads
will accept on the same socket.
The fast path handles most of the connections, only the rare case of
no data yet on the socket is offloaded to the slow path (which used to
be the mainloop) for completion. This scales nicely, as the higher the
load is, the higher chances are that an accept happens only after the
network roundtrip to the peer completes, leading to more connections
that already have data waiting for us.
We also consolidate the workstruct init and deinit code, preparation
for later unifying all thread state structs for all protocols (mca,
udc and tcp).
The config file gains a per tcp connection worker count and some docs
explaining what this knob buys and costs, nudging the user to either
keep it 0 or dial it up to at least 2.
Diffstat (limited to 'tests/integration/test_tcpworkers.py')
| -rw-r--r-- | tests/integration/test_tcpworkers.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/integration/test_tcpworkers.py b/tests/integration/test_tcpworkers.py new file mode 100644 index 0000000..e43612a --- /dev/null +++ b/tests/integration/test_tcpworkers.py | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | """The tracker with its tcp side handed to worker threads. | ||
| 2 | |||
| 3 | With workers configured, a connection is accepted and answered on a worker's | ||
| 4 | own stack. Anything it cannot finish there is copied to the heap and handed | ||
| 5 | to the event loop, which is the one place where a worker and the main thread | ||
| 6 | touch the same structures, so these drive all three outcomes: answered | ||
| 7 | inline, held open, and parked waiting for another thread entirely. | ||
| 8 | """ | ||
| 9 | |||
| 10 | import threading | ||
| 11 | import unittest | ||
| 12 | import urllib.parse | ||
| 13 | |||
| 14 | from harness import TrackerTestCase, bdecode | ||
| 15 | |||
| 16 | WORKERS = 4 | ||
| 17 | |||
| 18 | |||
| 19 | class TcpWorkers(TrackerTestCase): | ||
| 20 | config = "listen.tcp.workers %d\n" % WORKERS | ||
| 21 | |||
| 22 | def announce_path(self, name, port=6881): | ||
| 23 | return "/announce?info_hash=%s&peer_id=%s&port=%d&left=0" % ( | ||
| 24 | urllib.parse.quote_from_bytes(self.hash_for(name), safe=""), | ||
| 25 | "x" * 20, | ||
| 26 | port, | ||
| 27 | ) | ||
| 28 | |||
| 29 | def test_many_at_once_are_all_answered(self): | ||
| 30 | """Every worker gets something to do, and the swarm adds up.""" | ||
| 31 | info_hash = self.hash_for("workers-swarm") | ||
| 32 | errors = [] | ||
| 33 | |||
| 34 | def announce(index): | ||
| 35 | try: | ||
| 36 | self.tracker.announce(info_hash, peer_id=b"-OT0001-%012d" % index, port=7000 + index, left=0) | ||
| 37 | except Exception as problem: # noqa: BLE001 - reported, not swallowed | ||
| 38 | errors.append("%d: %r" % (index, problem)) | ||
| 39 | |||
| 40 | threads = [threading.Thread(target=announce, args=(i,)) for i in range(32)] | ||
| 41 | for thread in threads: | ||
| 42 | thread.start() | ||
| 43 | for thread in threads: | ||
| 44 | thread.join() | ||
| 45 | |||
| 46 | self.assertEqual(errors, [], "\n".join(errors)) | ||
| 47 | self.assertEqual(self.tracker.scrape(info_hash)[b"files"][info_hash][b"complete"], 32) | ||
| 48 | |||
| 49 | def test_a_kept_connection_outlives_the_worker_that_took_it(self): | ||
| 50 | """Keep-alive is the plain case of a connection a worker hands on.""" | ||
| 51 | connection = self.tracker.connect() | ||
| 52 | try: | ||
| 53 | for i in range(4): | ||
| 54 | connection.send(self.announce_path("workers-kept")) | ||
| 55 | status, _, body = connection.reply() | ||
| 56 | self.assertIn(b"200", status) | ||
| 57 | self.assertIn(b"complete", bdecode(body)) | ||
| 58 | finally: | ||
| 59 | connection.close() | ||
| 60 | |||
| 61 | def test_a_full_scrape_asked_for_by_a_worker_still_arrives(self): | ||
| 62 | """The reply is built by another thread, so the worker parks the | ||
| 63 | connection waiting for nothing at all before handing it over.""" | ||
| 64 | for i in range(20): | ||
| 65 | self.tracker.announce(bytes([i]) + b"\0" * 19, port=7100 + i, left=0) | ||
| 66 | body = self.tracker.get("/scrape") | ||
| 67 | self.assertEqual(len(bdecode(body)[b"files"]), 20) | ||
