Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: measure only last request time in RPC metrics after 429 error
  • Loading branch information
smypmsa committed Feb 22, 2025
commit 115ed3c8d5c8cc44b241b8a169c79ff5f734a3ee
43 changes: 27 additions & 16 deletions common/metric_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,42 @@ async def fetch_data(self) -> float:
endpoint = self.config.endpoints.get_endpoint(self.method)

async with aiohttp.ClientSession() as session:
start_time = time.monotonic()
async with await self._send_request(session, endpoint, 0) as response: # type: ignore
response_time = 0.0 # Do not include retried requests after 429 error
response = None

for retry_count in range(MAX_RETRIES):
start_time = time.monotonic()
response = await self._send_request(session, endpoint, retry_count)
response_time = time.monotonic() - start_time

if response.status == 429 and retry_count < MAX_RETRIES - 1:
wait_time = int(response.headers.get("Retry-After", 15))
await response.release() # Release before retry
await asyncio.sleep(wait_time)
continue

break

if not response:
raise ValueError("No response received")

try:
if response.status != 200:
raise ValueError(f"Status code: {response.status}")

json_response = await response.json()
if "error" in json_response:
raise ValueError(f"JSON-RPC error: {json_response['error']}")
return time.monotonic() - start_time

return response_time
finally:
await response.release()

async def _send_request(
self, session: aiohttp.ClientSession, endpoint: str, retry_count: int
) -> aiohttp.ClientResponse:
"""Send the request and handle rate limiting with retries."""
if retry_count >= MAX_RETRIES:
raise ValueError("Status code: 429. Max retries exceeded")

response = await session.post(
"""Send the request without retry logic."""
return await session.post(
endpoint,
headers={
"Accept": "application/json",
Expand All @@ -201,14 +220,6 @@ async def _send_request(
timeout=self.config.timeout, # type: ignore
)

if response.status == 429 and retry_count < MAX_RETRIES:
wait_time = int(response.headers.get("Retry-After", 15))
await response.release()
await asyncio.sleep(wait_time)
return await self._send_request(session, endpoint, retry_count + 1)

return response

def process_data(self, value: float) -> float:
"""Process raw latency measurement."""
return value