mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-19 02:49:21 +02:00
Add semaphore + add error handling for .close
This commit is contained in:
parent
058bbd7ab6
commit
ee60be8b10
1 changed files with 23 additions and 4 deletions
|
@ -1,6 +1,7 @@
|
||||||
"""Provide a wrapper around epplib to handle authentication and errors."""
|
"""Provide a wrapper around epplib to handle authentication and errors."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from gevent.lock import BoundedSemaphore
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from epplib.client import Client
|
from epplib.client import Client
|
||||||
|
@ -52,6 +53,9 @@ class EPPLibWrapper:
|
||||||
"urn:ietf:params:xml:ns:contact-1.0",
|
"urn:ietf:params:xml:ns:contact-1.0",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
# We should only ever have one active connection at a time,
|
||||||
|
# given that
|
||||||
|
self.connection_lock = BoundedSemaphore(1)
|
||||||
try:
|
try:
|
||||||
self._initialize_client()
|
self._initialize_client()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -91,12 +95,23 @@ class EPPLibWrapper:
|
||||||
raise RegistryError(message) from err
|
raise RegistryError(message) from err
|
||||||
|
|
||||||
def _disconnect(self) -> None:
|
def _disconnect(self) -> None:
|
||||||
"""Close the connection."""
|
"""Close the connection. Sends a logout command and closes the connection."""
|
||||||
|
self._send_logout_command()
|
||||||
|
self._close_client()
|
||||||
|
|
||||||
|
def _send_logout_command(self):
|
||||||
|
"""Sends a logout command to epp"""
|
||||||
try:
|
try:
|
||||||
self._client.send(commands.Logout()) # type: ignore
|
self._client.send(commands.Logout()) # type: ignore
|
||||||
self._client.close() # type: ignore
|
except Exception as err:
|
||||||
except Exception:
|
logger.warning(f"Logout command not sent successfully: {err}")
|
||||||
logger.warning("Connection to registry was not cleanly closed.")
|
|
||||||
|
def _close_client(self):
|
||||||
|
"""Closes an active client connection"""
|
||||||
|
try:
|
||||||
|
self._client.close()
|
||||||
|
except Exception as err:
|
||||||
|
logger.warning(f"Connection to registry was not cleanly closed: {err}")
|
||||||
|
|
||||||
def _send(self, command):
|
def _send(self, command):
|
||||||
"""Helper function used by `send`."""
|
"""Helper function used by `send`."""
|
||||||
|
@ -146,6 +161,8 @@ class EPPLibWrapper:
|
||||||
cmd_type = command.__class__.__name__
|
cmd_type = command.__class__.__name__
|
||||||
if not cleaned:
|
if not cleaned:
|
||||||
raise ValueError("Please sanitize user input before sending it.")
|
raise ValueError("Please sanitize user input before sending it.")
|
||||||
|
|
||||||
|
self.connection_lock.acquire()
|
||||||
try:
|
try:
|
||||||
return self._send(command)
|
return self._send(command)
|
||||||
except RegistryError as err:
|
except RegistryError as err:
|
||||||
|
@ -161,6 +178,8 @@ class EPPLibWrapper:
|
||||||
return self._retry(command)
|
return self._retry(command)
|
||||||
else:
|
else:
|
||||||
raise err
|
raise err
|
||||||
|
finally:
|
||||||
|
self.connection_lock.release()
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue