aio-pika icon indicating copy to clipboard operation
aio-pika copied to clipboard

Add method delete for Pool

Open shizacat opened this issue 3 years ago • 0 comments

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)

shizacat avatar Dec 02 '21 07:12 shizacat