From 1cf712cfac58a1e3beef8e108c0f2bd76487539e Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 20 Aug 2023 16:22:46 -0400 Subject: [PATCH] Add `Arbiter.delete_sockaddr()` to remove addrs Since stale addrs can be leaked where the actor transport server task crashes but doesn't (successfully) unregister from the registrar, we need a remote way to remove such entries; hence this new (registrar) method. To implement this make use of the `bidict` lib for the `._registry` table thus making it super simple to do reverse uuid lookups from an input socket-address. --- setup.py | 3 +++ tractor/_runtime.py | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index d26deb9..4cefe67 100755 --- a/setup.py +++ b/setup.py @@ -41,6 +41,9 @@ setup( ], install_requires=[ + # discovery subsys + 'bidict', + # trio related # proper range spec: # https://packaging.python.org/en/latest/discussions/install-requires-vs-requirements/#id5 diff --git a/tractor/_runtime.py b/tractor/_runtime.py index c9e4bfe..67bb0c5 100644 --- a/tractor/_runtime.py +++ b/tractor/_runtime.py @@ -40,6 +40,7 @@ from contextlib import ExitStack import warnings from async_generator import aclosing +from bidict import bidict from exceptiongroup import BaseExceptionGroup import trio # type: ignore from trio_typing import TaskStatus @@ -1774,10 +1775,10 @@ class Arbiter(Actor): def __init__(self, *args, **kwargs) -> None: - self._registry: dict[ + self._registry: bidict[ tuple[str, str], tuple[str, int], - ] = {} + ] = bidict({}) self._waiters: dict[ str, # either an event to sync to receiving an actor uid (which @@ -1871,3 +1872,15 @@ class Arbiter(Actor): entry: tuple = self._registry.pop(uid, None) if entry is None: log.warning(f'Request to de-register {uid} failed?') + + + async def delete_sockaddr( + self, + sockaddr: tuple[str, int], + + ) -> tuple[str, str]: + uid: tuple = self._registry.inverse.pop(sockaddr) + log.warning( + f'Deleting registry entry for {sockaddr}@{uid}!' + ) + return uid