diff --git a/src/epplibwrapper/client.py b/src/epplibwrapper/client.py index bd1740ec9..8cdb85fad 100644 --- a/src/epplibwrapper/client.py +++ b/src/epplibwrapper/client.py @@ -17,7 +17,6 @@ from django.conf import settings from .cert import Cert, Key from .errors import LoginError, RegistryError -from .socket import Socket logger = logging.getLogger(__name__) @@ -64,11 +63,11 @@ class EPPLibWrapper: ) options = { # Pool size - "size": 10, + "size": settings.EPP_CONNECTION_POOL_SIZE, # Which errors the pool should look out for "exc_classes": (LoginError, RegistryError,), - # Should we ping the connection on occasion to keep it alive? - "keepalive": None, + # Occasionally pings the registry to keep the connection alive + "keepalive": settings.POOL_KEEP_ALIVE, } self._pool = EppConnectionPool(client=self._client, login=self._login, options=options) diff --git a/src/epplibwrapper/utility/pool.py b/src/epplibwrapper/utility/pool.py index 6d5ab5d58..2ad9f82c2 100644 --- a/src/epplibwrapper/utility/pool.py +++ b/src/epplibwrapper/utility/pool.py @@ -1,9 +1,13 @@ import logging from geventconnpool import ConnectionPool -from epplibwrapper import RegistryError -from epplibwrapper.errors import LoginError +from epplibwrapper.errors import RegistryError, LoginError from epplibwrapper.socket import Socket +try: + from epplib.commands import Hello +except ImportError: + pass + logger = logging.getLogger(__name__) class EppConnectionPool(ConnectionPool): @@ -20,11 +24,17 @@ class EppConnectionPool(ConnectionPool): return connection except LoginError as err: message = "_new_connection failed to execute due to a registry login error." - logger.warning(message, exc_info=True) + logger.error(message, exc_info=True) raise RegistryError(message) from err - def _keepalive(self, connection): - pass + def _keepalive(self, c): + """Sends a command to the server to keep the connection alive.""" + try: + # Sends a ping to EPPLib + c.send(Hello()) + except Exception as err: + logger.error("Failed to keep the connection alive.", exc_info=True) + raise RegistryError("Failed to keep the connection alive.") from err def create_socket(self, client, login) -> Socket: """Creates and returns a socket instance""" diff --git a/src/registrar/config/settings.py b/src/registrar/config/settings.py index ceb215a4d..ee3b496bf 100644 --- a/src/registrar/config/settings.py +++ b/src/registrar/config/settings.py @@ -534,6 +534,16 @@ SECRET_REGISTRY_KEY = secret_registry_key SECRET_REGISTRY_KEY_PASSPHRASE = secret_registry_key_passphrase SECRET_REGISTRY_HOSTNAME = secret_registry_hostname +# endregion +# region: Registry Connection Pool----------------------------------------------------------### + +# Use this variable to set the size of our connection pool in client.py +# WARNING: Setting this value too high could cause frequent app crashes! +EPP_CONNECTION_POOL_SIZE = 10 + +# Determines if we should ping open connections +POOL_KEEP_ALIVE = True + # endregion # region: Security and Privacy----------------------------------------------###