Skip to content

Commit

Permalink
feat: adapt health check timeout algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
RobPasMue committed Nov 28, 2024
1 parent 7ab4a01 commit 954cd0a
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/ansys/geometry/core/connection/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,21 @@ def wait_until_healthy(channel: grpc.Channel, timeout: float):
t_max = time.time() + timeout
health_stub = health_pb2_grpc.HealthStub(channel)
request = health_pb2.HealthCheckRequest(service="")

t_out = 0.1
while time.time() < t_max:
try:
out = health_stub.Check(request, timeout=0.1)
out = health_stub.Check(request, timeout=t_out)
if out.status is health_pb2.HealthCheckResponse.SERVING:
break
except _InactiveRpcError:
# Duplicate timeout and try again
t_now = time.time()
t_out *= 2
# If we have time to try again, continue.. but if we don't,
# just try for the remaining time
if t_now + t_out > t_max:
t_out = t_max - t_now
continue
else:
target_str = channel._channel.target().decode()
Expand Down Expand Up @@ -171,7 +180,8 @@ def __init__(
)

# do not finish initialization until channel is healthy
wait_until_healthy(self._channel, timeout)
self._grpc_health_timeout = timeout
wait_until_healthy(self._channel, self._grpc_health_timeout)

# once connection with the client is established, create a logger
self._log = LOG.add_instance_logger(
Expand Down Expand Up @@ -275,12 +285,10 @@ def healthy(self) -> bool:
"""Flag indicating whether the client channel is healthy."""
if self._closed:
return False
health_stub = health_pb2_grpc.HealthStub(self._channel)
request = health_pb2.HealthCheckRequest(service="")
try:
out = health_stub.Check(request, timeout=0.1)
return out.status is health_pb2.HealthCheckResponse.SERVING
except _InactiveRpcError: # pragma: no cover
wait_until_healthy(self._channel, self._grpc_health_timeout)
return True
except TimeoutError:
return False

def __repr__(self) -> str:
Expand Down

0 comments on commit 954cd0a

Please sign in to comment.