Skip to content

Commit 9931dad

Browse files
authored
Add timeout to IA availability requests to avoid localhost hanging (#11247)
1 parent 2bf54df commit 9931dad

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

openlibrary/core/lending.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
import datetime
66
import logging
7+
import os
78
import time
89
import uuid
910
from typing import TYPE_CHECKING, Literal, TypedDict, cast
1011

1112
import eventer
13+
import httpx
1214
import requests
1315
import web
1416
from simplejson.errors import JSONDecodeError
@@ -202,10 +204,22 @@ def get_groundtruth_availability(ocaid, s3_keys=None):
202204
including 1-hour borrows"""
203205
params = '?action=availability&identifier=' + ocaid
204206
url = S3_LOAN_URL % config_bookreader_host
207+
timeout = 2 if os.getenv('LOCAL_DEV') else 5
205208
try:
206-
response = requests.post(url + params, data=s3_keys)
209+
response = httpx.post(url + params, data=s3_keys, timeout=timeout)
207210
response.raise_for_status()
208-
except requests.HTTPError:
211+
except httpx.TimeoutException:
212+
if os.getenv('LOCAL_DEV'):
213+
logger.warning(
214+
"Availability request timed out in LOCAL_DEV environment. Returning empty dictionary."
215+
)
216+
return {}
217+
else:
218+
logger.error(
219+
"Availability request timed out in non-LOCAL_DEV environment. Re-raising the exception."
220+
)
221+
raise # Re-raise the timeout exception if not in LOCAL_DEV
222+
except httpx.HTTPError:
209223
pass # TODO: Handle unexpected responses from the availability server.
210224
try:
211225
data = response.json().get('lending_status', {})

0 commit comments

Comments
 (0)