Optimized packet-level link callback handling to use single, batched thread model

This commit is contained in:
Mark Qvist 2026-12-02 13:26:44 +01:00
parent b572537e2b
commit b2c2f59071
No known key found for this signature in database

View file

@ -33,6 +33,7 @@ from RNS.Stamps import stamp_valid, stamp_value, generate_stamped_key, cancel_wo
from RNS.Channel import Channel, LinkChannelOutlet
from time import sleep
from collections import deque
from .vendor import umsgpack as umsgpack
import threading
import inspect
@ -145,6 +146,8 @@ class Link:
MTU_BYTEMASK = 0x1FFFFF
MODE_BYTEMASK = 0xE0
PACKET_MAX_PENDING = 2048
@staticmethod
def signalling_bytes(mtu, mode):
if not mode in Link.ENABLED_MODES: raise TypeError(f"Requested link mode {Link.MODE_DESCRIPTIONS[mode]} not enabled")
@ -254,6 +257,11 @@ class Link:
self.outgoing_resources = []
self.incoming_resources = []
self.pending_requests = []
self.packet_max_pending = Link.PACKET_MAX_PENDING
self.pending_packets = deque(maxlen=self.packet_max_pending)
self.callback_condition = threading.Condition()
self.callback_thread = None
self.inbound_dropped = 0
self.last_inbound = 0
self.last_outbound = 0
self.last_keepalive = 0
@ -409,6 +417,23 @@ class Link:
RNS.log(f"Link request {RNS.prettyhexrep(self.link_id)} sent to {self.destination}", RNS.LOG_DEBUG)
RNS.log(f"Establishment timeout is {RNS.prettytime(self.establishment_timeout)} for link request {RNS.prettyhexrep(self.link_id)}", RNS.LOG_EXTREME)
def __callback_loop(self):
try:
while self.status < Link.CLOSED:
batch = []
with self.callback_condition:
while len(self.pending_packets) == 0 and self.status < Link.CLOSED: self.callback_condition.wait(0.2)
batch = list(self.pending_packets)
self.pending_packets.clear()
if self.callbacks.packet:
for plaintext, packet in batch:
try: self.callbacks.packet(plaintext, packet)
except Exception as e: RNS.log(f"Error while running external link packet callback on {self}: {e}", RNS.LOG_ERROR)
except Exception as e: RNS.log(f"Error in {self} callback handler: {e}", RNS.LOG_ERROR)
finally: self.callback_thread = None
def load_peer(self, peer_pub_bytes, peer_sig_pub_bytes):
self.peer_pub_bytes = peer_pub_bytes
self.peer_pub = X25519PublicKey.from_public_bytes(self.peer_pub_bytes)
@ -1043,9 +1068,17 @@ class Link:
self.__update_phy_stats(packet, query_shared=True)
if self.callbacks.packet != None:
thread = threading.Thread(target=self.callbacks.packet, args=(plaintext, packet))
thread.daemon = True
thread.start()
with self.callback_condition:
if not self.callback_thread:
self.callback_thread = threading.Thread(target=self.__callback_loop, daemon=True)
self.callback_thread.start()
if len(self.pending_packets) >= self.pending_packets.maxlen:
RNS.log(f"External packet callback not consuming packets fast enough, dropped inbound packet on {self}", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
self.inbound_dropped += 1
self.pending_packets.append((plaintext, packet))
self.callback_condition.notify()
if self.destination.proof_strategy == RNS.Destination.PROVE_ALL:
packet.prove()