25
25
HTTPStatusError ,
26
26
Limits ,
27
27
ReadError ,
28
+ ReadTimeout ,
28
29
RemoteProtocolError ,
29
30
RequestError ,
30
31
Response ,
55
56
WeaviateConnectionError ,
56
57
WeaviateGRPCUnavailableError ,
57
58
WeaviateStartUpError ,
59
+ WeaviateTimeoutError ,
58
60
)
59
61
from weaviate .proto .v1 import weaviate_pb2_grpc
60
62
from weaviate .util import (
@@ -219,12 +221,6 @@ def __make_mounts(self) -> Dict[str, AsyncHTTPTransport]:
219
221
def __make_async_client (self ) -> AsyncClient :
220
222
return AsyncClient (
221
223
headers = self ._headers ,
222
- timeout = Timeout (
223
- None ,
224
- connect = self .timeout_config .init ,
225
- read = self .timeout_config .query ,
226
- write = self .timeout_config .insert ,
227
- ),
228
224
mounts = self .__make_mounts (),
229
225
)
230
226
@@ -409,12 +405,36 @@ def __get_latest_headers(self) -> Dict[str, str]:
409
405
copied_headers .update ({"authorization" : self .get_current_bearer_token ()})
410
406
return copied_headers
411
407
408
+ def __get_timeout (
409
+ self , method : Literal ["DELETE" , "GET" , "HEAD" , "PATCH" , "POST" , "PUT" ], is_gql_query : bool
410
+ ) -> Timeout :
411
+ """
412
+ In this way, the client waits the `httpx` default of 5s when connecting to a socket (connect), writing chunks (write), and
413
+ acquiring a connection from the pool (pool), but a custom amount as specified for reading the response (read).
414
+
415
+ From the PoV of the user, a request is considered to be timed out if no response is received within the specified time.
416
+ They specify the times depending on how they expect Weaviate to behave. For example, a query might take longer than an insert or vice versa.
417
+
418
+ https://www.python-httpx.org/advanced/timeouts/
419
+ """
420
+ timeout = None
421
+ if method == "DELETE" or method == "PATCH" or method == "PUT" :
422
+ timeout = self .timeout_config .insert
423
+ elif method == "GET" or method == "HEAD" :
424
+ timeout = self .timeout_config .query
425
+ elif method == "POST" and is_gql_query :
426
+ timeout = self .timeout_config .query
427
+ elif method == "POST" and not is_gql_query :
428
+ timeout = self .timeout_config .insert
429
+ return Timeout (timeout = 5.0 , read = timeout )
430
+
412
431
async def __send (
413
432
self ,
414
433
method : Literal ["DELETE" , "GET" , "HEAD" , "PATCH" , "POST" , "PUT" ],
415
434
url : str ,
416
435
error_msg : str ,
417
436
status_codes : Optional [_ExpectedStatusCodes ],
437
+ is_gql_query : bool = False ,
418
438
weaviate_object : Optional [JSONPayload ] = None ,
419
439
params : Optional [Dict [str , Any ]] = None ,
420
440
) -> Response :
@@ -430,6 +450,7 @@ async def __send(
430
450
json = weaviate_object ,
431
451
params = params ,
432
452
headers = self .__get_latest_headers (),
453
+ timeout = self .__get_timeout (method , is_gql_query ),
433
454
)
434
455
res = await self ._client .send (req )
435
456
if status_codes is not None and res .status_code not in status_codes .ok :
@@ -439,6 +460,8 @@ async def __send(
439
460
raise WeaviateClosedClientError () from e
440
461
except ConnectError as conn_err :
441
462
raise WeaviateConnectionError (error_msg ) from conn_err
463
+ except ReadTimeout as read_err :
464
+ raise WeaviateTimeoutError (error_msg ) from read_err
442
465
except Exception as e :
443
466
raise e
444
467
@@ -483,6 +506,7 @@ async def post(
483
506
params : Optional [Dict [str , Any ]] = None ,
484
507
error_msg : str = "" ,
485
508
status_codes : Optional [_ExpectedStatusCodes ] = None ,
509
+ is_gql_query : bool = False ,
486
510
) -> Response :
487
511
return await self .__send (
488
512
"POST" ,
@@ -491,6 +515,7 @@ async def post(
491
515
params = params ,
492
516
error_msg = error_msg ,
493
517
status_codes = status_codes ,
518
+ is_gql_query = is_gql_query ,
494
519
)
495
520
496
521
async def put (
0 commit comments