This commit is contained in:
@@ -3,26 +3,20 @@ from typing import Any, Union
|
||||
|
||||
from ..exceptions import ConnectionError, InvalidResponse, ResponseError
|
||||
from ..typing import EncodableT
|
||||
from .base import (
|
||||
AsyncPushNotificationsParser,
|
||||
PushNotificationsParser,
|
||||
_AsyncRESPBase,
|
||||
_RESPBase,
|
||||
)
|
||||
from .base import _AsyncRESPBase, _RESPBase
|
||||
from .socket import SERVER_CLOSED_CONNECTION_ERROR
|
||||
|
||||
|
||||
class _RESP3Parser(_RESPBase, PushNotificationsParser):
|
||||
class _RESP3Parser(_RESPBase):
|
||||
"""RESP3 protocol implementation"""
|
||||
|
||||
def __init__(self, socket_read_size):
|
||||
super().__init__(socket_read_size)
|
||||
self.pubsub_push_handler_func = self.handle_pubsub_push_response
|
||||
self.invalidation_push_handler_func = None
|
||||
self.push_handler_func = self.handle_push_response
|
||||
|
||||
def handle_pubsub_push_response(self, response):
|
||||
def handle_push_response(self, response):
|
||||
logger = getLogger("push_response")
|
||||
logger.debug("Push response: " + str(response))
|
||||
logger.info("Push response: " + str(response))
|
||||
return response
|
||||
|
||||
def read_response(self, disable_decoding=False, push_request=False):
|
||||
@@ -91,16 +85,19 @@ class _RESP3Parser(_RESPBase, PushNotificationsParser):
|
||||
# set response
|
||||
elif byte == b"~":
|
||||
# redis can return unhashable types (like dict) in a set,
|
||||
# so we return sets as list, all the time, for predictability
|
||||
# so we need to first convert to a list, and then try to convert it to a set
|
||||
response = [
|
||||
self._read_response(disable_decoding=disable_decoding)
|
||||
for _ in range(int(response))
|
||||
]
|
||||
try:
|
||||
response = set(response)
|
||||
except TypeError:
|
||||
pass
|
||||
# map response
|
||||
elif byte == b"%":
|
||||
# We cannot use a dict-comprehension to parse stream.
|
||||
# Evaluation order of key:val expression in dict comprehension only
|
||||
# became defined to be left-right in version 3.8
|
||||
# we use this approach and not dict comprehension here
|
||||
# because this dict comprehension fails in python 3.7
|
||||
resp_dict = {}
|
||||
for _ in range(int(response)):
|
||||
key = self._read_response(disable_decoding=disable_decoding)
|
||||
@@ -116,13 +113,13 @@ class _RESP3Parser(_RESPBase, PushNotificationsParser):
|
||||
)
|
||||
for _ in range(int(response))
|
||||
]
|
||||
response = self.handle_push_response(response)
|
||||
res = self.push_handler_func(response)
|
||||
if not push_request:
|
||||
return self._read_response(
|
||||
disable_decoding=disable_decoding, push_request=push_request
|
||||
)
|
||||
else:
|
||||
return response
|
||||
return res
|
||||
else:
|
||||
raise InvalidResponse(f"Protocol Error: {raw!r}")
|
||||
|
||||
@@ -130,16 +127,18 @@ class _RESP3Parser(_RESPBase, PushNotificationsParser):
|
||||
response = self.encoder.decode(response)
|
||||
return response
|
||||
|
||||
def set_push_handler(self, push_handler_func):
|
||||
self.push_handler_func = push_handler_func
|
||||
|
||||
class _AsyncRESP3Parser(_AsyncRESPBase, AsyncPushNotificationsParser):
|
||||
|
||||
class _AsyncRESP3Parser(_AsyncRESPBase):
|
||||
def __init__(self, socket_read_size):
|
||||
super().__init__(socket_read_size)
|
||||
self.pubsub_push_handler_func = self.handle_pubsub_push_response
|
||||
self.invalidation_push_handler_func = None
|
||||
self.push_handler_func = self.handle_push_response
|
||||
|
||||
async def handle_pubsub_push_response(self, response):
|
||||
def handle_push_response(self, response):
|
||||
logger = getLogger("push_response")
|
||||
logger.debug("Push response: " + str(response))
|
||||
logger.info("Push response: " + str(response))
|
||||
return response
|
||||
|
||||
async def read_response(
|
||||
@@ -215,23 +214,23 @@ class _AsyncRESP3Parser(_AsyncRESPBase, AsyncPushNotificationsParser):
|
||||
# set response
|
||||
elif byte == b"~":
|
||||
# redis can return unhashable types (like dict) in a set,
|
||||
# so we always convert to a list, to have predictable return types
|
||||
# so we need to first convert to a list, and then try to convert it to a set
|
||||
response = [
|
||||
(await self._read_response(disable_decoding=disable_decoding))
|
||||
for _ in range(int(response))
|
||||
]
|
||||
try:
|
||||
response = set(response)
|
||||
except TypeError:
|
||||
pass
|
||||
# map response
|
||||
elif byte == b"%":
|
||||
# We cannot use a dict-comprehension to parse stream.
|
||||
# Evaluation order of key:val expression in dict comprehension only
|
||||
# became defined to be left-right in version 3.8
|
||||
resp_dict = {}
|
||||
for _ in range(int(response)):
|
||||
key = await self._read_response(disable_decoding=disable_decoding)
|
||||
resp_dict[key] = await self._read_response(
|
||||
disable_decoding=disable_decoding, push_request=push_request
|
||||
response = {
|
||||
(await self._read_response(disable_decoding=disable_decoding)): (
|
||||
await self._read_response(disable_decoding=disable_decoding)
|
||||
)
|
||||
response = resp_dict
|
||||
for _ in range(int(response))
|
||||
}
|
||||
# push response
|
||||
elif byte == b">":
|
||||
response = [
|
||||
@@ -242,16 +241,19 @@ class _AsyncRESP3Parser(_AsyncRESPBase, AsyncPushNotificationsParser):
|
||||
)
|
||||
for _ in range(int(response))
|
||||
]
|
||||
response = await self.handle_push_response(response)
|
||||
res = self.push_handler_func(response)
|
||||
if not push_request:
|
||||
return await self._read_response(
|
||||
disable_decoding=disable_decoding, push_request=push_request
|
||||
)
|
||||
else:
|
||||
return response
|
||||
return res
|
||||
else:
|
||||
raise InvalidResponse(f"Protocol Error: {raw!r}")
|
||||
|
||||
if isinstance(response, bytes) and disable_decoding is False:
|
||||
response = self.encoder.decode(response)
|
||||
return response
|
||||
|
||||
def set_push_handler(self, push_handler_func):
|
||||
self.push_handler_func = push_handler_func
|
||||
|
||||
Reference in New Issue
Block a user