aio-pika
aio-pika copied to clipboard
Add method delete for Pool
Hi, I met the problem, when I used the Pool class for channel, if in some one from them arise error, and it got back into pool, next time it will call exception 'ChannelInvalidStateError'. Then me need delete this channel from pool, for this need: custom context-manager, method delete. Suggest slightly modify Pool. What do you think about this?
Example delete:
class PoolChannelContextManager(Generic[T], AsyncContextManager):
__slots__ = "pool", "item"
def __init__(self, pool: Pool):
self.pool = pool
self.item = None
async def __aenter__(self) -> T:
self.item = await self.pool.get()
return self.item
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.item is None:
return
if self.item.is_closed:
self.pool.delete(self.item)
else:
await self.pool.put(self.item)