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.

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)

    gRPC health service name

  • timeout (Numeric) (defaults to: 1)

    default call timeout in seconds



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

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.

Parameters:

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

    gRPC deadline

Returns:

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


26
27
28
# File 'lib/nonnative/grpc_health.rb', line 26

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

#endpointString

Returns the checked gRPC health endpoint for lifecycle diagnostics.

Returns:

  • (String)


43
44
45
# File 'lib/nonnative/grpc_health.rb', line 43

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.

Parameters:

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

    gRPC deadline

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/nonnative/grpc_health.rb', line 34

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