Exemple #1
0
    async def __call__(self, scope: Scope, receive: Receive,
                       send: Send) -> None:
        if (scope['type'] == 'http'
                and self.db.config['use_connection_for_request']):
            scope['connection'] = await self.db.acquire(lazy=True)
            await self.app(scope, receive, send)
            conn = scope.pop('connection', None)
            if conn is not None:
                await conn.release()
            return

        if scope['type'] == 'lifespan':

            async def receiver() -> Message:
                message = await receive()
                if message["type"] == "lifespan.startup":
                    await self.db.set_bind(
                        self.db.config['dsn'],
                        echo=self.db.config['echo'],
                        min_size=self.db.config['min_size'],
                        max_size=self.db.config['max_size'],
                        ssl=self.db.config['ssl'],
                        **self.db.config['kwargs'],
                    )
                elif message["type"] == "lifespan.shutdown":
                    await self.db.pop_bind().close()
                return message

            await self.app(scope, receiver, send)
            return

        await self.app(scope, receive, send)
Exemple #2
0
    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        if scope["type"] == "http" and self.db.config["use_connection_for_request"]:
            scope["connection"] = await self.db.acquire(lazy=True)
            try:
                await self.app(scope, receive, send)
            finally:
                conn = scope.pop("connection", None)
                if conn is not None:
                    await conn.release()
            return

        if scope["type"] == "lifespan":

            async def receiver() -> Message:
                message = await receive()
                if message["type"] == "lifespan.startup":
                    await self.db.set_bind(
                        self.db.config["dsn"],
                        echo=self.db.config["echo"],
                        min_size=self.db.config["min_size"],
                        max_size=self.db.config["max_size"],
                        ssl=self.db.config["ssl"],
                        **self.db.config["kwargs"],
                    )
                elif message["type"] == "lifespan.shutdown":
                    await self.db.pop_bind().close()
                return message

            await self.app(scope, receiver, send)
            return

        await self.app(scope, receive, send)
Exemple #3
0
    async def __call__(
        self, scope: Scope, receive: Receive, send: Send
    ) -> None:
        if scope["type"] == "http" and self._conn_for_req:
            scope["connection"] = await self.db.acquire(lazy=True)
            try:
                await self.app(scope, receive, send)
            finally:
                conn = scope.pop("connection", None)
                if conn is not None:
                    await conn.release()
            return

        await self.app(scope, receive, send)