2024-12-13 16:45:21 +03:00
|
|
|
import asyncio
|
2024-10-23 11:54:32 +03:00
|
|
|
import aio_pika
|
2024-12-13 16:45:21 +03:00
|
|
|
from starlette.requests import Request
|
2024-10-23 11:54:32 +03:00
|
|
|
from aio_pika.abc import AbstractRobustConnection
|
|
|
|
|
|
|
|
from ..config import RMQ_HOST, RMQ_PORT, RMQ_USER, RMQ_PASSWORD
|
|
|
|
|
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
fqdn = f'amqp://{RMQ_USER}:{str(RMQ_PASSWORD)}@{RMQ_HOST}:{RMQ_PORT}/'
|
2024-10-23 11:54:32 +03:00
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
async def get_connection() -> AbstractRobustConnection:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
conn = await aio_pika.connect_robust(fqdn)
|
|
|
|
return conn
|
|
|
|
except ConnectionError:
|
|
|
|
await asyncio.sleep(2)
|
2024-10-23 11:54:32 +03:00
|
|
|
|
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
async def get_channel(conn_pool: AbstractRobustConnection) -> aio_pika.Channel:
|
2024-10-23 11:54:32 +03:00
|
|
|
async with conn_pool.acquire() as connection:
|
|
|
|
return await connection.channel()
|
|
|
|
|
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
async def get_rmq(request: Request) -> aio_pika.Channel:
|
|
|
|
async with request.app.state.rmq_chan_pool.acquire() as chan:
|
2024-10-23 11:54:32 +03:00
|
|
|
yield chan
|
|
|
|
|