22import typing
33from functools import partial
44
5+ from cqrs .adapters import protocol
6+
57import aio_pika
68from aio_pika import abc , pool
79
@@ -10,46 +12,28 @@ async def connection_pool_factory(url: str) -> abc.AbstractRobustConnection:
1012 return await aio_pika .connect_robust (url = url )
1113
1214
13- async def channel_pool_factory (connection_pool : pool .Pool ) -> aio_pika .Channel :
15+ async def channel_pool_factory (
16+ connection_pool : pool .Pool [aio_pika .abc .AbstractConnection ],
17+ ) -> aio_pika .abc .AbstractChannel :
1418 async with connection_pool .acquire () as connection :
1519 return await connection .channel ()
1620
1721
18- class AMQPPublisher :
19- def __init__ (self , url : str , max_connection_pool_size = 2 , max_channel_pool_size = 10 ):
20- self .url = url
21- self .max_connection_pool_size = max_connection_pool_size
22- self .max_channel_pool_size = max_channel_pool_size
23- self .connection_pool : pool .Pool = pool .Pool (
24- partial (connection_pool_factory , url = url ),
25- max_size = self .max_connection_pool_size ,
26- )
27- self .channel_pool : pool .Pool = pool .Pool (
28- partial (channel_pool_factory , connection_pool = self .connection_pool ),
29- max_size = self .max_channel_pool_size ,
30- )
22+ class AMQPPublisher (protocol .AMQPPublisher ):
23+ def __init__ (self , channel_pool : pool .Pool [aio_pika .abc .AbstractChannel ]):
24+ self .channel_pool = channel_pool
3125
3226 async def publish (self , message : abc .AbstractMessage , queue_name : str , exchange_name : str ) -> None :
3327 async with self .channel_pool .acquire () as channel :
34- queue : aio_pika . Queue = await channel .declare_queue (queue_name )
35- exchange : aio_pika . Exchange = await channel .declare_exchange (exchange_name , type = "direct" , auto_delete = True )
28+ queue = await channel .declare_queue (queue_name )
29+ exchange = await channel .declare_exchange (exchange_name , type = "direct" , auto_delete = True )
3630 await queue .bind (exchange = exchange , routing_key = queue_name )
3731 await exchange .publish (message = message , routing_key = queue_name )
3832
3933
40- class AMQPConsumer :
41- def __init__ (self , url : str , max_connection_pool_size = 2 , max_channel_pool_size = 10 ):
42- self .url = url
43- self .max_connection_pool_size = max_connection_pool_size
44- self .max_channel_pool_size = max_channel_pool_size
45- self .connection_pool : pool .Pool = pool .Pool (
46- partial (connection_pool_factory , url = url ),
47- max_size = self .max_connection_pool_size ,
48- )
49- self .channel_pool : pool .Pool = pool .Pool (
50- partial (channel_pool_factory , connection_pool = self .connection_pool ),
51- max_size = self .max_channel_pool_size ,
52- )
34+ class AMQPConsumer (protocol .AMQPConsumer ):
35+ def __init__ (self , channel_pool : pool .Pool [aio_pika .abc .AbstractChannel ]):
36+ self .channel_pool = channel_pool
5337
5438 async def consume (
5539 self ,
@@ -59,5 +43,41 @@ async def consume(
5943 async with self .channel_pool .acquire () as channel :
6044 await channel .set_qos (prefetch_count = 1 )
6145 queue = await channel .declare_queue (queue_name )
62- await queue ._consume (handler )
46+ await queue .consume (handler )
6347 await asyncio .Future ()
48+
49+
50+ def amqp_publisher_factory (
51+ url : typing .Text ,
52+ max_connection_pool_size : int = 2 ,
53+ max_channel_pool_size : int = 10 ,
54+ ) -> AMQPPublisher :
55+ max_connection_pool_size = max_connection_pool_size
56+ max_channel_pool_size = max_channel_pool_size
57+ connection_pool = pool .Pool (
58+ partial (connection_pool_factory , url = url ),
59+ max_size = max_connection_pool_size ,
60+ )
61+ channel_pool = pool .Pool (
62+ partial (channel_pool_factory , connection_pool = connection_pool ),
63+ max_size = max_channel_pool_size ,
64+ )
65+ return AMQPPublisher (channel_pool = channel_pool )
66+
67+
68+ def amqp_consumer_factory (
69+ url : typing .Text ,
70+ max_connection_pool_size : int = 2 ,
71+ max_channel_pool_size : int = 10 ,
72+ ) -> AMQPConsumer :
73+ max_connection_pool_size = max_connection_pool_size
74+ max_channel_pool_size = max_channel_pool_size
75+ connection_pool = pool .Pool (
76+ partial (connection_pool_factory , url = url ),
77+ max_size = max_connection_pool_size ,
78+ )
79+ channel_pool = pool .Pool (
80+ partial (channel_pool_factory , connection_pool = connection_pool ),
81+ max_size = max_channel_pool_size ,
82+ )
83+ return AMQPConsumer (channel_pool = channel_pool )
0 commit comments