| 1 |
from logging import getLogger |
| 2 |
from typing import Any, Union |
| 3 |
|
| 4 |
from ..exceptions import ConnectionError, InvalidResponse, ResponseError |
| 5 |
from ..typing import EncodableT |
| 6 |
from .base import _AsyncRESPBase, _RESPBase |
| 7 |
from .socket import SERVER_CLOSED_CONNECTION_ERROR |
| 8 |
|
| 9 |
_INVALIDATION_MESSAGE = [b"invalidate", "invalidate"] |
| 10 |
|
| 11 |
|
| 12 |
class _RESP3Parser(_RESPBase): |
| 13 |
"""RESP3 protocol implementation""" |
| 14 |
|
| 15 |
def __init__(self, socket_read_size): |
| 16 |
super().__init__(socket_read_size) |
| 17 |
self.pubsub_push_handler_func = self.handle_pubsub_push_response |
| 18 |
self.invalidations_push_handler_func = None |
| 19 |
|
| 20 |
def handle_pubsub_push_response(self, response): |
| 21 |
logger = getLogger("push_response") |
| 22 |
logger.debug("Push response: " + str(response)) |
| 23 |
return response |
| 24 |
|
| 25 |
def read_response(self, disable_decoding=False, push_request=False): |
| 26 |
pos = self._buffer.get_pos() if self._buffer else None |
| 27 |
try: |
| 28 |
result = self._read_response( |
| 29 |
disable_decoding=disable_decoding, push_request=push_request |
| 30 |
) |
| 31 |
except BaseException: |
| 32 |
if self._buffer: |
| 33 |
self._buffer.rewind(pos) |
| 34 |
raise |
| 35 |
else: |
| 36 |
self._buffer.purge() |
| 37 |
return result |
| 38 |
|
| 39 |
def _read_response(self, disable_decoding=False, push_request=False): |
| 40 |
raw = self._buffer.readline() |
| 41 |
if not raw: |
| 42 |
raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) |
| 43 |
|
| 44 |
byte, response = raw[:1], raw[1:] |
| 45 |
|
| 46 |
# server returned an error |
| 47 |
if byte in (b"-", b"!"): |
| 48 |
if byte == b"!": |
| 49 |
response = self._buffer.read(int(response)) |
| 50 |
response = response.decode("utf-8", errors="replace") |
| 51 |
error = self.parse_error(response) |
| 52 |
# if the error is a ConnectionError, raise immediately so the user |
| 53 |
# is notified |
| 54 |
if isinstance(error, ConnectionError): |
| 55 |
raise error |
| 56 |
# otherwise, we're dealing with a ResponseError that might belong |
| 57 |
# inside a pipeline response. the connection's read_response() |
| 58 |
# and/or the pipeline's execute() will raise this error if |
| 59 |
# necessary, so just return the exception instance here. |
| 60 |
return error |
| 61 |
# single value |
| 62 |
elif byte == b"+": |
| 63 |
pass |
| 64 |
# null value |
| 65 |
elif byte == b"_": |
| 66 |
return None |
| 67 |
# int and big int values |
| 68 |
elif byte in (b":", b"("): |
| 69 |
return int(response) |
| 70 |
# double value |
| 71 |
elif byte == b",": |
| 72 |
return float(response) |
| 73 |
# bool value |
| 74 |
elif byte == b"#": |
| 75 |
return response == b"t" |
| 76 |
# bulk response |
| 77 |
elif byte == b"$": |
| 78 |
response = self._buffer.read(int(response)) |
| 79 |
# verbatim string response |
| 80 |
elif byte == b"=": |
| 81 |
response = self._buffer.read(int(response))[4:] |
| 82 |
# array response |
| 83 |
elif byte == b"*": |
| 84 |
response = [ |
| 85 |
self._read_response(disable_decoding=disable_decoding) |
| 86 |
for _ in range(int(response)) |
| 87 |
] |
| 88 |
# set response |
| 89 |
elif byte == b"~": |
| 90 |
# valkey can return unhashable types (like dict) in a set, |
| 91 |
# so we return sets as lists |
| 92 |
response = [ |
| 93 |
self._read_response(disable_decoding=disable_decoding) |
| 94 |
for _ in range(int(response)) |
| 95 |
] |
| 96 |
# map response |
| 97 |
elif byte == b"%": |
| 98 |
# We cannot use a dict-comprehension to parse stream. |
| 99 |
# Evaluation order of key:val expression in dict comprehension only |
| 100 |
# became defined to be left-right in version 3.8 |
| 101 |
resp_dict = {} |
| 102 |
for _ in range(int(response)): |
| 103 |
key = self._read_response(disable_decoding=disable_decoding) |
| 104 |
resp_dict[key] = self._read_response( |
| 105 |
disable_decoding=disable_decoding, push_request=push_request |
| 106 |
) |
| 107 |
response = resp_dict |
| 108 |
# push response |
| 109 |
elif byte == b">": |
| 110 |
response = [ |
| 111 |
self._read_response( |
| 112 |
disable_decoding=disable_decoding, push_request=push_request |
| 113 |
) |
| 114 |
for _ in range(int(response)) |
| 115 |
] |
| 116 |
response = self.handle_push_response( |
| 117 |
response, disable_decoding, push_request |
| 118 |
) |
| 119 |
else: |
| 120 |
raise InvalidResponse(f"Protocol Error: {raw!r}") |
| 121 |
|
| 122 |
if isinstance(response, bytes) and disable_decoding is False: |
| 123 |
response = self.encoder.decode(response) |
| 124 |
return response |
| 125 |
|
| 126 |
def handle_push_response(self, response, disable_decoding, push_request): |
| 127 |
if response[0] in _INVALIDATION_MESSAGE: |
| 128 |
res = self.invalidation_push_handler_func(response) |
| 129 |
else: |
| 130 |
res = self.pubsub_push_handler_func(response) |
| 131 |
if not push_request: |
| 132 |
return self._read_response( |
| 133 |
disable_decoding=disable_decoding, push_request=push_request |
| 134 |
) |
| 135 |
else: |
| 136 |
return res |
| 137 |
|
| 138 |
def set_pubsub_push_handler(self, pubsub_push_handler_func): |
| 139 |
self.pubsub_push_handler_func = pubsub_push_handler_func |
| 140 |
|
| 141 |
def set_invalidation_push_handler(self, invalidations_push_handler_func): |
| 142 |
self.invalidation_push_handler_func = invalidations_push_handler_func |
| 143 |
|
| 144 |
|
| 145 |
class _AsyncRESP3Parser(_AsyncRESPBase): |
| 146 |
def __init__(self, socket_read_size): |
| 147 |
super().__init__(socket_read_size) |
| 148 |
self.pubsub_push_handler_func = self.handle_pubsub_push_response |
| 149 |
self.invalidations_push_handler_func = None |
| 150 |
|
| 151 |
def handle_pubsub_push_response(self, response): |
| 152 |
logger = getLogger("push_response") |
| 153 |
logger.debug("Push response: " + str(response)) |
| 154 |
return response |
| 155 |
|
| 156 |
async def read_response( |
| 157 |
self, disable_decoding: bool = False, push_request: bool = False |
| 158 |
): |
| 159 |
if self._chunks: |
| 160 |
# augment parsing buffer with previously read data |
| 161 |
self._buffer += b"".join(self._chunks) |
| 162 |
self._chunks.clear() |
| 163 |
self._pos = 0 |
| 164 |
response = await self._read_response( |
| 165 |
disable_decoding=disable_decoding, push_request=push_request |
| 166 |
) |
| 167 |
# Successfully parsing a response allows us to clear our parsing buffer |
| 168 |
self._clear() |
| 169 |
return response |
| 170 |
|
| 171 |
async def _read_response( |
| 172 |
self, disable_decoding: bool = False, push_request: bool = False |
| 173 |
) -> Union[EncodableT, ResponseError, None]: |
| 174 |
if not self._stream or not self.encoder: |
| 175 |
raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) |
| 176 |
raw = await self._readline() |
| 177 |
response: Any |
| 178 |
byte, response = raw[:1], raw[1:] |
| 179 |
|
| 180 |
# if byte not in (b"-", b"+", b":", b"$", b"*"): |
| 181 |
# raise InvalidResponse(f"Protocol Error: {raw!r}") |
| 182 |
|
| 183 |
# server returned an error |
| 184 |
if byte in (b"-", b"!"): |
| 185 |
if byte == b"!": |
| 186 |
response = await self._read(int(response)) |
| 187 |
response = response.decode("utf-8", errors="replace") |
| 188 |
error = self.parse_error(response) |
| 189 |
# if the error is a ConnectionError, raise immediately so the user |
| 190 |
# is notified |
| 191 |
if isinstance(error, ConnectionError): |
| 192 |
self._clear() # Successful parse |
| 193 |
raise error |
| 194 |
# otherwise, we're dealing with a ResponseError that might belong |
| 195 |
# inside a pipeline response. the connection's read_response() |
| 196 |
# and/or the pipeline's execute() will raise this error if |
| 197 |
# necessary, so just return the exception instance here. |
| 198 |
return error |
| 199 |
# single value |
| 200 |
elif byte == b"+": |
| 201 |
pass |
| 202 |
# null value |
| 203 |
elif byte == b"_": |
| 204 |
return None |
| 205 |
# int and big int values |
| 206 |
elif byte in (b":", b"("): |
| 207 |
return int(response) |
| 208 |
# double value |
| 209 |
elif byte == b",": |
| 210 |
return float(response) |
| 211 |
# bool value |
| 212 |
elif byte == b"#": |
| 213 |
return response == b"t" |
| 214 |
# bulk response |
| 215 |
elif byte == b"$": |
| 216 |
response = await self._read(int(response)) |
| 217 |
# verbatim string response |
| 218 |
elif byte == b"=": |
| 219 |
response = (await self._read(int(response)))[4:] |
| 220 |
# array response |
| 221 |
elif byte == b"*": |
| 222 |
response = [ |
| 223 |
(await self._read_response(disable_decoding=disable_decoding)) |
| 224 |
for _ in range(int(response)) |
| 225 |
] |
| 226 |
# set response |
| 227 |
elif byte == b"~": |
| 228 |
# valkey can return unhashable types (like dict) in a set, |
| 229 |
# so we return sets as lists |
| 230 |
response = [ |
| 231 |
(await self._read_response(disable_decoding=disable_decoding)) |
| 232 |
for _ in range(int(response)) |
| 233 |
] |
| 234 |
# map response |
| 235 |
elif byte == b"%": |
| 236 |
# We cannot use a dict-comprehension to parse stream. |
| 237 |
# Evaluation order of key:val expression in dict comprehension only |
| 238 |
# became defined to be left-right in version 3.8 |
| 239 |
resp_dict = {} |
| 240 |
for _ in range(int(response)): |
| 241 |
key = await self._read_response(disable_decoding=disable_decoding) |
| 242 |
resp_dict[key] = await self._read_response( |
| 243 |
disable_decoding=disable_decoding, push_request=push_request |
| 244 |
) |
| 245 |
response = resp_dict |
| 246 |
# push response |
| 247 |
elif byte == b">": |
| 248 |
response = [ |
| 249 |
( |
| 250 |
await self._read_response( |
| 251 |
disable_decoding=disable_decoding, push_request=push_request |
| 252 |
) |
| 253 |
) |
| 254 |
for _ in range(int(response)) |
| 255 |
] |
| 256 |
response = await self.handle_push_response( |
| 257 |
response, disable_decoding, push_request |
| 258 |
) |
| 259 |
else: |
| 260 |
raise InvalidResponse(f"Protocol Error: {raw!r}") |
| 261 |
|
| 262 |
if isinstance(response, bytes) and disable_decoding is False: |
| 263 |
response = self.encoder.decode(response) |
| 264 |
return response |
| 265 |
|
| 266 |
async def handle_push_response(self, response, disable_decoding, push_request): |
| 267 |
if response[0] in _INVALIDATION_MESSAGE: |
| 268 |
res = self.invalidation_push_handler_func(response) |
| 269 |
else: |
| 270 |
res = self.pubsub_push_handler_func(response) |
| 271 |
if not push_request: |
| 272 |
return await self._read_response( |
| 273 |
disable_decoding=disable_decoding, push_request=push_request |
| 274 |
) |
| 275 |
else: |
| 276 |
return res |
| 277 |
|
| 278 |
def set_pubsub_push_handler(self, pubsub_push_handler_func): |
| 279 |
self.pubsub_push_handler_func = pubsub_push_handler_func |
| 280 |
|
| 281 |
def set_invalidation_push_handler(self, invalidations_push_handler_func): |
| 282 |
self.invalidation_push_handler_func = invalidations_push_handler_func |