Skip to content

Commit 2da21c9

Browse files
Avoid unnecessary retry sleeps after final page batch
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 5b01060 commit 2da21c9

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

hyperbrowser/client/polling.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,20 +201,23 @@ def collect_paginated_results(
201201
raise HyperbrowserTimeoutError(
202202
f"Timed out fetching paginated results for {operation_name} after {max_wait_seconds} seconds"
203203
)
204+
should_sleep = True
204205
try:
205206
page_response = get_next_page(current_page_batch + 1)
206207
on_page_success(page_response)
207208
current_page_batch = get_current_page_batch(page_response)
208209
total_page_batches = get_total_page_batches(page_response)
209210
failures = 0
210211
first_check = False
212+
should_sleep = current_page_batch < total_page_batches
211213
except Exception as exc:
212214
failures += 1
213215
if failures >= max_attempts:
214216
raise HyperbrowserError(
215217
f"Failed to fetch page batch {current_page_batch + 1} for {operation_name} after {max_attempts} attempts: {exc}"
216218
) from exc
217-
time.sleep(retry_delay_seconds)
219+
if should_sleep:
220+
time.sleep(retry_delay_seconds)
218221

219222

220223
async def collect_paginated_results_async(
@@ -244,20 +247,23 @@ async def collect_paginated_results_async(
244247
raise HyperbrowserTimeoutError(
245248
f"Timed out fetching paginated results for {operation_name} after {max_wait_seconds} seconds"
246249
)
250+
should_sleep = True
247251
try:
248252
page_response = await get_next_page(current_page_batch + 1)
249253
on_page_success(page_response)
250254
current_page_batch = get_current_page_batch(page_response)
251255
total_page_batches = get_total_page_batches(page_response)
252256
failures = 0
253257
first_check = False
258+
should_sleep = current_page_batch < total_page_batches
254259
except Exception as exc:
255260
failures += 1
256261
if failures >= max_attempts:
257262
raise HyperbrowserError(
258263
f"Failed to fetch page batch {current_page_batch + 1} for {operation_name} after {max_attempts} attempts: {exc}"
259264
) from exc
260-
await asyncio.sleep(retry_delay_seconds)
265+
if should_sleep:
266+
await asyncio.sleep(retry_delay_seconds)
261267

262268

263269
def wait_for_job_result(

tests/test_polling.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
import hyperbrowser.client.polling as polling_helpers
56
from hyperbrowser.client.polling import (
67
collect_paginated_results,
78
collect_paginated_results_async,
@@ -225,6 +226,55 @@ async def run() -> None:
225226
asyncio.run(run())
226227

227228

229+
def test_collect_paginated_results_does_not_sleep_after_last_page(monkeypatch):
230+
sleep_calls = []
231+
232+
monkeypatch.setattr(
233+
polling_helpers.time, "sleep", lambda delay: sleep_calls.append(delay)
234+
)
235+
236+
collect_paginated_results(
237+
operation_name="sync single-page",
238+
get_next_page=lambda page: {"current": 1, "total": 1, "items": ["a"]},
239+
get_current_page_batch=lambda response: response["current"],
240+
get_total_page_batches=lambda response: response["total"],
241+
on_page_success=lambda response: None,
242+
max_wait_seconds=1.0,
243+
max_attempts=2,
244+
retry_delay_seconds=0.5,
245+
)
246+
247+
assert sleep_calls == []
248+
249+
250+
def test_collect_paginated_results_async_does_not_sleep_after_last_page(monkeypatch):
251+
sleep_calls = []
252+
253+
async def fake_sleep(delay: float) -> None:
254+
sleep_calls.append(delay)
255+
256+
monkeypatch.setattr(polling_helpers.asyncio, "sleep", fake_sleep)
257+
258+
async def run() -> None:
259+
async def get_next_page(page: int) -> dict:
260+
return {"current": 1, "total": 1, "items": ["a"]}
261+
262+
await collect_paginated_results_async(
263+
operation_name="async single-page",
264+
get_next_page=get_next_page,
265+
get_current_page_batch=lambda response: response["current"],
266+
get_total_page_batches=lambda response: response["total"],
267+
on_page_success=lambda response: None,
268+
max_wait_seconds=1.0,
269+
max_attempts=2,
270+
retry_delay_seconds=0.5,
271+
)
272+
273+
asyncio.run(run())
274+
275+
assert sleep_calls == []
276+
277+
228278
def test_collect_paginated_results_raises_after_page_failures():
229279
with pytest.raises(HyperbrowserError, match="Failed to fetch page batch 1"):
230280
collect_paginated_results(

0 commit comments

Comments
 (0)