Skip to content

Instantly share code, notes, and snippets.

@rponte
Created January 15, 2021 17:58
Show Gist options
  • Select an option

  • Save rponte/c0e97d7f539ebc144526bdcdc5ef4244 to your computer and use it in GitHub Desktop.

Select an option

Save rponte/c0e97d7f539ebc144526bdcdc5ef4244 to your computer and use it in GitHub Desktop.
GRPC: simple example of GRPC Health Checking Protocol
package br.com.zup.edu
import io.grpc.health.v1.HealthCheckRequest
import io.grpc.health.v1.HealthCheckResponse
import io.grpc.health.v1.HealthGrpc
import io.grpc.stub.StreamObserver
import javax.inject.Singleton
@Singleton
class HealthCheckerService: HealthGrpc.HealthImplBase() {
override fun check(request: HealthCheckRequest?, responseObserver: StreamObserver<HealthCheckResponse>?) {
responseObserver?.onNext(HealthCheckResponse.newBuilder()
.setStatus(HealthCheckResponse.ServingStatus.SERVING)
.build())
responseObserver?.onCompleted()
}
override fun watch(request: HealthCheckRequest?, responseObserver: StreamObserver<HealthCheckResponse>?) {
for (i in 0 until 10 ) {
val status = when (i % 2) {
0 -> HealthCheckResponse.ServingStatus.SERVING
else -> HealthCheckResponse.ServingStatus.NOT_SERVING
}
Thread.sleep(1000)
responseObserver?.onNext(HealthCheckResponse.newBuilder()
.setStatus(status)
.build())
}
responseObserver?.onCompleted()
}
}
@rponte
Copy link
Author

rponte commented Jan 15, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment