Skip to content

Commit ff6c2a7

Browse files
authored
fix: add timeout to SSE client
add timeout to SSE client
2 parents 8ecf9d5 + f17ec58 commit ff6c2a7

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

growthbook/growthbook.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ def destroy(self) -> None:
120120

121121

122122
class SSEClient:
123-
def __init__(self, api_host, client_key, on_event, reconnect_delay=5, headers=None):
123+
def __init__(self, api_host, client_key, on_event, reconnect_delay=5, headers=None, timeout = 30):
124124
self.api_host = api_host
125125
self.client_key = client_key
126126

127127
self.on_event = on_event
128128
self.reconnect_delay = reconnect_delay
129+
self.timeout = timeout
129130

130131
self._sse_session = None
131132
self._sse_thread = None
@@ -173,7 +174,8 @@ async def _init_session(self):
173174

174175
while self.is_running:
175176
try:
176-
async with aiohttp.ClientSession(headers=self.headers) as session:
177+
async with aiohttp.ClientSession(headers=self.headers,
178+
timeout=aiohttp.ClientTimeout(total=self.timeout)) as session:
177179
self._sse_session = session
178180

179181
async with session.get(url) as response:
@@ -407,10 +409,10 @@ async def _fetch_features_async(
407409
return data
408410

409411

410-
def startAutoRefresh(self, api_host, client_key, cb):
412+
def startAutoRefresh(self, api_host, client_key, cb, streaming_timeout=30):
411413
if not client_key:
412414
raise ValueError("Must specify `client_key` to start features streaming")
413-
self.sse_client = self.sse_client or SSEClient(api_host=api_host, client_key=client_key, on_event=cb)
415+
self.sse_client = self.sse_client or SSEClient(api_host=api_host, client_key=client_key, on_event=cb, timeout=streaming_timeout)
414416
self.sse_client.connect()
415417

416418
def stopAutoRefresh(self):
@@ -443,6 +445,7 @@ def __init__(
443445
sticky_bucket_identifier_attributes: List[str] = None,
444446
savedGroups: dict = {},
445447
streaming: bool = False,
448+
streaming_timeout: int = 30,
446449
plugins: List = None,
447450
# Deprecated args
448451
trackingCallback=None,
@@ -471,6 +474,7 @@ def __init__(
471474
self._trackingCallback = on_experiment_viewed or trackingCallback
472475

473476
self._streaming = streaming
477+
self._streaming_timeout = streaming_timeout
474478

475479
# Deprecated args
476480
self._user = user
@@ -587,7 +591,8 @@ def startAutoRefresh(self):
587591
feature_repo.startAutoRefresh(
588592
api_host=self._api_host,
589593
client_key=self._client_key,
590-
cb=self._dispatch_sse_event
594+
cb=self._dispatch_sse_event,
595+
streaming_timeout=self._streaming_timeout
591596
)
592597

593598
def stopAutoRefresh(self):

0 commit comments

Comments
 (0)