Skip to content

Commit

Permalink
⚡ perf: use persistant aiohttp session
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeris1One committed Nov 14, 2023
1 parent 21a5a41 commit 5a7b49f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions plugins/monitoring/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, bot: Gunibot):
self.logger = core.setup_logger(self.file)
self.config = core.config.get(self.file)
self.error_counter = 0
self.session = aiohttp.ClientSession()

async def cog_load(self) -> None:
if self.config["monitoring_enabled"]:
Expand All @@ -50,22 +51,21 @@ async def ping_monitoring(self):
"?status=up&msg=OK&ping=" + str(ping))

# send request
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status != 200:
self.logger.error("Monitoring ping failed with status %s", resp.status)
return False
else:
json = await resp.json()
try:
if not json["ok"]:
self.logger.error("Monitoring ping failed with error : %s", json["msg"])
return False
else:
return True
except KeyError:
self.logger.error("Monitoring ping failed")
async with self.session.get(url) as resp:
if resp.status != 200:
self.logger.error("Monitoring ping failed with status %s", resp.status)
return False
else:
json = await resp.json()
try:
if not json["ok"]:
self.logger.error("Monitoring ping failed with error : %s", json["msg"])
return False
else:
return True
except KeyError:
self.logger.error("Monitoring ping failed")
return False

@tasks.loop(seconds=20)
async def loop(self):
Expand Down

0 comments on commit 5a7b49f

Please sign in to comment.