Skip to content

Commit

Permalink
Add lock for DLI API client
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Nov 24, 2023
1 parent a1456cd commit cf08621
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lvmnps/actor/commands/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def validate_nps(command: NPSCommand):
"""Checks that the NPS implements scripting."""

nps = command.actor.nps
print(nps.implementations.get("scripting"))

if nps.implementations.get("scripting", False) is False:
command.fail("Scripting not allowed for this NPS.")
return False
Expand Down
7 changes: 7 additions & 0 deletions src/lvmnps/nps/implementations/dli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import annotations

import asyncio
import warnings

import httpx
Expand Down Expand Up @@ -44,9 +45,13 @@ class APIClient:
def __post_init__(self):
self.client: httpx.AsyncClient | None = None

self.lock = asyncio.Lock()

async def __aenter__(self):
"""Yields a new client."""

await self.lock.acquire()

log.debug(f"Creating async client to {self.base_url!r} with digest.")

auth = httpx.DigestAuth(self.user, self.password.get_secret_value())
Expand All @@ -61,6 +66,8 @@ async def __aenter__(self):
async def __aexit__(self, exc_type, exc, tb):
"""Closes the client."""

self.lock.release()

if self.client and not self.client.is_closed:
log.debug("Closing async client.")
await self.client.aclose()
Expand Down

0 comments on commit cf08621

Please sign in to comment.