This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
80 lines (63 loc) · 2.32 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import asyncio
import uuid
from typing import MutableMapping
import orjson
from aio_pika import Message, connect
from aio_pika.abc import AbstractChannel
from aio_pika.abc import AbstractConnection
from aio_pika.abc import AbstractIncomingMessage
from aio_pika.abc import AbstractQueue
class RpcClient:
connection: AbstractConnection
channel: AbstractChannel
callback_queue: AbstractQueue
loop: asyncio.AbstractEventLoop
def __init__(
self,
host: str,
port: int
) -> None:
self._host: str = host
self._port: int = port
self.futures: MutableMapping[str, asyncio.Future] = {}
self.loop = asyncio.get_running_loop()
async def connect(self) -> "RpcClient":
self.connection = await connect(
host=self._host,
port=self._port,
loop=self.loop
)
self.channel = await self.connection.channel()
self.callback_queue = await self.channel.declare_queue(exclusive=True)
await self.callback_queue.consume(self.on_response, no_ack=True)
return self
def on_response(self, message: AbstractIncomingMessage) -> None:
if message.correlation_id is None:
print(f"Bad message {message!r}")
return
future: asyncio.Future = self.futures.pop(message.correlation_id)
future.set_result(message.body)
async def call(self, request: dict) -> dict:
correlation_id = str(uuid.uuid4())
future = self.loop.create_future()
self.futures[correlation_id] = future
# message: any -> body: bytes
body: bytes = orjson.dumps(request)
await self.channel.default_exchange.publish(
Message(
body,
content_type='application/octet-stream',
correlation_id=correlation_id,
reply_to=self.callback_queue.name,
),
routing_key="rpc_queue",
)
x: bytes = await future # TODO
return orjson.loads(x)
async def __aenter__(self) -> "RpcClient":
await self.connect()
return self
async def __aexit__(self, exc_type, exc_value, traceback) -> None:
if exc_type is not None:
print(f"Exception caught: {exc_type!r}, {exc_value!r}")
await self.connection.close()