Upgrade rns and remove hacks for previous version

This commit is contained in:
Aaron Heise 2023-02-18 07:44:24 -06:00
parent 1f31307307
commit acc217df90
4 changed files with 1 additions and 113 deletions

View file

@ -9,7 +9,7 @@ readme = "README.md"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.9" python = "^3.9"
docopt = "^0.6.2" docopt = "^0.6.2"
rns = "^0.4.8" rns = "^0.4.9"
tomli = "^2.0.1" tomli = "^2.0.1"
[tool.poetry.scripts] [tool.poetry.scripts]

View file

@ -1,67 +0,0 @@
import asyncio
import threading
import RNS
import logging
module_logger = logging.getLogger(__name__)
class SelfPruningAssociation:
def __init__(self, ttl: float, loop: asyncio.AbstractEventLoop):
self._log = module_logger.getChild(self.__class__.__name__)
self._ttl = ttl
self._associations = list()
self._lock = threading.RLock()
self._loop = loop
# self._log.debug("__init__")
def _schedule_prune(self, pair):
# self._log.debug(f"schedule prune {pair}")
def schedule_prune_inner(p):
# self._log.debug(f"schedule inner {p}")
self._loop.call_later(self._ttl, self._prune, p)
self._loop.call_soon_threadsafe(schedule_prune_inner, pair)
def _prune(self, pair: any):
# self._log.debug(f"prune {pair}")
with self._lock:
self._associations.remove(pair)
def get(self, key: any) -> any:
# self._log.debug(f"get {key}")
with self._lock:
pair = next(filter(lambda x: x[0] == key, self._associations), None)
if not pair:
return None
return pair[1]
def put(self, key: any, value: any):
# self._log.debug(f"put {key},{value}")
with self._lock:
pair = (key, value)
self._associations.append(pair)
self._schedule_prune(pair)
_request_to_link: SelfPruningAssociation = None
_link_handle_request_orig = RNS.Link.handle_request
def _link_handle_request(self, request_id, unpacked_request):
global _request_to_link
_request_to_link.put(request_id, self.link_id)
_link_handle_request_orig(self, request_id, unpacked_request)
def request_request_id_hack(new_api_reponse_generator, loop):
global _request_to_link
if _request_to_link is None:
RNS.Link.handle_request = _link_handle_request
_request_to_link = SelfPruningAssociation(1.0, loop)
def listen_request(path, data, request_id, remote_identity, requested_at):
link_id = _request_to_link.get(request_id)
return new_api_reponse_generator(path, data, request_id, link_id, remote_identity, requested_at)
return listen_request

View file

@ -45,7 +45,6 @@ import rnsh.exception as exception
import rnsh.process as process import rnsh.process as process
import rnsh.retry as retry import rnsh.retry as retry
import rnsh.rnslogging as rnslogging import rnsh.rnslogging as rnslogging
import rnsh.hacks as hacks
import rnsh.session as session import rnsh.session as session
import re import re
import contextlib import contextlib

View file

@ -1,44 +0,0 @@
import asyncio
import rnsh.hacks as hacks
import pytest
import time
import logging
logging.getLogger().setLevel(logging.DEBUG)
class FakeLink:
def __init__(self, link_id):
self.link_id = link_id
@pytest.mark.asyncio
async def test_pruning():
def listen_request(path, data, request_id, link_id, remote_identity, requested_at):
assert path == 1
assert data == 2
assert request_id == 3
assert remote_identity == 4
assert requested_at == 5
assert link_id == 6
return 7
lhr_called = 0
link = FakeLink(6)
def link_handle_request(self, request_id, unpacked_request):
nonlocal lhr_called
lhr_called += 1
old_func = hacks.request_request_id_hack(listen_request, asyncio.get_running_loop())
hacks._link_handle_request_orig = link_handle_request
hacks._link_handle_request(link, 3, None)
link_id = hacks._request_to_link.get(3)
assert link_id == link.link_id
result = old_func(1, 2, 3, 4, 5)
assert result == 7
link_id = hacks._request_to_link.get(3)
assert link_id == 6
await asyncio.sleep(1.5)
link_id = hacks._request_to_link.get(3)
assert link_id is None