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

Returns a new instance of GRPCHealth.

Parameters:

  • host (String)

    gRPC server host

  • port (Integer)

    gRPC server port

  • service (String, nil)

    gRPC health service name, or empty/nil for overall server health

  • timeout (Numeric) (defaults to: 1)

    default call timeout in seconds



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.

Parameters:

  • deadline (Time) (defaults to: Time.now + timeout)

    gRPC deadline

Returns:

  • (Grpc::Health::V1::HealthCheckResponse)


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.

Returns:

  • (String)


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.

Parameters:

  • deadline (Time) (defaults to: Time.now + timeout)

    gRPC deadline

Returns:

  • (Boolean)


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