Skip to content

Commit

Permalink
Use suppress KeyError instead of except
Browse files Browse the repository at this point in the history
  • Loading branch information
Selutario committed Apr 29, 2022
1 parent 0019db2 commit ba87003
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions framework/wazuh/core/cluster/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2

import asyncio
import contextlib
import functools
import inspect
import itertools
Expand All @@ -19,8 +20,6 @@
from wazuh.core.cluster import common as c_common
from wazuh.core.cluster.utils import ClusterFilter, context_tag

NO_RESULT = 'no_result'


class AbstractServerHandler(c_common.Handler):
"""
Expand Down Expand Up @@ -192,12 +191,11 @@ def connection_lost(self, exc):
del self.server.clients[self.name]
for task in self.handler_tasks:
task.cancel()
elif exc is not None:
self.logger.error(f"Error during handshake with incoming connection: {exc}. \n"
f"{''.join(traceback.format_tb(exc.__traceback__))}", exc_info=False)
else:
if exc is not None:
self.logger.error(f"Error during handshake with incoming connection: {exc}. \n"
f"{''.join(traceback.format_tb(exc.__traceback__))}", exc_info=False)
else:
self.logger.error("Error during handshake with incoming connection.", exc_info=False)
self.logger.error("Error during handshake with incoming connection.", exc_info=False)

def add_request(self, broadcast_id, f, *args, **kwargs):
"""Add a request to the queue to execute a function in this server handler.
Expand Down Expand Up @@ -238,17 +236,17 @@ async def broadcast_reader(self):
self.logger.error(f"Error while broadcasting function. ID: {q_item['broadcast_id']}. Error: {e}.")
result = e

try:
with contextlib.suppress(KeyError):
self.server.broadcast_results[q_item['broadcast_id']][self.name] = result
except KeyError:
pass


class AbstractServer:
"""
Define an asynchronous server. Handle connections from all clients.
"""

NO_RESULT = 'no_result'

def __init__(self, performance_test: int, concurrency_test: int, configuration: Dict, cluster_items: Dict,
enable_ssl: bool, logger: logging.Logger = None, tag: str = "Abstract Server"):
"""Class constructor.
Expand Down Expand Up @@ -340,7 +338,7 @@ def broadcast_add(self, f, *args, **kwargs):

for name, client in self.clients.items():
try:
self.broadcast_results[broadcast_id][name] = NO_RESULT
self.broadcast_results[broadcast_id][name] = AbstractServer.NO_RESULT
client.add_request(broadcast_id, f, *args, **kwargs)
self.logger.debug2(f'Added broadcast request to execute "{f.__name__}" in {name}.')
except Exception as e:
Expand Down Expand Up @@ -375,7 +373,7 @@ def broadcast_pop(self, broadcast_id):
the results are ready, it is, the request was executed in all server handlers.
"""
for name, result in self.broadcast_results.get(broadcast_id, {}).items():
if name in self.clients and result == NO_RESULT:
if name in self.clients and result == AbstractServer.NO_RESULT:
return False

return self.broadcast_results.pop(broadcast_id, True)
Expand Down

0 comments on commit ba87003

Please sign in to comment.