Skip to content

Commit 41972f1

Browse files
authored
Fix timeouts (#27)
1 parent 3ee6075 commit 41972f1

File tree

6 files changed

+12
-11
lines changed

6 files changed

+12
-11
lines changed

src/collectors/cardano.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async def _blockHeight(self, websocket):
2424
}
2525
}
2626
await websocket.send(json.dumps(payload))
27-
result = await websocket.recv()
27+
result = await asyncio.wait_for(websocket.recv(), timeout=cfg.response_timeout)
2828
return key_from_json_str(result, "result")
2929

3030
async def _probe(self) -> results:

src/collectors/conflux.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def __init__(self, rpc_metadata):
2323
async def _cfx_clientVersion(self, websocket):
2424
payload = {"jsonrpc": "2.0", "method": "cfx_clientVersion", "params": [], "id": 1}
2525
await websocket.send(json.dumps(payload))
26-
result = await websocket.recv()
26+
result = await asyncio.wait_for(websocket.recv(), timeout=cfg.response_timeout)
2727
return key_from_json_str(result, "result")
2828

2929
async def _cfx_epochNumber(self, websocket):
3030
payload = {"jsonrpc": "2.0", "method": "cfx_epochNumber", "params": [], "id": 1}
3131
await websocket.send(json.dumps(payload))
32-
result = await websocket.recv()
32+
result = await asyncio.wait_for(websocket.recv(), timeout=cfg.response_timeout)
3333
return hex_to_int(key_from_json_str(result, "result"))
3434

3535
async def _probe(self) -> results:
@@ -50,7 +50,7 @@ async def _probe(self) -> results:
5050
logger.error(
5151
f"Timed out while trying to establish websocket connection. Current open_timeout value in config: {cfg.open_timeout}.",
5252
url=self.stripped_url)
53-
results.record_health(self.url, False)
53+
results.record_health(self.url, False)
5454
except Exception as exc:
5555
results.record_health(self.url, False)
5656
logger.error(f"{exc}", url=self.stripped_url)

src/collectors/evm.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from settings import logger, cfg
22
from helpers import strip_url, generate_labels_from_metadata, hex_to_int, key_from_json_str
33
from metrics_processor import results
4-
54
from collectors.ws import subscription, fetch_latency
5+
import json
66
import websockets
77
import asyncio
8-
import json
98

109

1110
class evm_collector():
@@ -23,13 +22,13 @@ def __init__(self, rpc_metadata):
2322
async def _web3_clientVersion(self, websocket):
2423
payload = {"jsonrpc": "2.0", "method": "web3_clientVersion", "params": [], "id": self.chain_id}
2524
await websocket.send(json.dumps(payload))
26-
result = await websocket.recv()
25+
result = await asyncio.wait_for(websocket.recv(), timeout=cfg.response_timeout)
2726
return key_from_json_str(result, "result")
2827

2928
async def _eth_blockNumber(self, websocket):
3029
payload = {"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": self.chain_id}
3130
await websocket.send(json.dumps(payload))
32-
result = await websocket.recv()
31+
result = await asyncio.wait_for(websocket.recv(), timeout=cfg.response_timeout)
3332
return hex_to_int(key_from_json_str(result, "result"))
3433

3534
async def _probe(self) -> results:
@@ -49,7 +48,7 @@ async def _probe(self) -> results:
4948
results.record_disconnects(self.url, self.sub.disconnects)
5049
except asyncio.exceptions.TimeoutError:
5150
logger.error(
52-
f"Timed out while trying to establish websocket connection. Current open_timeout value in config: {cfg.open_timeout}.",
51+
f"Timed out while trying to establish websocket connection. Current response_timeout value in config: {cfg.response_timeout}.",
5352
url=self.stripped_url)
5453
results.record_health(self.url, False)
5554
except Exception as exc:

src/collectors/https.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from settings import logger
33
from helpers import strip_url
44
from time import perf_counter
5+
from settings import cfg
56

67

78
class https_connection():
@@ -11,7 +12,7 @@ def __init__(self, url):
1112

1213
def is_connected_post_check(self, payload):
1314
try:
14-
response = requests.post(self.url, json=payload)
15+
response = requests.post(self.url, json=payload, timeout=cfg.response_timeout)
1516
response.raise_for_status()
1617
except (IOError, requests.HTTPError) as exc:
1718
logger.error(exc, url=self.stripped_url)

src/collectors/ws.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ async def _subscribe(self):
4848
async def fetch_latency(websocket):
4949
start = perf_counter()
5050
pong = await websocket.ping()
51-
await pong
51+
await asyncio.wait_for(pong, timeout=cfg.response_timeout)
5252
return (perf_counter() - start) * 1000

src/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(self, config_file_path: str, validation_file_path: str):
4343
except KeyError:
4444
self.ping_timeout = 7
4545
logger.info(f"connection_parameters.ping_timeout not set, defaulting to {self.ping_timeout}")
46+
4647
def _populate_chain_id_metadata(self):
4748
# Conditionally add chain_id based on the colelctor type to each rpc item.
4849
if self.configuration['collector'] not in ['cardano', 'solana', 'bitcoin', 'doge', 'filecoin', 'starkware']:

0 commit comments

Comments
 (0)