@@ -70,16 +70,6 @@ async def __aexit__(self, *args):
70
70
71
71
nullcontext = NullContext ()
72
72
73
- NONBLOCKING_EXCEPTION_ERROR_NUMBERS = {
74
- BlockingIOError : errno .EWOULDBLOCK ,
75
- ssl .SSLWantReadError : 2 ,
76
- ssl .SSLWantWriteError : 2 ,
77
- ssl .SSLError : 2 ,
78
- }
79
-
80
- NONBLOCKING_EXCEPTIONS = tuple (NONBLOCKING_EXCEPTION_ERROR_NUMBERS .keys ())
81
-
82
-
83
73
SYM_STAR = b"*"
84
74
SYM_DOLLAR = b"$"
85
75
SYM_CRLF = b"\r \n "
@@ -233,11 +223,9 @@ def __init__(
233
223
self ,
234
224
stream_reader : asyncio .StreamReader ,
235
225
socket_read_size : int ,
236
- socket_timeout : Optional [float ],
237
226
):
238
227
self ._stream : Optional [asyncio .StreamReader ] = stream_reader
239
228
self .socket_read_size = socket_read_size
240
- self .socket_timeout = socket_timeout
241
229
self ._buffer : Optional [io .BytesIO ] = io .BytesIO ()
242
230
# number of bytes written to the buffer from the socket
243
231
self .bytes_written = 0
@@ -248,52 +236,35 @@ def __init__(
248
236
def length (self ):
249
237
return self .bytes_written - self .bytes_read
250
238
251
- async def _read_from_socket (
252
- self ,
253
- length : Optional [int ] = None ,
254
- timeout : Union [float , None , _Sentinel ] = SENTINEL ,
255
- raise_on_timeout : bool = True ,
256
- ) -> bool :
239
+ async def _read_from_socket (self , length : Optional [int ] = None ) -> bool :
257
240
buf = self ._buffer
258
241
if buf is None or self ._stream is None :
259
242
raise RedisError ("Buffer is closed." )
260
243
buf .seek (self .bytes_written )
261
244
marker = 0
262
- timeout = timeout if timeout is not SENTINEL else self .socket_timeout
263
245
264
- try :
265
- while True :
266
- async with async_timeout .timeout (timeout ):
267
- data = await self ._stream .read (self .socket_read_size )
268
- # an empty string indicates the server shutdown the socket
269
- if isinstance (data , bytes ) and len (data ) == 0 :
270
- raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
271
- buf .write (data )
272
- data_length = len (data )
273
- self .bytes_written += data_length
274
- marker += data_length
275
-
276
- if length is not None and length > marker :
277
- continue
278
- return True
279
- except (socket .timeout , asyncio .TimeoutError ):
280
- if raise_on_timeout :
281
- raise TimeoutError ("Timeout reading from socket" )
282
- return False
283
- except NONBLOCKING_EXCEPTIONS as ex :
284
- # if we're in nonblocking mode and the recv raises a
285
- # blocking error, simply return False indicating that
286
- # there's no data to be read. otherwise raise the
287
- # original exception.
288
- allowed = NONBLOCKING_EXCEPTION_ERROR_NUMBERS .get (ex .__class__ , - 1 )
289
- if not raise_on_timeout and ex .errno == allowed :
290
- return False
291
- raise ConnectionError (f"Error while reading from socket: { ex .args } " )
246
+ while True :
247
+ data = await self ._stream .read (self .socket_read_size )
248
+ # an empty string indicates the server shutdown the socket
249
+ if isinstance (data , bytes ) and len (data ) == 0 :
250
+ raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
251
+ buf .write (data )
252
+ data_length = len (data )
253
+ self .bytes_written += data_length
254
+ marker += data_length
255
+
256
+ if length is not None and length > marker :
257
+ continue
258
+ return True
292
259
293
260
async def can_read (self , timeout : float ) -> bool :
294
- return bool (self .length ) or await self ._read_from_socket (
295
- timeout = timeout , raise_on_timeout = False
296
- )
261
+ if self .length :
262
+ return True
263
+ try :
264
+ async with async_timeout .timeout (timeout ):
265
+ return await self ._read_from_socket ()
266
+ except asyncio .TimeoutError :
267
+ return False
297
268
298
269
async def read (self , length : int ) -> bytes :
299
270
length = length + 2 # make sure to read the \r\n terminator
@@ -376,9 +347,7 @@ def on_connect(self, connection: "Connection"):
376
347
if self ._stream is None :
377
348
raise RedisError ("Buffer is closed." )
378
349
379
- self ._buffer = SocketBuffer (
380
- self ._stream , self ._read_size , connection .socket_timeout
381
- )
350
+ self ._buffer = SocketBuffer (self ._stream , self ._read_size )
382
351
self .encoder = connection .encoder
383
352
384
353
def on_disconnect (self ):
@@ -448,7 +417,7 @@ async def read_response(
448
417
class HiredisParser (BaseParser ):
449
418
"""Parser class for connections using Hiredis"""
450
419
451
- __slots__ = BaseParser .__slots__ + ("_next_response" , "_reader" , "_socket_timeout" )
420
+ __slots__ = BaseParser .__slots__ + ("_next_response" , "_reader" )
452
421
453
422
_next_response : bool
454
423
@@ -457,7 +426,6 @@ def __init__(self, socket_read_size: int):
457
426
raise RedisError ("Hiredis is not available." )
458
427
super ().__init__ (socket_read_size = socket_read_size )
459
428
self ._reader : Optional [hiredis .Reader ] = None
460
- self ._socket_timeout : Optional [float ] = None
461
429
462
430
def on_connect (self , connection : "Connection" ):
463
431
self ._stream = connection ._reader
@@ -471,7 +439,6 @@ def on_connect(self, connection: "Connection"):
471
439
472
440
self ._reader = hiredis .Reader (** kwargs )
473
441
self ._next_response = False
474
- self ._socket_timeout = connection .socket_timeout
475
442
476
443
def on_disconnect (self ):
477
444
self ._stream = None
@@ -485,42 +452,21 @@ async def can_read(self, timeout: float):
485
452
if self ._next_response is False :
486
453
self ._next_response = self ._reader .gets ()
487
454
if self ._next_response is False :
488
- return await self .read_from_socket (timeout = timeout , raise_on_timeout = False )
455
+ try :
456
+ with async_timeout .timeout (timeout ):
457
+ return await self .read_from_socket ()
458
+ except asyncio .TimeoutError :
459
+ return False
489
460
return True
490
461
491
- async def read_from_socket (
492
- self ,
493
- timeout : Union [float , None , _Sentinel ] = SENTINEL ,
494
- raise_on_timeout : bool = True ,
495
- ):
496
- timeout = self ._socket_timeout if timeout is SENTINEL else timeout
497
- try :
498
- if timeout is None :
499
- buffer = await self ._stream .read (self ._read_size )
500
- else :
501
- async with async_timeout .timeout (timeout ):
502
- buffer = await self ._stream .read (self ._read_size )
503
- if not buffer or not isinstance (buffer , bytes ):
504
- raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR ) from None
505
- self ._reader .feed (buffer )
506
- # data was read from the socket and added to the buffer.
507
- # return True to indicate that data was read.
508
- return True
509
- except asyncio .CancelledError :
510
- raise
511
- except (socket .timeout , asyncio .TimeoutError ):
512
- if raise_on_timeout :
513
- raise TimeoutError ("Timeout reading from socket" ) from None
514
- return False
515
- except NONBLOCKING_EXCEPTIONS as ex :
516
- # if we're in nonblocking mode and the recv raises a
517
- # blocking error, simply return False indicating that
518
- # there's no data to be read. otherwise raise the
519
- # original exception.
520
- allowed = NONBLOCKING_EXCEPTION_ERROR_NUMBERS .get (ex .__class__ , - 1 )
521
- if not raise_on_timeout and ex .errno == allowed :
522
- return False
523
- raise ConnectionError (f"Error while reading from socket: { ex .args } " )
462
+ async def read_from_socket (self ):
463
+ buffer = await self ._stream .read (self ._read_size )
464
+ if not buffer or not isinstance (buffer , bytes ):
465
+ raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR ) from None
466
+ self ._reader .feed (buffer )
467
+ # data was read from the socket and added to the buffer.
468
+ # return True to indicate that data was read.
469
+ return True
524
470
525
471
async def read_response (
526
472
self , disable_decoding : bool = False
0 commit comments