Skip to content

Commit 5d9b8e8

Browse files
committed
feat: Add disable_connection_reuse config method
Disables connection pooling
1 parent 1572580 commit 5d9b8e8

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

posthog/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
RequiresServerEvaluation as RequiresServerEvaluation,
2424
)
2525
from posthog.request import (
26+
disable_connection_reuse as disable_connection_reuse,
2627
enable_keep_alive as enable_keep_alive,
2728
set_socket_options as set_socket_options,
2829
SocketOptions as SocketOptions,

posthog/request.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ def _build_session(socket_options: Optional[SocketOptions] = None) -> requests.S
8585

8686

8787
_session = _build_session()
88+
_socket_options: Optional[SocketOptions] = None
89+
_pooling_enabled = True
90+
91+
92+
def _get_session() -> requests.Session:
93+
if _pooling_enabled:
94+
return _session
95+
return _build_session(_socket_options)
8896

8997

9098
def set_socket_options(socket_options: Optional[SocketOptions]) -> None:
@@ -95,7 +103,8 @@ def set_socket_options(socket_options: Optional[SocketOptions]) -> None:
95103
from posthog import set_socket_options
96104
set_socket_options([(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)])
97105
"""
98-
global _session
106+
global _session, _socket_options
107+
_socket_options = socket_options
99108
_session = _build_session(socket_options)
100109

101110

@@ -104,6 +113,12 @@ def enable_keep_alive() -> None:
104113
set_socket_options(KEEP_ALIVE_SOCKET_OPTIONS)
105114

106115

116+
def disable_connection_reuse() -> None:
117+
"""Disable connection reuse, creating a fresh connection for each request."""
118+
global _pooling_enabled
119+
_pooling_enabled = False
120+
121+
107122
US_INGESTION_ENDPOINT = "https://us.i.posthog.com"
108123
EU_INGESTION_ENDPOINT = "https://eu.i.posthog.com"
109124
DEFAULT_HOST = US_INGESTION_ENDPOINT
@@ -148,7 +163,7 @@ def post(
148163
gz.write(data.encode("utf-8"))
149164
data = buf.getvalue()
150165

151-
res = _session.post(url, data=data, headers=headers, timeout=timeout)
166+
res = _get_session().post(url, data=data, headers=headers, timeout=timeout)
152167

153168
if res.status_code == 200:
154169
log.debug("data uploaded successfully")
@@ -263,7 +278,7 @@ def get(
263278
if etag:
264279
headers["If-None-Match"] = etag
265280

266-
res = _session.get(full_url, headers=headers, timeout=timeout)
281+
res = _get_session().get(full_url, headers=headers, timeout=timeout)
267282

268283
masked_url = _mask_tokens_in_url(full_url)
269284

posthog/test/test_request.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77
import requests
88

9+
import posthog.request as request_module
910
from posthog.request import (
1011
APIError,
1112
DatetimeSerializer,
@@ -16,6 +17,7 @@
1617
batch_post,
1718
decide,
1819
determine_server_host,
20+
disable_connection_reuse,
1921
enable_keep_alive,
2022
get,
2123
set_socket_options,
@@ -370,3 +372,13 @@ def test_set_socket_options_clears_with_none():
370372
assert adapter.socket_options is None
371373
finally:
372374
set_socket_options(None)
375+
376+
377+
def test_disable_connection_reuse_creates_fresh_sessions():
378+
try:
379+
disable_connection_reuse()
380+
session1 = request_module._get_session()
381+
session2 = request_module._get_session()
382+
assert session1 is not session2
383+
finally:
384+
request_module._pooling_enabled = True

0 commit comments

Comments
 (0)