|
| 1 | +/* |
| 2 | + * Copyright 2025 LY Corporation |
| 3 | + * |
| 4 | + * LY Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package com.linecorp.armeria.internal.client.grpc; |
| 17 | + |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.locks.ReentrantLock; |
| 20 | + |
| 21 | +import com.google.common.annotations.VisibleForTesting; |
| 22 | + |
| 23 | +import com.linecorp.armeria.client.ClientRequestContext; |
| 24 | +import com.linecorp.armeria.client.ClientRequestContextCaptor; |
| 25 | +import com.linecorp.armeria.client.Clients; |
| 26 | +import com.linecorp.armeria.client.Endpoint; |
| 27 | +import com.linecorp.armeria.client.endpoint.healthcheck.HealthCheckerContext; |
| 28 | +import com.linecorp.armeria.client.grpc.GrpcClients; |
| 29 | +import com.linecorp.armeria.common.ResponseHeaders; |
| 30 | +import com.linecorp.armeria.common.SessionProtocol; |
| 31 | +import com.linecorp.armeria.common.annotation.Nullable; |
| 32 | +import com.linecorp.armeria.common.util.AsyncCloseable; |
| 33 | +import com.linecorp.armeria.common.util.AsyncCloseableSupport; |
| 34 | +import com.linecorp.armeria.internal.common.util.ReentrantShortLock; |
| 35 | + |
| 36 | +import io.grpc.health.v1.HealthCheckRequest; |
| 37 | +import io.grpc.health.v1.HealthCheckResponse; |
| 38 | +import io.grpc.health.v1.HealthGrpc; |
| 39 | +import io.grpc.stub.StreamObserver; |
| 40 | + |
| 41 | +public final class GrpcHealthChecker implements AsyncCloseable { |
| 42 | + |
| 43 | + static final double HEALTHY = 1d; |
| 44 | + static final double UNHEALTHY = 0d; |
| 45 | + |
| 46 | + private final HealthCheckerContext ctx; |
| 47 | + @Nullable private final String service; |
| 48 | + private final HealthGrpc.HealthStub stub; |
| 49 | + |
| 50 | + private final ReentrantLock lock = new ReentrantShortLock(); |
| 51 | + private final AsyncCloseableSupport closeable = AsyncCloseableSupport.of(this::closeAsync); |
| 52 | + |
| 53 | + public GrpcHealthChecker(HealthCheckerContext ctx, Endpoint endpoint, SessionProtocol sessionProtocol, |
| 54 | + @Nullable String service) { |
| 55 | + this.ctx = ctx; |
| 56 | + this.service = service; |
| 57 | + |
| 58 | + this.stub = GrpcClients.builder(sessionProtocol, endpoint) |
| 59 | + .options(ctx.clientOptions()) |
| 60 | + .build(HealthGrpc.HealthStub.class); |
| 61 | + } |
| 62 | + |
| 63 | + public void start() { |
| 64 | + check(); |
| 65 | + } |
| 66 | + |
| 67 | + @VisibleForTesting |
| 68 | + void check() { |
| 69 | + lock(); |
| 70 | + try { |
| 71 | + final HealthCheckRequest.Builder builder = HealthCheckRequest.newBuilder(); |
| 72 | + if (this.service != null) { |
| 73 | + builder.setService(service); |
| 74 | + } |
| 75 | + |
| 76 | + try (ClientRequestContextCaptor reqCtxCaptor = Clients.newContextCaptor()) { |
| 77 | + stub.check(builder.build(), new StreamObserver<HealthCheckResponse>() { |
| 78 | + @Override |
| 79 | + public void onNext(HealthCheckResponse healthCheckResponse) { |
| 80 | + final ClientRequestContext reqCtx = reqCtxCaptor.get(); |
| 81 | + if (healthCheckResponse.getStatus() == HealthCheckResponse.ServingStatus.SERVING) { |
| 82 | + ctx.updateHealth(HEALTHY, reqCtx, null, null); |
| 83 | + } else { |
| 84 | + ctx.updateHealth(UNHEALTHY, reqCtx, null, null); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onError(Throwable throwable) { |
| 90 | + final ClientRequestContext reqCtx = reqCtxCaptor.get(); |
| 91 | + ctx.updateHealth(UNHEALTHY, reqCtx, ResponseHeaders.of(500), throwable); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onCompleted() { |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + } finally { |
| 100 | + unlock(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public CompletableFuture<?> closeAsync() { |
| 106 | + return closeable.closeAsync(); |
| 107 | + } |
| 108 | + |
| 109 | + private synchronized void closeAsync(CompletableFuture<?> future) { |
| 110 | + future.complete(null); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void close() { |
| 115 | + closeable.close(); |
| 116 | + } |
| 117 | + |
| 118 | + private void lock() { |
| 119 | + lock.lock(); |
| 120 | + } |
| 121 | + |
| 122 | + private void unlock() { |
| 123 | + lock.unlock(); |
| 124 | + } |
| 125 | +} |
0 commit comments