@@ -20,48 +20,52 @@ def __init__(self, client: 'RPCClient', method_name: str, *args, **kwargs):
2020 self .args = args
2121 self .kwargs = kwargs
2222
23+ def _prepare_and_call (self , timeout : Optional [float ], need_response : bool ,
24+ mode : str , call_method : str ) -> Any :
25+ """Common method to prepare RPC params and make the call.
26+
27+ Args:
28+ timeout: Timeout for the RPC call
29+ need_response: Whether a response is expected
30+ mode: The RPC mode ("sync", "async", "future")
31+ call_method: The method name to call on the client
32+
33+ Returns:
34+ The result of the client method call
35+ """
36+ rpc_params = RPCParams (timeout = timeout ,
37+ need_response = need_response ,
38+ mode = mode )
39+ self .kwargs ["__rpc_params" ] = rpc_params
40+ client_method = getattr (self .client , call_method )
41+ return client_method (self .method_name , * self .args , ** self .kwargs )
42+
2343 def remote (self ,
2444 timeout : Optional [float ] = None ,
2545 need_response : bool = True ) -> Any :
2646 """Synchronous remote call with optional RPC parameters."""
27- rpc_params = RPCParams (timeout = timeout ,
28- need_response = need_response ,
29- mode = "sync" )
30- self .kwargs ["__rpc_params" ] = rpc_params
31- return self .client ._call_sync (self .method_name , * self .args ,
32- ** self .kwargs )
47+ return self ._prepare_and_call (timeout , need_response , "sync" ,
48+ "_call_sync" )
3349
3450 def remote_async (self ,
3551 timeout : Optional [float ] = None ,
3652 need_response : bool = True ):
3753 """Asynchronous remote call that returns a coroutine."""
38- rpc_params = RPCParams (timeout = timeout ,
39- need_response = need_response ,
40- mode = "async" )
41- self .kwargs ["__rpc_params" ] = rpc_params
42- return self .client ._call_async (self .method_name , * self .args ,
43- ** self .kwargs )
54+ return self ._prepare_and_call (timeout , need_response , "async" ,
55+ "_call_async" )
4456
4557 def remote_future (self ,
4658 timeout : Optional [float ] = None ,
4759 need_response : bool = True ) -> concurrent .futures .Future :
4860 """Remote call that returns a Future object."""
49- rpc_params = RPCParams (timeout = timeout ,
50- need_response = need_response ,
51- mode = "future" )
52- self .kwargs ["__rpc_params" ] = rpc_params
53- return self .client .call_future (self .method_name , * self .args ,
54- ** self .kwargs )
61+ return self ._prepare_and_call (timeout , need_response , "future" ,
62+ "call_future" )
5563
5664 def remote_streaming (self ,
5765 timeout : Optional [float ] = None ) -> AsyncIterator [Any ]:
5866 """Remote call for streaming results."""
59- rpc_params = RPCParams (timeout = timeout ,
60- need_response = True ,
61- mode = "async" )
62- self .kwargs ["__rpc_params" ] = rpc_params
63- return self .client .call_streaming (self .method_name , * self .args ,
64- ** self .kwargs )
67+ # Streaming always needs a response
68+ return self ._prepare_and_call (timeout , True , "async" , "call_streaming" )
6569
6670
6771class RPCClient :
@@ -309,7 +313,7 @@ async def _call_async(self, method_name, *args, **kwargs):
309313 res = await future
310314 else :
311315 # Add 1 second to the timeout to ensure the client can get
312- res = await asyncio .wait_for (future , timeout + 1 )
316+ res = await asyncio .wait_for (future , timeout )
313317 logger_debug (
314318 f"RPC Client _call_async: Got result for request_id: { request_id } : { res } "
315319 )
@@ -361,7 +365,7 @@ def _call_sync(self, method_name, *args, **kwargs):
361365 f"RPC Client _call_sync: Got result for { method_name } : { result } " )
362366 return result
363367
364- def call_async (self , name : str , * args , ** kwargs ):
368+ def call_async (self , name : str , * args , ** kwargs ) -> Any :
365369 """
366370 Call a remote method asynchronously.
367371
@@ -408,7 +412,7 @@ def _async_to_sync():
408412
409413 return self ._executor .submit (_async_to_sync )
410414
411- def call_sync (self , name : str , * args , ** kwargs ):
415+ def call_sync (self , name : str , * args , ** kwargs ) -> Any :
412416 """
413417 Call a remote method synchronously (blocking).
414418
@@ -476,7 +480,7 @@ async def call_streaming(self, name: str, *args,
476480 response = await queue .get ()
477481 else :
478482 response = await asyncio .wait_for (queue .get (),
479- timeout = timeout + 1 )
483+ timeout = timeout )
480484
481485 logger_debug (
482486 f"RPC Client call_streaming received [{ response .stream_status } ] response: { response } " ,
0 commit comments