@@ -23,15 +23,17 @@ class AsyncMemFuse:
23
23
# Class variable to track all instances
24
24
_instances = set ()
25
25
26
- def __init__ (self , base_url : str = "http://localhost:8000" , api_key : Optional [str ] = None ):
26
+ def __init__ (self , base_url : str = "http://localhost:8000" , api_key : Optional [str ] = None , timeout : int = 10 ):
27
27
"""Initialize the MemFuse client.
28
28
29
29
Args:
30
30
base_url: URL of the MemFuse server API
31
31
api_key: API key for authentication (optional for local usage)
32
+ timeout: Request timeout in seconds (default: 10)
32
33
"""
33
34
self .base_url = base_url .rstrip ("/" )
34
35
self .api_key = api_key or os .environ .get ("MEMFUSE_API_KEY" )
36
+ self .timeout = timeout
35
37
self .session = None
36
38
37
39
# Initialize ASYNC API clients using the classes from .api
@@ -69,7 +71,7 @@ async def _check_server_health(self) -> bool:
69
71
await self ._ensure_session ()
70
72
try :
71
73
url = f"{ self .base_url } /api/v1/health"
72
- async with self .session .get (url , timeout = 10 ) as response :
74
+ async with self .session .get (url , timeout = self . timeout ) as response :
73
75
if response .status == 200 :
74
76
return True
75
77
return False
@@ -108,7 +110,7 @@ async def _request(
108
110
109
111
url = f"{ self .base_url } { endpoint } "
110
112
111
- async with getattr (self .session , method .lower ())(url , json = data ) as response :
113
+ async with getattr (self .session , method .lower ())(url , json = data , timeout = self . timeout ) as response :
112
114
response_data = await response .json ()
113
115
if response .status >= 400 :
114
116
error_message = response_data .get ("message" , "Unknown error" )
@@ -257,15 +259,17 @@ async def _thread_safe_coro_runner(self, coro):
257
259
class MemFuse :
258
260
"""Synchronous MemFuse client for communicating with the MemFuse server."""
259
261
260
- def __init__ (self , base_url : str = "http://localhost:8000" , api_key : Optional [str ] = None ):
262
+ def __init__ (self , base_url : str = "http://localhost:8000" , api_key : Optional [str ] = None , timeout : int = 10 ):
261
263
"""Initialize the synchronous MemFuse client.
262
264
263
265
Args:
264
266
base_url: URL of the MemFuse server API
265
267
api_key: API key for authentication (optional for local usage)
268
+ timeout: Request timeout in seconds (default: 10)
266
269
"""
267
270
self .base_url = base_url .rstrip ("/" )
268
271
self .api_key = api_key or os .environ .get ("MEMFUSE_API_KEY" )
272
+ self .timeout = timeout
269
273
self .sync_session = None # requests session for sync requests
270
274
271
275
# Initialize API clients using the classes from .api
@@ -300,7 +304,7 @@ def _check_server_health_sync(self) -> bool:
300
304
self ._ensure_sync_session ()
301
305
try :
302
306
url = f"{ self .base_url } /api/v1/health"
303
- response = self .sync_session .get (url , timeout = 10 )
307
+ response = self .sync_session .get (url , timeout = self . timeout )
304
308
return response .status_code == 200
305
309
except Exception :
306
310
return False
@@ -336,7 +340,7 @@ def _request_sync(
336
340
337
341
url = f"{ self .base_url } { endpoint } "
338
342
339
- response = getattr (self .sync_session , method .lower ())(url , json = data , timeout = 10 )
343
+ response = getattr (self .sync_session , method .lower ())(url , json = data , timeout = self . timeout )
340
344
response_data = response .json ()
341
345
if response .status_code >= 400 :
342
346
error_message = response_data .get ("message" , "Unknown error" )
0 commit comments