Skip to content

Commit

Permalink
fix(flags): Add a shorter configurable timeout for flag requests (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar authored Mar 4, 2024
1 parent cba6e86 commit fc1da7d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.5.0 - 2024-02-29

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.

## 3.4.2 - 2024-02-20

1. Add `historical_migration` option for bulk migration to PostHog Cloud.
Expand Down
2 changes: 2 additions & 0 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
project_api_key = None # type: Optional[str]
poll_interval = 30 # type: int
disable_geoip = True # type: bool
feature_flags_request_timeout_seconds = 3 # type: int

default_client = None # type: Optional[Client]

Expand Down Expand Up @@ -452,6 +453,7 @@ def _proxy(method, *args, **kwargs):
poll_interval=poll_interval,
disabled=disabled,
disable_geoip=disable_geoip,
feature_flags_request_timeout_seconds=feature_flags_request_timeout_seconds,
)

# always set incase user changes it
Expand Down
4 changes: 3 additions & 1 deletion posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(
disabled=False,
disable_geoip=True,
historical_migration=False,
feature_flags_request_timeout_seconds=3,
):
self.queue = queue.Queue(max_queue_size)

Expand All @@ -70,6 +71,7 @@ def __init__(
self.group_type_mapping = None
self.cohorts = None
self.poll_interval = poll_interval
self.feature_flags_request_timeout_seconds = feature_flags_request_timeout_seconds
self.poller = None
self.distinct_ids_feature_flags_reported = SizeLimitedDict(MAX_DICT_SIZE, set)
self.disabled = disabled
Expand Down Expand Up @@ -164,7 +166,7 @@ def get_decide(self, distinct_id, groups=None, person_properties=None, group_pro
"group_properties": group_properties,
"disable_geoip": disable_geoip,
}
resp_data = decide(self.api_key, self.host, timeout=10, **request_data)
resp_data = decide(self.api_key, self.host, timeout=self.feature_flags_request_timeout_seconds, **request_data)

return resp_data

Expand Down
17 changes: 9 additions & 8 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def test_basic_capture_with_feature_flags_returns_active_only(self, patch_decide
patch_decide.assert_called_with(
"random_key",
"https://us-api.i.posthog.com",
timeout=10,
timeout=3,
distinct_id="distinct_id",
groups={},
person_properties=None,
Expand All @@ -335,6 +335,7 @@ def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(se
on_error=self.set_fail,
personal_api_key=FAKE_TEST_API_KEY,
disable_geoip=True,
feature_flags_request_timeout_seconds=12,
)
success, msg = client.capture("distinct_id", "python test event", send_feature_flags=True, disable_geoip=False)
client.flush()
Expand All @@ -356,7 +357,7 @@ def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(se
patch_decide.assert_called_with(
"random_key",
"https://us-api.i.posthog.com",
timeout=10,
timeout=12,
distinct_id="distinct_id",
groups={},
person_properties=None,
Expand Down Expand Up @@ -784,7 +785,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
patch_decide.assert_called_with(
"random_key",
"https://us-api.i.posthog.com",
timeout=10,
timeout=3,
distinct_id="some_id",
groups={},
person_properties={"distinct_id": "some_id"},
Expand All @@ -796,7 +797,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
patch_decide.assert_called_with(
"random_key",
"https://us-api.i.posthog.com",
timeout=10,
timeout=3,
distinct_id="feature_enabled_distinct_id",
groups={},
person_properties={"distinct_id": "feature_enabled_distinct_id"},
Expand All @@ -808,7 +809,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
patch_decide.assert_called_with(
"random_key",
"https://us-api.i.posthog.com",
timeout=10,
timeout=3,
distinct_id="all_flags_payloads_id",
groups={},
person_properties={"distinct_id": "all_flags_payloads_id"},
Expand Down Expand Up @@ -844,7 +845,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
patch_decide.assert_called_with(
"random_key",
"http://app2.posthog.com",
timeout=10,
timeout=3,
distinct_id="some_id",
groups={"company": "id:5", "instance": "app.posthog.com"},
person_properties={"distinct_id": "some_id", "x1": "y1"},
Expand All @@ -870,7 +871,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
patch_decide.assert_called_with(
"random_key",
"http://app2.posthog.com",
timeout=10,
timeout=3,
distinct_id="some_id",
groups={"company": "id:5", "instance": "app.posthog.com"},
person_properties={"distinct_id": "override"},
Expand All @@ -887,7 +888,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
patch_decide.assert_called_with(
"random_key",
"http://app2.posthog.com",
timeout=10,
timeout=3,
distinct_id="some_id",
groups={},
person_properties={"distinct_id": "some_id"},
Expand Down
2 changes: 1 addition & 1 deletion posthog/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "3.4.2"
VERSION = "3.5.0"

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

0 comments on commit fc1da7d

Please sign in to comment.