Skip to content

Commit 0c1c984

Browse files
committed
Fix: Shared HTTP client usage
1 parent 76711c6 commit 0c1c984

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

bbblb/api/bbbapi.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import hmac
55
import typing
66
import uuid
7-
import lxml.builder
87
import lxml.etree
98
import logging
109
from sqlalchemy import update

bbblb/api/bbblbapi.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,17 @@ async def trigger_callback(
124124
params: typing.Mapping[str, str] | None = None,
125125
data: bytes | typing.Mapping[str, str] | None = None,
126126
):
127-
for i in range(config.WEBHOOK_RETRY):
128-
try:
129-
async with bbblib.HTTP.request(method, url, params=params, data=data) as rs:
130-
rs.raise_for_status()
131-
except aiohttp.ClientError:
132-
LOG.warning(
133-
f"Failed to forward callback {url} ({i + 1}/{config.WEBHOOK_RETRY})"
134-
)
135-
await asyncio.sleep(10 * i)
136-
continue
127+
async with await bbblib.get_client() as client:
128+
for i in range(config.WEBHOOK_RETRY):
129+
try:
130+
async with client.request(method, url, params=params, data=data) as rs:
131+
rs.raise_for_status()
132+
except aiohttp.ClientError:
133+
LOG.warning(
134+
f"Failed to forward callback {url} ({i + 1}/{config.WEBHOOK_RETRY})"
135+
)
136+
await asyncio.sleep(10 * i)
137+
continue
137138

138139

139140
async def fire_callback(callback: model.Callback, payload: dict, clear=True):

bbblb/bbblib.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ async def get_pool():
2727
return CONNPOOL
2828

2929

30+
async def get_client():
31+
return aiohttp.ClientSession(connector=await get_pool(), connector_owner=False)
32+
33+
3034
async def close_pool():
3135
if CONNPOOL and not CONNPOOL.closed:
3236
await CONNPOOL.close()
@@ -87,9 +91,7 @@ async def get_session(self):
8791
# Hint: Closing a session does nothing if it does not own the connector,
8892
# so we do not need to close it.
8993
if not self.session or self.session.closed:
90-
self.session = aiohttp.ClientSession(
91-
connector=await get_pool(), connector_owner=False
92-
)
94+
self.session = await get_client()
9395
return self.session
9496

9597
def encode_uri(self, endpoint: str, query: dict[str, str]):

0 commit comments

Comments
 (0)