event.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import asyncio
  2. import threading
  3. from abc import ABC, abstractmethod
  4. from enum import Enum
  5. from typing import List, Optional, Union
  6. from redis.auth.token import TokenInterface
  7. from redis.credentials import CredentialProvider, StreamingCredentialProvider
  8. class EventListenerInterface(ABC):
  9. """
  10. Represents a listener for given event object.
  11. """
  12. @abstractmethod
  13. def listen(self, event: object):
  14. pass
  15. class AsyncEventListenerInterface(ABC):
  16. """
  17. Represents an async listener for given event object.
  18. """
  19. @abstractmethod
  20. async def listen(self, event: object):
  21. pass
  22. class EventDispatcherInterface(ABC):
  23. """
  24. Represents a dispatcher that dispatches events to listeners
  25. associated with given event.
  26. """
  27. @abstractmethod
  28. def dispatch(self, event: object):
  29. pass
  30. @abstractmethod
  31. async def dispatch_async(self, event: object):
  32. pass
  33. class EventException(Exception):
  34. """
  35. Exception wrapper that adds an event object into exception context.
  36. """
  37. def __init__(self, exception: Exception, event: object):
  38. self.exception = exception
  39. self.event = event
  40. super().__init__(exception)
  41. class EventDispatcher(EventDispatcherInterface):
  42. # TODO: Make dispatcher to accept external mappings.
  43. def __init__(self):
  44. """
  45. Mapping should be extended for any new events or listeners to be added.
  46. """
  47. self._event_listeners_mapping = {
  48. AfterConnectionReleasedEvent: [
  49. ReAuthConnectionListener(),
  50. ],
  51. AfterPooledConnectionsInstantiationEvent: [
  52. RegisterReAuthForPooledConnections()
  53. ],
  54. AfterSingleConnectionInstantiationEvent: [
  55. RegisterReAuthForSingleConnection()
  56. ],
  57. AfterPubSubConnectionInstantiationEvent: [RegisterReAuthForPubSub()],
  58. AfterAsyncClusterInstantiationEvent: [RegisterReAuthForAsyncClusterNodes()],
  59. AsyncAfterConnectionReleasedEvent: [
  60. AsyncReAuthConnectionListener(),
  61. ],
  62. }
  63. def dispatch(self, event: object):
  64. listeners = self._event_listeners_mapping.get(type(event))
  65. for listener in listeners:
  66. listener.listen(event)
  67. async def dispatch_async(self, event: object):
  68. listeners = self._event_listeners_mapping.get(type(event))
  69. for listener in listeners:
  70. await listener.listen(event)
  71. class AfterConnectionReleasedEvent:
  72. """
  73. Event that will be fired before each command execution.
  74. """
  75. def __init__(self, connection):
  76. self._connection = connection
  77. @property
  78. def connection(self):
  79. return self._connection
  80. class AsyncAfterConnectionReleasedEvent(AfterConnectionReleasedEvent):
  81. pass
  82. class ClientType(Enum):
  83. SYNC = ("sync",)
  84. ASYNC = ("async",)
  85. class AfterPooledConnectionsInstantiationEvent:
  86. """
  87. Event that will be fired after pooled connection instances was created.
  88. """
  89. def __init__(
  90. self,
  91. connection_pools: List,
  92. client_type: ClientType,
  93. credential_provider: Optional[CredentialProvider] = None,
  94. ):
  95. self._connection_pools = connection_pools
  96. self._client_type = client_type
  97. self._credential_provider = credential_provider
  98. @property
  99. def connection_pools(self):
  100. return self._connection_pools
  101. @property
  102. def client_type(self) -> ClientType:
  103. return self._client_type
  104. @property
  105. def credential_provider(self) -> Union[CredentialProvider, None]:
  106. return self._credential_provider
  107. class AfterSingleConnectionInstantiationEvent:
  108. """
  109. Event that will be fired after single connection instances was created.
  110. :param connection_lock: For sync client thread-lock should be provided,
  111. for async asyncio.Lock
  112. """
  113. def __init__(
  114. self,
  115. connection,
  116. client_type: ClientType,
  117. connection_lock: Union[threading.Lock, asyncio.Lock],
  118. ):
  119. self._connection = connection
  120. self._client_type = client_type
  121. self._connection_lock = connection_lock
  122. @property
  123. def connection(self):
  124. return self._connection
  125. @property
  126. def client_type(self) -> ClientType:
  127. return self._client_type
  128. @property
  129. def connection_lock(self) -> Union[threading.Lock, asyncio.Lock]:
  130. return self._connection_lock
  131. class AfterPubSubConnectionInstantiationEvent:
  132. def __init__(
  133. self,
  134. pubsub_connection,
  135. connection_pool,
  136. client_type: ClientType,
  137. connection_lock: Union[threading.Lock, asyncio.Lock],
  138. ):
  139. self._pubsub_connection = pubsub_connection
  140. self._connection_pool = connection_pool
  141. self._client_type = client_type
  142. self._connection_lock = connection_lock
  143. @property
  144. def pubsub_connection(self):
  145. return self._pubsub_connection
  146. @property
  147. def connection_pool(self):
  148. return self._connection_pool
  149. @property
  150. def client_type(self) -> ClientType:
  151. return self._client_type
  152. @property
  153. def connection_lock(self) -> Union[threading.Lock, asyncio.Lock]:
  154. return self._connection_lock
  155. class AfterAsyncClusterInstantiationEvent:
  156. """
  157. Event that will be fired after async cluster instance was created.
  158. Async cluster doesn't use connection pools,
  159. instead ClusterNode object manages connections.
  160. """
  161. def __init__(
  162. self,
  163. nodes: dict,
  164. credential_provider: Optional[CredentialProvider] = None,
  165. ):
  166. self._nodes = nodes
  167. self._credential_provider = credential_provider
  168. @property
  169. def nodes(self) -> dict:
  170. return self._nodes
  171. @property
  172. def credential_provider(self) -> Union[CredentialProvider, None]:
  173. return self._credential_provider
  174. class ReAuthConnectionListener(EventListenerInterface):
  175. """
  176. Listener that performs re-authentication of given connection.
  177. """
  178. def listen(self, event: AfterConnectionReleasedEvent):
  179. event.connection.re_auth()
  180. class AsyncReAuthConnectionListener(AsyncEventListenerInterface):
  181. """
  182. Async listener that performs re-authentication of given connection.
  183. """
  184. async def listen(self, event: AsyncAfterConnectionReleasedEvent):
  185. await event.connection.re_auth()
  186. class RegisterReAuthForPooledConnections(EventListenerInterface):
  187. """
  188. Listener that registers a re-authentication callback for pooled connections.
  189. Required by :class:`StreamingCredentialProvider`.
  190. """
  191. def __init__(self):
  192. self._event = None
  193. def listen(self, event: AfterPooledConnectionsInstantiationEvent):
  194. if isinstance(event.credential_provider, StreamingCredentialProvider):
  195. self._event = event
  196. if event.client_type == ClientType.SYNC:
  197. event.credential_provider.on_next(self._re_auth)
  198. event.credential_provider.on_error(self._raise_on_error)
  199. else:
  200. event.credential_provider.on_next(self._re_auth_async)
  201. event.credential_provider.on_error(self._raise_on_error_async)
  202. def _re_auth(self, token):
  203. for pool in self._event.connection_pools:
  204. pool.re_auth_callback(token)
  205. async def _re_auth_async(self, token):
  206. for pool in self._event.connection_pools:
  207. await pool.re_auth_callback(token)
  208. def _raise_on_error(self, error: Exception):
  209. raise EventException(error, self._event)
  210. async def _raise_on_error_async(self, error: Exception):
  211. raise EventException(error, self._event)
  212. class RegisterReAuthForSingleConnection(EventListenerInterface):
  213. """
  214. Listener that registers a re-authentication callback for single connection.
  215. Required by :class:`StreamingCredentialProvider`.
  216. """
  217. def __init__(self):
  218. self._event = None
  219. def listen(self, event: AfterSingleConnectionInstantiationEvent):
  220. if isinstance(
  221. event.connection.credential_provider, StreamingCredentialProvider
  222. ):
  223. self._event = event
  224. if event.client_type == ClientType.SYNC:
  225. event.connection.credential_provider.on_next(self._re_auth)
  226. event.connection.credential_provider.on_error(self._raise_on_error)
  227. else:
  228. event.connection.credential_provider.on_next(self._re_auth_async)
  229. event.connection.credential_provider.on_error(
  230. self._raise_on_error_async
  231. )
  232. def _re_auth(self, token):
  233. with self._event.connection_lock:
  234. self._event.connection.send_command(
  235. "AUTH", token.try_get("oid"), token.get_value()
  236. )
  237. self._event.connection.read_response()
  238. async def _re_auth_async(self, token):
  239. async with self._event.connection_lock:
  240. await self._event.connection.send_command(
  241. "AUTH", token.try_get("oid"), token.get_value()
  242. )
  243. await self._event.connection.read_response()
  244. def _raise_on_error(self, error: Exception):
  245. raise EventException(error, self._event)
  246. async def _raise_on_error_async(self, error: Exception):
  247. raise EventException(error, self._event)
  248. class RegisterReAuthForAsyncClusterNodes(EventListenerInterface):
  249. def __init__(self):
  250. self._event = None
  251. def listen(self, event: AfterAsyncClusterInstantiationEvent):
  252. if isinstance(event.credential_provider, StreamingCredentialProvider):
  253. self._event = event
  254. event.credential_provider.on_next(self._re_auth)
  255. event.credential_provider.on_error(self._raise_on_error)
  256. async def _re_auth(self, token: TokenInterface):
  257. for key in self._event.nodes:
  258. await self._event.nodes[key].re_auth_callback(token)
  259. async def _raise_on_error(self, error: Exception):
  260. raise EventException(error, self._event)
  261. class RegisterReAuthForPubSub(EventListenerInterface):
  262. def __init__(self):
  263. self._connection = None
  264. self._connection_pool = None
  265. self._client_type = None
  266. self._connection_lock = None
  267. self._event = None
  268. def listen(self, event: AfterPubSubConnectionInstantiationEvent):
  269. if isinstance(
  270. event.pubsub_connection.credential_provider, StreamingCredentialProvider
  271. ) and event.pubsub_connection.get_protocol() in [3, "3"]:
  272. self._event = event
  273. self._connection = event.pubsub_connection
  274. self._connection_pool = event.connection_pool
  275. self._client_type = event.client_type
  276. self._connection_lock = event.connection_lock
  277. if self._client_type == ClientType.SYNC:
  278. self._connection.credential_provider.on_next(self._re_auth)
  279. self._connection.credential_provider.on_error(self._raise_on_error)
  280. else:
  281. self._connection.credential_provider.on_next(self._re_auth_async)
  282. self._connection.credential_provider.on_error(
  283. self._raise_on_error_async
  284. )
  285. def _re_auth(self, token: TokenInterface):
  286. with self._connection_lock:
  287. self._connection.send_command(
  288. "AUTH", token.try_get("oid"), token.get_value()
  289. )
  290. self._connection.read_response()
  291. self._connection_pool.re_auth_callback(token)
  292. async def _re_auth_async(self, token: TokenInterface):
  293. async with self._connection_lock:
  294. await self._connection.send_command(
  295. "AUTH", token.try_get("oid"), token.get_value()
  296. )
  297. await self._connection.read_response()
  298. await self._connection_pool.re_auth_callback(token)
  299. def _raise_on_error(self, error: Exception):
  300. raise EventException(error, self._event)
  301. async def _raise_on_error_async(self, error: Exception):
  302. raise EventException(error, self._event)