Debian Codegraph @

valkey/_parsers/libvalkey.py

raw
1 import asyncio
2 import socket
3 import sys
4 from typing import Callable, List, Optional, TypedDict, Union
5
6 if sys.version_info >= (3, 11, 3):
7 from asyncio import timeout as async_timeout
8 else:
9 from async_timeout import timeout as async_timeout
10
11 from ..exceptions import ConnectionError, InvalidResponse, ValkeyError
12 from ..typing import EncodableT
13 from ..utils import LIBVALKEY_AVAILABLE
14 from .base import AsyncBaseParser, BaseParser
15 from .socket import (
16 NONBLOCKING_EXCEPTION_ERROR_NUMBERS,
17 NONBLOCKING_EXCEPTIONS,
18 SENTINEL,
19 SERVER_CLOSED_CONNECTION_ERROR,
20 )
21
22 # Used to signal that libvalkey-py does not have enough data to parse.
23 # Using `False` or `None` is not reliable, given that the parser can
24 # return `False` or `None` for legitimate reasons from RESP payloads.
25 NOT_ENOUGH_DATA = object()
26
27
28 class _LibvalkeyReaderArgs(TypedDict, total=False):
29 protocolError: Callable[[str], Exception]
30 replyError: Callable[[str], Exception]
31 encoding: Optional[str]
32 errors: Optional[str]
33
34
35 class _LibvalkeyParser(BaseParser):
36 "Parser class for connections using libvalkey"
37
38 def __init__(self, socket_read_size):
39 if not LIBVALKEY_AVAILABLE:
40 raise ValkeyError("libvalkey is not installed")
41 self.socket_read_size = socket_read_size
42 self._buffer = bytearray(socket_read_size)
43
44 def __del__(self):
45 try:
46 self.on_disconnect()
47 except Exception:
48 pass
49
50 def on_connect(self, connection, **kwargs):
51 import libvalkey
52
53 self._sock = connection._sock
54 self._socket_timeout = connection.socket_timeout
55 kwargs = {
56 "protocolError": InvalidResponse,
57 "replyError": self.parse_error,
58 "errors": connection.encoder.encoding_errors,
59 "notEnoughData": NOT_ENOUGH_DATA,
60 "convertSetsToLists": True,
61 }
62
63 if connection.encoder.decode_responses:
64 kwargs["encoding"] = connection.encoder.encoding
65 self._reader = libvalkey.Reader(**kwargs)
66 self._next_response = NOT_ENOUGH_DATA
67
68 def on_disconnect(self):
69 self._sock = None
70 self._reader = None
71 self._next_response = NOT_ENOUGH_DATA
72
73 def can_read(self, timeout):
74 if not self._reader:
75 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
76
77 if self._next_response is NOT_ENOUGH_DATA:
78 self._next_response = self._reader.gets()
79 if self._next_response is NOT_ENOUGH_DATA:
80 return self.read_from_socket(timeout=timeout, raise_on_timeout=False)
81 return True
82
83 def read_from_socket(self, timeout=SENTINEL, raise_on_timeout=True):
84 sock = self._sock
85 custom_timeout = timeout is not SENTINEL
86 try:
87 if custom_timeout:
88 sock.settimeout(timeout)
89 bufflen = self._sock.recv_into(self._buffer)
90 if bufflen == 0:
91 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
92 self._reader.feed(self._buffer, 0, bufflen)
93 # data was read from the socket and added to the buffer.
94 # return True to indicate that data was read.
95 return True
96 except socket.timeout:
97 if raise_on_timeout:
98 raise TimeoutError("Timeout reading from socket")
99 return False
100 except NONBLOCKING_EXCEPTIONS as ex:
101 # if we're in nonblocking mode and the recv raises a
102 # blocking error, simply return False indicating that
103 # there's no data to be read. otherwise raise the
104 # original exception.
105 allowed = NONBLOCKING_EXCEPTION_ERROR_NUMBERS.get(ex.__class__, -1)
106 if not raise_on_timeout and ex.errno == allowed:
107 return False
108 raise ConnectionError(f"Error while reading from socket: {ex.args}")
109 finally:
110 if custom_timeout:
111 sock.settimeout(self._socket_timeout)
112
113 def read_response(self, disable_decoding=False):
114 if not self._reader:
115 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
116
117 # _next_response might be cached from a can_read() call
118 if self._next_response is not NOT_ENOUGH_DATA:
119 response = self._next_response
120 self._next_response = NOT_ENOUGH_DATA
121 return response
122
123 if disable_decoding:
124 response = self._reader.gets(False)
125 else:
126 response = self._reader.gets()
127
128 while response is NOT_ENOUGH_DATA:
129 self.read_from_socket()
130 if disable_decoding:
131 response = self._reader.gets(False)
132 else:
133 response = self._reader.gets()
134 # if the response is a ConnectionError or the response is a list and
135 # the first item is a ConnectionError, raise it as something bad
136 # happened
137 if isinstance(response, ConnectionError):
138 raise response
139 elif (
140 isinstance(response, list)
141 and response
142 and isinstance(response[0], ConnectionError)
143 ):
144 raise response[0]
145 return response
146
147
148 class _AsyncLibvalkeyParser(AsyncBaseParser):
149 """Async implementation of parser class for connections using libvalkey"""
150
151 __slots__ = ("_reader",)
152
153 def __init__(self, socket_read_size: int):
154 if not LIBVALKEY_AVAILABLE:
155 raise ValkeyError("libvalkey is not available.")
156 super().__init__(socket_read_size=socket_read_size)
157 self._reader = None
158
159 def on_connect(self, connection):
160 import libvalkey
161
162 self._stream = connection._reader
163 kwargs: _LibvalkeyReaderArgs = {
164 "protocolError": InvalidResponse,
165 "replyError": self.parse_error,
166 "notEnoughData": NOT_ENOUGH_DATA,
167 "convertSetsToLists": True,
168 }
169 if connection.encoder.decode_responses:
170 kwargs["encoding"] = connection.encoder.encoding
171 kwargs["errors"] = connection.encoder.encoding_errors
172
173 self._reader = libvalkey.Reader(**kwargs)
174 self._connected = True
175
176 def on_disconnect(self):
177 self._connected = False
178
179 async def can_read_destructive(self):
180 if not self._connected:
181 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
182 if self._reader.gets() is not NOT_ENOUGH_DATA:
183 return True
184 try:
185 async with async_timeout(0):
186 return await self.read_from_socket()
187 except asyncio.TimeoutError:
188 return False
189
190 async def read_from_socket(self):
191 buffer = await self._stream.read(self._read_size)
192 if not buffer or not isinstance(buffer, bytes):
193 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) from None
194 self._reader.feed(buffer)
195 # data was read from the socket and added to the buffer.
196 # return True to indicate that data was read.
197 return True
198
199 async def read_response(
200 self, disable_decoding: bool = False
201 ) -> Union[EncodableT, List[EncodableT]]:
202 # If `on_disconnect()` has been called, prohibit any more reads
203 # even if they could happen because data might be present.
204 # We still allow reads in progress to finish
205 if not self._connected:
206 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) from None
207
208 if disable_decoding:
209 response = self._reader.gets(False)
210 else:
211 response = self._reader.gets()
212 while response is NOT_ENOUGH_DATA:
213 await self.read_from_socket()
214 if disable_decoding:
215 response = self._reader.gets(False)
216 else:
217 response = self._reader.gets()
218
219 # if the response is a ConnectionError or the response is a list and
220 # the first item is a ConnectionError, raise it as something bad
221 # happened
222 if isinstance(response, ConnectionError):
223 raise response
224 elif (
225 isinstance(response, list)
226 and response
227 and isinstance(response[0], ConnectionError)
228 ):
229 raise response[0]
230 return response