class HealthChecker
Performs health checks on endpoints. Called by LoadBalancer's loop. Runs within the caller's reactor; does not spawn tasks or reactors. Only HTTP health checks are supported; gRPC health checks return :unknown.
Definitions
def initialize(health_checks)
Initialize health checker
Signature
-
parameter
health_checksArray<Hash> Health check configurations from cluster
Implementation
def initialize(health_checks)
@health_checks = health_checks
@endpoints = []
@cache = {}
end
def update_endpoints(endpoints)
Update endpoints (cleans cache for removed endpoints)
Signature
-
parameter
endpointsArray<Async::HTTP::Endpoint> Current endpoints
Implementation
def update_endpoints(endpoints)
removed = @endpoints - endpoints
removed.each{|endpoint| @cache.delete(endpoint)}
@endpoints = endpoints
end
def check(endpoint)
Check health of endpoint. Runs in caller's reactor.
Signature
-
parameter
endpointAsync::HTTP::Endpoint Endpoint to check
-
returns
Symbol :healthy, :unhealthy, or :unknown
Implementation
def check(endpoint)
if cached = @cache[endpoint]
return cached[:status] if Time.now - cached[:time] < 5
end
status = perform_check(endpoint)
@cache[endpoint] = {status: status, time: Time.now}
status
end
def close
Close health checker
Implementation
def close
@cache.clear
end