Avoid fetching time in the klap send success path

On success we did expensive log message formatting and threw it away
This commit is contained in:
J. Nick Koston 2024-01-18 07:44:51 -10:00
parent 642e9a1f5b
commit 7d754be952
No known key found for this signature in database

View File

@ -296,6 +296,16 @@ class KlapTransport(BaseTransport):
or self._session_expire_at - time.time() <= 0 or self._session_expire_at - time.time() <= 0
) )
def _generate_send_log_message(
self, seq: int, response_status: int, request: str
) -> str:
"""Generate a log message for a send."""
return (
f"at {datetime.datetime.now()}. Host is {self._host}, "
+ f"Sequence is {seq}, "
+ f"Response status is {response_status}, Request was {request}"
)
async def send(self, request: str): async def send(self, request: str):
"""Send the request.""" """Send the request."""
if not self._handshake_done or self._handshake_session_expired(): if not self._handshake_done or self._handshake_session_expired():
@ -314,13 +324,11 @@ class KlapTransport(BaseTransport):
cookies_dict=self._session_cookie, cookies_dict=self._session_cookie,
) )
msg = (
f"at {datetime.datetime.now()}. Host is {self._host}, "
+ f"Sequence is {seq}, "
+ f"Response status is {response_status}, Request was {request}"
)
if response_status != 200: if response_status != 200:
_LOGGER.error("Query failed after succesful authentication " + msg) _LOGGER.error(
"Query failed after successful authentication "
+ self._generate_send_log_message(seq, response_status, request)
)
# If we failed with a security error, force a new handshake next time. # If we failed with a security error, force a new handshake next time.
if response_status == 403: if response_status == 403:
self._handshake_done = False self._handshake_done = False
@ -334,7 +342,12 @@ class KlapTransport(BaseTransport):
+ f"request with seq {seq}" + f"request with seq {seq}"
) )
else: else:
_LOGGER.debug("Query posted " + msg) debug_enabled = _LOGGER.isEnabledFor(logging.DEBUG)
if debug_enabled:
_LOGGER.debug(
"Query posted "
+ self._generate_send_log_message(seq, response_status, request)
)
# Check for mypy # Check for mypy
if self._encryption_session is not None: if self._encryption_session is not None:
@ -342,11 +355,12 @@ class KlapTransport(BaseTransport):
json_payload = json_loads(decrypted_response) json_payload = json_loads(decrypted_response)
_LOGGER.debug( if debug_enabled:
"%s << %s", _LOGGER.debug(
self._host, "%s << %s",
_LOGGER.isEnabledFor(logging.DEBUG) and pf(json_payload), self._host,
) pf(json_payload),
)
return json_payload return json_payload