From 01cfbbdd64d0261f6b9f6b0cee56c5f432cb60b2 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sat, 31 Mar 2018 12:02:22 -0400 Subject: [PATCH] Handle network outages Quote queries will hang indefinitely when the network goes down. Instead poll for network reestablishment such that roaming on wifi is supported and real-time feeds will resume once the network is back. --- piker/brokers/core.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/piker/brokers/core.py b/piker/brokers/core.py index 36b9cb3a..01a2c9eb 100644 --- a/piker/brokers/core.py +++ b/piker/brokers/core.py @@ -3,6 +3,8 @@ Core broker-daemon tasks and API. """ import time import inspect +from functools import partial +import socket from types import ModuleType from typing import AsyncContextManager @@ -44,6 +46,17 @@ async def quote(brokermod: ModuleType, tickers: [str]) -> dict: return results +async def wait_for_network(get_quotes, sleep=1): + """Wait until the network comes back up. + """ + while True: + try: + return await get_quotes() + except socket.gaierror: + log.warn(f"Network is down waiting for reestablishment...") + await trio.sleep(sleep) + + async def poll_tickers( client: 'Client', quoter: AsyncContextManager, @@ -64,7 +77,16 @@ async def poll_tickers( async with quoter(client, tickers) as get_quotes: while True: # use an event here to trigger exit? prequote_start = time.time() - quotes = await get_quotes(tickers) + + with trio.move_on_after(3) as cancel_scope: + quotes = await get_quotes(tickers) + + cancelled = cancel_scope.cancelled_caught + if cancelled: + log.warn("Quote query timed out after 3 seconds, retrying...") + # handle network outages by idling until response is received + quotes = await wait_for_network(partial(get_quotes, tickers)) + postquote_start = time.time() payload = {} for symbol, quote in quotes.items():