Skip to content

Commit 7827f66

Browse files
committed
Add exponential backoff to HTTPQueue put (#18013)
(cherry picked from commit bb47517)
1 parent d958639 commit 7827f66

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

requirements/app/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ inquirer >=2.10.0, <=3.1.3
1313
psutil <5.9.5
1414
click <=8.1.3
1515
python-multipart>=0.0.5, <=0.0.6
16+
backoff >=2.2.1, <2.3.0
1617

1718
fastapi >=0.92.0, <0.100.0
1819
starlette # https://fastapi.tiangolo.com/deployment/versions/#about-starlette

src/lightning/app/core/queues.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from typing import Any, Optional, Tuple
2424
from urllib.parse import urljoin
2525

26+
import backoff
2627
import requests
2728
from requests.exceptions import ConnectionError, ConnectTimeout, ReadTimeout
2829

@@ -431,6 +432,7 @@ def _get(self) -> Any:
431432
# we consider the queue is empty to avoid failing the app.
432433
raise queue.Empty
433434

435+
@backoff.on_exception(backoff.expo, (RuntimeError, requests.exceptions.HTTPError))
434436
def put(self, item: Any) -> None:
435437
if not self.app_id:
436438
raise ValueError(f"The Lightning App ID couldn't be extracted from the queue name: {self.name}")

tests/tests_app/core/test_queues.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,21 @@ def test_http_queue_get(self, monkeypatch):
217217

218218
def test_unreachable_queue(monkeypatch):
219219
monkeypatch.setattr(queues, "HTTP_QUEUE_TOKEN", "test-token")
220+
220221
test_queue = QueuingSystem.HTTP.get_queue(queue_name="test_http_queue")
221222

222-
resp = mock.MagicMock()
223-
resp.status_code = 204
223+
resp1 = mock.MagicMock()
224+
resp1.status_code = 204
225+
226+
resp2 = mock.MagicMock()
227+
resp2.status_code = 201
224228

225229
test_queue.client = mock.MagicMock()
226-
test_queue.client.post.return_value = resp
230+
test_queue.client.post = mock.Mock(side_effect=[resp1, resp1, resp2])
227231

228232
with pytest.raises(queue.Empty):
229233
test_queue._get()
234+
235+
# Test backoff on queue.put
236+
test_queue.put("foo")
237+
assert test_queue.client.post.call_count == 3

0 commit comments

Comments
 (0)