Skip to content

Commit

Permalink
Fix/SK-1167 | Fix exception so it handles different errors better (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorvaladi authored Nov 13, 2024
1 parent bb4d40e commit a253cca
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions fedn/network/combiner/hooks/hook_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@ class CombinerHookInterface:

def __init__(self):
"""Initialize CombinerHookInterface client."""
try:
self.hook_service_host = os.getenv("HOOK_SERVICE_HOST", "hook:12081")
self.channel = grpc.insecure_channel(
self.hook_service_host,
options=[
("grpc.keepalive_time_ms", 30000), # 30 seconds ping interval
("grpc.keepalive_timeout_ms", 5000), # 5 seconds timeout for a response
("grpc.keepalive_permit_without_calls", 1), # allow keepalives even with no active calls
("grpc.enable_retries", 1), # automatic retries
("grpc.initial_reconnect_backoff_ms", 1000), # initial delay before retrying
("grpc.max_reconnect_backoff_ms", 5000), # maximum delay before retrying
],
)
self.stub = rpc.FunctionServiceStub(self.channel)
except Exception as e:
logger.warning(f"Failed to initialize connection to hooks container with error {e}")
self.hook_service_host = os.getenv("HOOK_SERVICE_HOST", "hook:12081")
self.channel = grpc.insecure_channel(
self.hook_service_host,
options=[
("grpc.keepalive_time_ms", 30000), # 30 seconds ping interval
("grpc.keepalive_timeout_ms", 5000), # 5 seconds timeout for a response
("grpc.keepalive_permit_without_calls", 1), # allow keepalives even with no active calls
("grpc.enable_retries", 1), # automatic retries
("grpc.initial_reconnect_backoff_ms", 1000), # initial delay before retrying
("grpc.max_reconnect_backoff_ms", 5000), # maximum delay before retrying
],
)
self.stub = rpc.FunctionServiceStub(self.channel)

def provided_functions(self, server_functions: str):
"""Communicates to hook container and asks which functions are available.
Expand All @@ -47,8 +44,14 @@ def provided_functions(self, server_functions: str):

response = self.stub.HandleProvidedFunctions(request)
return response.available_functions
except grpc.RpcError as rpc_error:
if rpc_error.code() == grpc.StatusCode.UNAVAILABLE:
logger.warning(f"Server-functions container is unavailable; using default implementations: {rpc_error}")
else:
logger.error(f"gRPC error: {rpc_error.code().name} - {rpc_error.details()}")
return {}
except Exception as e:
logger.warning(f"Was not able to communicate to hooks container due to: {e}")
logger.error(f"Unexpected error communicating with hooks container: {e}")
return {}

def client_settings(self, global_model) -> dict:
Expand Down

0 comments on commit a253cca

Please sign in to comment.