Class: Nonnative::GRPCHealth

Inherits:
Object
  • Object
show all
Defined in:
lib/nonnative/grpc_health.rb

Overview

Client helper for the standard gRPC health checking protocol over an insecure plaintext channel. An empty or nil service name checks overall server health.

Constant Summary collapse

NETWORK_ERRORS =
[
  GRPC::BadStatus,
  GRPC::Core::CallError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, service:, timeout: 1) ⇒ GRPCHealth



16
17
18
19
20
21
# File 'lib/nonnative/grpc_health.rb', line 16

def initialize(host:, port:, service:, timeout: 1)
  @host = host
  @port = port
  @service = service.to_s
  @timeout = timeout
end

Instance Method Details

#check(deadline: Time.now + timeout) ⇒ Grpc::Health::V1::HealthCheckResponse

Calls the gRPC health check endpoint.

gRPC status and network failures are propagated to the caller.



29
30
31
# File 'lib/nonnative/grpc_health.rb', line 29

def check(deadline: Time.now + timeout)
  stub.check(request, deadline: deadline)
end

#endpointString

Returns the checked gRPC health endpoint for lifecycle diagnostics.



48
49
50
# File 'lib/nonnative/grpc_health.rb', line 48

def endpoint
  service.empty? ? "grpc://#{host}:#{port}" : "grpc://#{host}:#{port}/#{service}"
end

#serving?(deadline: Time.now + timeout) ⇒ Boolean

Returns true when the gRPC health endpoint reports SERVING.

Returns false for non-SERVING responses and gRPC status/network failures.



39
40
41
42
43
# File 'lib/nonnative/grpc_health.rb', line 39

def serving?(deadline: Time.now + timeout)
  check(deadline: deadline).status == :SERVING
rescue *NETWORK_ERRORS
  false
end