# HG changeset patch # User drewp@bigasterisk.com # Date 2023-05-29 18:47:35 # Node ID 74b4acd3dde083dc57d1b4d861f7b776629932f7 # Parent f226f40a731d1b9c95de5c77ff0b54f0477cbca2 collector client retries a bit. not sure we want this. diff --git a/light9/collector/collector_client_asyncio.py b/light9/collector/collector_client_asyncio.py --- a/light9/collector/collector_client_asyncio.py +++ b/light9/collector/collector_client_asyncio.py @@ -1,23 +1,46 @@ +import logging +import tenacity from light9 import networking from light9.collector.collector_client import toCollectorJson from light9.effect.settings import DeviceSettings import aiohttp -class Sender: + +log = logging.getLogger('coll_client') + + +class _Sender: + def __init__(self): - pass#self.http_session = aiohttp.ClientSession() + self.reconnect() + + def reconnect(self): + self.http_session = aiohttp.ClientSession() async def send(self, client: str, session: str, settings: DeviceSettings): msg = toCollectorJson(client, session, settings).encode('utf8') - async with aiohttp.ClientSession() as ses: # i want to use the one from __init__! - async with ses.put( - networking.collector.path('attrs'), data=msg, timeout=1) as resp: + @tenacity.retry( + retry=tenacity.retry_if_exception_type(aiohttp.ServerTimeoutError), + reraise=True, + stop=tenacity.stop_after_attempt(3), + ) + async def put(): + + async with self.http_session.put(networking.collector.path('attrs'), data=msg, timeout=.2) as resp: if resp.status != 202: body = await resp.text() + self.reconnect() raise ValueError(f'collector returned {resp.status}: {body}') -_sender = Sender() + await put() + attempts = put.retry.statistics['attempt_number'] + if attempts!=1: + log.warning(f"{attempts=}") + + +_sender = _Sender() + async def sendToCollector(client: str, session: str, settings: DeviceSettings, useZmq=False): await _sender.send(client, session, settings) diff --git a/pdm.lock b/pdm.lock --- a/pdm.lock +++ b/pdm.lock @@ -829,6 +829,12 @@ version = "0.8.0" summary = "A list of Python Standard Libraries (2.6-7, 3.2-9)." [[package]] +name = "tenacity" +version = "8.2.2" +requires_python = ">=3.6" +summary = "Retry code until it succeeds" + +[[package]] name = "tomli" version = "2.0.1" requires_python = ">=3.7" @@ -1026,7 +1032,7 @@ dependencies = [ [metadata] lock_version = "4.1" -content_hash = "sha256:b143dcaeecae81f7c7c117ef8680f17f1e7ba3b43102cf94d5dd000299db60bc" +content_hash = "sha256:dc1401c032c754cf047170f72e28807ba5f94185bb51f54d2b038dbacf44ac64" [metadata.files] "aiohttp 3.8.4" = [ @@ -2215,6 +2221,10 @@ content_hash = "sha256:b143dcaeecae81f7c {url = "https://files.pythonhosted.org/packages/7a/b1/52f59dcf31ead2f0ceff8976288449608d912972b911f55dff712cef5719/stdlib_list-0.8.0-py3-none-any.whl", hash = "sha256:2ae0712a55b68f3fbbc9e58d6fa1b646a062188f49745b495f94d3310a9fdd3e"}, {url = "https://files.pythonhosted.org/packages/bb/01/237cea071fd305f162ebbe1db18a59b56b0b5fdff19533c9a1beea0bdee7/stdlib-list-0.8.0.tar.gz", hash = "sha256:a1e503719720d71e2ed70ed809b385c60cd3fb555ba7ec046b96360d30b16d9f"}, ] +"tenacity 8.2.2" = [ + {url = "https://files.pythonhosted.org/packages/d3/f0/6ccd8854f4421ce1f227caf3421d9be2979aa046939268c9300030c0d250/tenacity-8.2.2.tar.gz", hash = "sha256:43af037822bd0029025877f3b2d97cc4d7bb0c2991000a3d59d71517c5c969e0"}, + {url = "https://files.pythonhosted.org/packages/e7/b0/c23bd61e1b32c9b96fbca996c87784e196a812da8d621d8d04851f6c8181/tenacity-8.2.2-py3-none-any.whl", hash = "sha256:2f277afb21b851637e8f52e6a613ff08734c347dc19ade928e519d7d2d8569b0"}, +] "tomli 2.0.1" = [ {url = "https://files.pythonhosted.org/packages/97/75/10a9ebee3fd790d20926a90a2547f0bf78f371b2f13aa822c759680ca7b9/tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, {url = "https://files.pythonhosted.org/packages/c0/3f/d7af728f075fb08564c5949a9c95e44352e23dee646869fa104a3b2060a3/tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, diff --git a/pyproject.toml b/pyproject.toml --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ dependencies = [ "webcolors>=1.11.1", "scipy>=1.9.3", "braillegraph>=0.6", + "tenacity>=8.2.2", ] requires-python = ">=3.10"