Skip to content

Commit a558001

Browse files
committed
fix: make LinkedEvents API timeout configurable
The hardcoded 20-second timeout was causing ReadTimeout errors when the external LinkedEvents API experienced high load or network latency. Changes: - Add LINKED_EVENTS_API_TIMEOUT env variable (default: 60s) - Read timeout from config in LinkedEventsApiClient - Remove hardcoded CONNECTION_TIMEOUT constant Resolves Sentry issue 89742127. Refs: PT-1992
1 parent 26b1994 commit a558001

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

graphene_linked_events/rest_client.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33

44
class LinkedEventsApiClient(object):
5-
CONNECTION_TIMEOUT = 20
6-
75
def __init__(self, config) -> None:
86
self.root = config["ROOT"]
97
self.api_key = config["API_KEY"]
108
self.data_source = config["DATA_SOURCE"]
9+
self.timeout = config.get("TIMEOUT", 60)
1110
super().__init__()
1211

1312
def get_actions(self, resource=None):
@@ -34,13 +33,13 @@ def retrieve(self, resource, id, params=None, is_event_staff=False):
3433
params=formatted_params,
3534
headers=headers,
3635
cookies=cookies,
37-
timeout=self.CONNECTION_TIMEOUT,
36+
timeout=self.timeout,
3837
)
3938
return requests.request(
4039
actions["retrieve"]["method"],
4140
actions["retrieve"]["url"].format(id),
4241
params=formatted_params,
43-
timeout=self.CONNECTION_TIMEOUT,
42+
timeout=self.timeout,
4443
)
4544

4645
def list(self, resource, filter_list=None, is_event_staff=False):
@@ -61,13 +60,13 @@ def list(self, resource, filter_list=None, is_event_staff=False):
6160
params=filter_params,
6261
headers=headers,
6362
cookies=cookies,
64-
timeout=self.CONNECTION_TIMEOUT,
63+
timeout=self.timeout,
6564
)
6665
return requests.request(
6766
actions["list"]["method"],
6867
actions["list"]["url"],
6968
params=filter_params,
70-
timeout=self.CONNECTION_TIMEOUT,
69+
timeout=self.timeout,
7170
)
7271

7372
def create(self, resource, body):
@@ -78,7 +77,7 @@ def create(self, resource, body):
7877
actions["create"]["url"],
7978
data=body,
8079
headers=headers,
81-
timeout=self.CONNECTION_TIMEOUT,
80+
timeout=self.timeout,
8281
)
8382

8483
def update(self, resource, id, body):
@@ -89,7 +88,7 @@ def update(self, resource, id, body):
8988
actions["update"]["url"].format(id),
9089
data=body,
9190
headers=headers,
92-
timeout=self.CONNECTION_TIMEOUT,
91+
timeout=self.timeout,
9392
)
9493

9594
def delete(self, resource, id):
@@ -99,7 +98,7 @@ def delete(self, resource, id):
9998
actions["delete"]["method"],
10099
actions["delete"]["url"].format(id),
101100
headers=headers,
102-
timeout=self.CONNECTION_TIMEOUT,
101+
timeout=self.timeout,
103102
)
104103

105104
# Special action to full-text search generic resources
@@ -110,7 +109,7 @@ def search(self, params):
110109
action["method"],
111110
action["url"],
112111
params=search_params,
113-
timeout=self.CONNECTION_TIMEOUT,
112+
timeout=self.timeout,
114113
)
115114
return response
116115

@@ -123,7 +122,7 @@ def upload(self, resource, body, files):
123122
data=body,
124123
files=files,
125124
headers=headers,
126-
timeout=self.CONNECTION_TIMEOUT,
125+
timeout=self.timeout,
127126
)
128127

129128
@staticmethod

palvelutarjotin/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
ENABLE_GRAPHIQL=(bool, False),
6969
LINKED_EVENTS_API_ROOT=(str, "https://api.hel.fi/linkedevents/v1/"),
7070
LINKED_EVENTS_API_KEY=(str, ""),
71+
LINKED_EVENTS_API_TIMEOUT=(int, 60),
7172
LINKED_EVENTS_DATA_SOURCE=(str, "palvelutarjotin"),
7273
SERVICEMAP_API_ROOT=(str, "https://www.hel.fi/palvelukarttaws/rest/v4/unit/"),
7374
NOTIFICATION_SERVICE_SMS_ENABLED=(bool, True),
@@ -432,6 +433,7 @@ def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
432433
"ROOT": env.str("LINKED_EVENTS_API_ROOT"),
433434
"API_KEY": env.str("LINKED_EVENTS_API_KEY"),
434435
"DATA_SOURCE": env.str("LINKED_EVENTS_DATA_SOURCE"),
436+
"TIMEOUT": env.int("LINKED_EVENTS_API_TIMEOUT"),
435437
}
436438

437439
SERVICEMAP_API_CONFIG = {"ROOT": env.str("SERVICEMAP_API_ROOT")}

0 commit comments

Comments
 (0)