Search in sources :

Example 1 with Health

use of io.servicetalk.health.v1.Health in project servicetalk by apple.

the class HealthClientExample method main.

public static void main(String... args) throws Exception {
    final String serviceName = "World";
    try (BlockingGreeterClient client = GrpcClients.forAddress("localhost", 8080).buildBlocking(new Greeter.ClientFactory());
        BlockingHealthClient healthClient = GrpcClients.forAddress("localhost", 8080).buildBlocking(new Health.ClientFactory())) {
        // Check health before
        checkHealth(healthClient, serviceName);
        HelloReply reply = client.sayHello(HelloRequest.newBuilder().setName("World").build());
        System.out.println("HelloReply=" + reply.getMessage());
        // Check the health after to observe it changed.
        checkHealth(healthClient, serviceName);
    }
}
Also used : Health(io.servicetalk.health.v1.Health) Greeter(io.grpc.examples.health.Greeter) BlockingGreeterClient(io.grpc.examples.health.Greeter.BlockingGreeterClient) HelloReply(io.grpc.examples.health.HelloReply) BlockingHealthClient(io.servicetalk.health.v1.Health.BlockingHealthClient)

Example 2 with Health

use of io.servicetalk.health.v1.Health in project servicetalk by apple.

the class HealthClientExample method checkHealth.

private static void checkHealth(BlockingHealthClient healthClient, String serviceName) throws Exception {
    try {
        HealthCheckResponse response = healthClient.check(HealthCheckRequest.newBuilder().setService(serviceName).build());
        System.out.println("Service '" + serviceName + "' health=" + response.getStatus());
    } catch (GrpcStatusException e) {
        System.out.println("Service '" + serviceName + "' health exception=" + e);
    }
}
Also used : GrpcStatusException(io.servicetalk.grpc.api.GrpcStatusException) HealthCheckResponse(io.servicetalk.health.v1.HealthCheckResponse)

Example 3 with Health

use of io.servicetalk.health.v1.Health in project servicetalk by apple.

the class DefaultHealthService method setStatus.

/**
 * Updates the status of the server.
 * @param service the name of some aspect of the server that is associated with a health status.
 * This name can have no relation with the gRPC services that the server is running with.
 * It can also be an empty String {@code ""} per the gRPC specification.
 * @param status is one of the values {@link ServingStatus#SERVING}, {@link ServingStatus#NOT_SERVING},
 * and {@link ServingStatus#UNKNOWN}.
 * @return {@code true} if this change was applied. {@code false} if it was not due to {@link #terminate()}.
 */
public boolean setStatus(String service, ServingStatus status) {
    final HealthCheckResponse resp;
    final HealthValue healthValue;
    lock.lock();
    try {
        if (terminated) {
            return false;
        }
        resp = newBuilder().setStatus(status).build();
        healthValue = serviceToStatusMap.computeIfAbsent(service, __ -> new HealthValue(resp));
    } finally {
        lock.unlock();
    }
    healthValue.next(resp);
    return true;
}
Also used : HealthCheckRequest(io.servicetalk.health.v1.HealthCheckRequest) Processors.newPublisherProcessorDropHeadOnOverflow(io.servicetalk.concurrent.api.Processors.newPublisherProcessorDropHeadOnOverflow) Health(io.servicetalk.health.v1.Health) ReentrantLock(java.util.concurrent.locks.ReentrantLock) FAILED_PRECONDITION(io.servicetalk.grpc.api.GrpcStatusCode.FAILED_PRECONDITION) HealthCheckResponse(io.servicetalk.health.v1.HealthCheckResponse) NOT_SERVING(io.servicetalk.health.v1.HealthCheckResponse.ServingStatus.NOT_SERVING) Publisher(io.servicetalk.concurrent.api.Publisher) Predicate(java.util.function.Predicate) Processor(io.servicetalk.concurrent.PublisherSource.Processor) Single(io.servicetalk.concurrent.api.Single) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GrpcStatus(io.servicetalk.grpc.api.GrpcStatus) GrpcStatusCode(io.servicetalk.grpc.api.GrpcStatusCode) NOT_FOUND(io.servicetalk.grpc.api.GrpcStatusCode.NOT_FOUND) GrpcServiceContext(io.servicetalk.grpc.api.GrpcServiceContext) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Lock(java.util.concurrent.locks.Lock) SERVICE_UNKNOWN(io.servicetalk.health.v1.HealthCheckResponse.ServingStatus.SERVICE_UNKNOWN) ServingStatus(io.servicetalk.health.v1.HealthCheckResponse.ServingStatus) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SERVING(io.servicetalk.health.v1.HealthCheckResponse.ServingStatus.SERVING) HealthCheckResponse.newBuilder(io.servicetalk.health.v1.HealthCheckResponse.newBuilder) HealthCheckResponse(io.servicetalk.health.v1.HealthCheckResponse)

Aggregations

Health (io.servicetalk.health.v1.Health)2 HealthCheckResponse (io.servicetalk.health.v1.HealthCheckResponse)2 Greeter (io.grpc.examples.health.Greeter)1 BlockingGreeterClient (io.grpc.examples.health.Greeter.BlockingGreeterClient)1 HelloReply (io.grpc.examples.health.HelloReply)1 Processor (io.servicetalk.concurrent.PublisherSource.Processor)1 Processors.newPublisherProcessorDropHeadOnOverflow (io.servicetalk.concurrent.api.Processors.newPublisherProcessorDropHeadOnOverflow)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 Single (io.servicetalk.concurrent.api.Single)1 SourceAdapters.fromSource (io.servicetalk.concurrent.api.SourceAdapters.fromSource)1 GrpcServiceContext (io.servicetalk.grpc.api.GrpcServiceContext)1 GrpcStatus (io.servicetalk.grpc.api.GrpcStatus)1 GrpcStatusCode (io.servicetalk.grpc.api.GrpcStatusCode)1 FAILED_PRECONDITION (io.servicetalk.grpc.api.GrpcStatusCode.FAILED_PRECONDITION)1 NOT_FOUND (io.servicetalk.grpc.api.GrpcStatusCode.NOT_FOUND)1 GrpcStatusException (io.servicetalk.grpc.api.GrpcStatusException)1 BlockingHealthClient (io.servicetalk.health.v1.Health.BlockingHealthClient)1 HealthCheckRequest (io.servicetalk.health.v1.HealthCheckRequest)1 ServingStatus (io.servicetalk.health.v1.HealthCheckResponse.ServingStatus)1 NOT_SERVING (io.servicetalk.health.v1.HealthCheckResponse.ServingStatus.NOT_SERVING)1