Skip to content

Commit fc1da7d

Browse files
authored
fix(flags): Add a shorter configurable timeout for flag requests (#120)
1 parent cba6e86 commit fc1da7d

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.5.0 - 2024-02-29
2+
3+
1. - Adds a new `feature_flags_request_timeout_seconds` timeout parameter for feature flags which defaults to 3 seconds, updated from the default 10s for all other API calls.
4+
15
## 3.4.2 - 2024-02-20
26

37
1. Add `historical_migration` option for bulk migration to PostHog Cloud.

posthog/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
project_api_key = None # type: Optional[str]
1919
poll_interval = 30 # type: int
2020
disable_geoip = True # type: bool
21+
feature_flags_request_timeout_seconds = 3 # type: int
2122

2223
default_client = None # type: Optional[Client]
2324

@@ -452,6 +453,7 @@ def _proxy(method, *args, **kwargs):
452453
poll_interval=poll_interval,
453454
disabled=disabled,
454455
disable_geoip=disable_geoip,
456+
feature_flags_request_timeout_seconds=feature_flags_request_timeout_seconds,
455457
)
456458

457459
# always set incase user changes it

posthog/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def __init__(
5050
disabled=False,
5151
disable_geoip=True,
5252
historical_migration=False,
53+
feature_flags_request_timeout_seconds=3,
5354
):
5455
self.queue = queue.Queue(max_queue_size)
5556

@@ -70,6 +71,7 @@ def __init__(
7071
self.group_type_mapping = None
7172
self.cohorts = None
7273
self.poll_interval = poll_interval
74+
self.feature_flags_request_timeout_seconds = feature_flags_request_timeout_seconds
7375
self.poller = None
7476
self.distinct_ids_feature_flags_reported = SizeLimitedDict(MAX_DICT_SIZE, set)
7577
self.disabled = disabled
@@ -164,7 +166,7 @@ def get_decide(self, distinct_id, groups=None, person_properties=None, group_pro
164166
"group_properties": group_properties,
165167
"disable_geoip": disable_geoip,
166168
}
167-
resp_data = decide(self.api_key, self.host, timeout=10, **request_data)
169+
resp_data = decide(self.api_key, self.host, timeout=self.feature_flags_request_timeout_seconds, **request_data)
168170

169171
return resp_data
170172

posthog/test/test_client.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def test_basic_capture_with_feature_flags_returns_active_only(self, patch_decide
315315
patch_decide.assert_called_with(
316316
"random_key",
317317
"https://us-api.i.posthog.com",
318-
timeout=10,
318+
timeout=3,
319319
distinct_id="distinct_id",
320320
groups={},
321321
person_properties=None,
@@ -335,6 +335,7 @@ def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(se
335335
on_error=self.set_fail,
336336
personal_api_key=FAKE_TEST_API_KEY,
337337
disable_geoip=True,
338+
feature_flags_request_timeout_seconds=12,
338339
)
339340
success, msg = client.capture("distinct_id", "python test event", send_feature_flags=True, disable_geoip=False)
340341
client.flush()
@@ -356,7 +357,7 @@ def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(se
356357
patch_decide.assert_called_with(
357358
"random_key",
358359
"https://us-api.i.posthog.com",
359-
timeout=10,
360+
timeout=12,
360361
distinct_id="distinct_id",
361362
groups={},
362363
person_properties=None,
@@ -784,7 +785,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
784785
patch_decide.assert_called_with(
785786
"random_key",
786787
"https://us-api.i.posthog.com",
787-
timeout=10,
788+
timeout=3,
788789
distinct_id="some_id",
789790
groups={},
790791
person_properties={"distinct_id": "some_id"},
@@ -796,7 +797,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
796797
patch_decide.assert_called_with(
797798
"random_key",
798799
"https://us-api.i.posthog.com",
799-
timeout=10,
800+
timeout=3,
800801
distinct_id="feature_enabled_distinct_id",
801802
groups={},
802803
person_properties={"distinct_id": "feature_enabled_distinct_id"},
@@ -808,7 +809,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
808809
patch_decide.assert_called_with(
809810
"random_key",
810811
"https://us-api.i.posthog.com",
811-
timeout=10,
812+
timeout=3,
812813
distinct_id="all_flags_payloads_id",
813814
groups={},
814815
person_properties={"distinct_id": "all_flags_payloads_id"},
@@ -844,7 +845,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
844845
patch_decide.assert_called_with(
845846
"random_key",
846847
"http://app2.posthog.com",
847-
timeout=10,
848+
timeout=3,
848849
distinct_id="some_id",
849850
groups={"company": "id:5", "instance": "app.posthog.com"},
850851
person_properties={"distinct_id": "some_id", "x1": "y1"},
@@ -870,7 +871,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
870871
patch_decide.assert_called_with(
871872
"random_key",
872873
"http://app2.posthog.com",
873-
timeout=10,
874+
timeout=3,
874875
distinct_id="some_id",
875876
groups={"company": "id:5", "instance": "app.posthog.com"},
876877
person_properties={"distinct_id": "override"},
@@ -887,7 +888,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
887888
patch_decide.assert_called_with(
888889
"random_key",
889890
"http://app2.posthog.com",
890-
timeout=10,
891+
timeout=3,
891892
distinct_id="some_id",
892893
groups={},
893894
person_properties={"distinct_id": "some_id"},

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.4.2"
1+
VERSION = "3.5.0"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)