diff --git a/grpc-advanced/CLAUDE.md b/grpc-advanced/CLAUDE.md index 3955a52..12b53b2 100644 --- a/grpc-advanced/CLAUDE.md +++ b/grpc-advanced/CLAUDE.md @@ -18,6 +18,12 @@ grpc:* → grpc-advanced:* (금지 — registry가 거부한다) `grpc-spring-boot-starter`의 registry 엔트리에는 어떤 advanced id도 없다. `verifyCleanArchitectureDependencies`가 build time에 이 규칙을 강제한다. runtime 검사는 없다. +### 공용 다이제스트 헬퍼를 복사해 두는 이유 (review 2026-11-01 01-11) + +`grpc-advanced`(`GrpcDiagnosticsRedactor`)는 SHA-256 계산을 자기 안에 둔다. 의존 방향이 +`grpc-advanced:* → grpc:*`로 닫혀 있고 `shared-contract` edge가 없는 것은 의도다. `grpc/CLAUDE.md`의 +같은 절과 이유가 같다(vendored 플랫폼, MSG-023 §6.2와 같은 배치). + ## Capability grade와 feature flag capability마다 별도 flag를 갖는다. 하나의 "advanced" 스위치로 묶지 않는 이유는, gRPC-Web을 켜는 diff --git a/grpc/CLAUDE.md b/grpc/CLAUDE.md index ada4282..19e78b3 100644 --- a/grpc/CLAUDE.md +++ b/grpc/CLAUDE.md @@ -41,6 +41,15 @@ server profile·named channel profile·method size profile·method policy 기본 `caskeleton.grpc.common.v1.generated` — 손으로 쓴 `dev.caskeleton`과 겹치지 않는다). 템플릿을 재사용할 때 바꾸는 방법은 `AGENTS.md` 「템플릿 재사용 체크리스트」에 있다. +### 공용 다이제스트 헬퍼를 복사해 두는 이유 (review 2026-11-01 01-11) + +`grpc-runtime`(`GrpcRequestFingerprint`, `GrpcResumeTokenCodec`)은 SHA-256·HMAC-SHA256 계산을 자기 +안에 둔다. 템플릿의 `shared-contract` `digest/Sha256`·`HmacSha256`을 쓰지 않는 것은 의도다. 이 +family는 optional build에 있는 vendored 플랫폼이고 registry edge는 `grpc:*` 안에서만 닫힌다(위 표). +`shared-contract` edge를 주면 플랫폼이 템플릿 계약 leaf에 묶여 따로 떼어 쓸 수 없게 된다 — messaging +family가 같은 이유로 고른 배치(MSG-023 §6.2)와 같다. 복사본을 고칠 때는 `shared-contract`의 정본과 +같은 규칙인지 함께 본다. + ## 증거 등급 — testkit이 하나의 leaf인 이유 원본 계획은 testkit을 core/in-process/netty/fault 4개 모듈로 쪼갠다. 목적은 "in-process 결과를 diff --git a/grpc/grpc-runtime/src/main/java/dev/caskeleton/grpc/server/GrpcAdmissionController.java b/grpc/grpc-runtime/src/main/java/dev/caskeleton/grpc/server/GrpcAdmissionController.java index a6b346c..b0aa8e3 100644 --- a/grpc/grpc-runtime/src/main/java/dev/caskeleton/grpc/server/GrpcAdmissionController.java +++ b/grpc/grpc-runtime/src/main/java/dev/caskeleton/grpc/server/GrpcAdmissionController.java @@ -14,7 +14,10 @@ public final class GrpcAdmissionController { private final int maxQueuedCalls; // One lock for both counters: every decision reads one and changes the other, and a check on an - // atomic followed by an increment lets racing callers all pass the same check. + // atomic followed by an increment lets racing callers all pass the same check. A private object + // rather than `synchronized` methods: the controller is a shared bean, and a method that locks + // `this` waits on a monitor any holder of the reference can take, stalling every admission. + private final Object lock = new Object(); private int inFlight; private int queued; private int rejected; @@ -49,54 +52,66 @@ public final class GrpcAdmissionController { } /** Admits a call, queues it, or refuses it. */ - public synchronized Decision tryAdmit() { - if (inFlight < maxConcurrentCalls) { - inFlight++; - return new Decision(true, inFlight, queued, "within the concurrency bound"); + public Decision tryAdmit() { + synchronized (lock) { + if (inFlight < maxConcurrentCalls) { + inFlight++; + return new Decision(true, inFlight, queued, "within the concurrency bound"); + } + if (queued < maxQueuedCalls) { + queued++; + return new Decision(true, inFlight, queued, "queued within the admission bound"); + } + rejected++; + return new Decision( + false, + inFlight, + queued, + "at capacity: " + + inFlight + + " in flight and " + + queued + + " queued. Refusing is better than accepting work whose caller will have given up."); } - if (queued < maxQueuedCalls) { - queued++; - return new Decision(true, inFlight, queued, "queued within the admission bound"); - } - rejected++; - return new Decision( - false, - inFlight, - queued, - "at capacity: " - + inFlight - + " in flight and " - + queued - + " queued. Refusing is better than accepting work whose caller will have given up."); } /** Moves a queued call into execution. */ - public synchronized void promoteFromQueue() { - if (queued > 0) { - queued--; - inFlight++; + public void promoteFromQueue() { + synchronized (lock) { + if (queued > 0) { + queued--; + inFlight++; + } } } /** Releases a finished call. */ - public synchronized void release() { - if (inFlight > 0) { - inFlight--; + public void release() { + synchronized (lock) { + if (inFlight > 0) { + inFlight--; + } } } /** How many calls are executing. */ - public synchronized int inFlight() { - return inFlight; + public int inFlight() { + synchronized (lock) { + return inFlight; + } } /** How many calls are waiting. */ - public synchronized int queued() { - return queued; + public int queued() { + synchronized (lock) { + return queued; + } } /** How many calls have been refused. */ - public synchronized int rejected() { - return rejected; + public int rejected() { + synchronized (lock) { + return rejected; + } } } diff --git a/grpc/grpc-runtime/src/test/java/dev/caskeleton/grpc/server/GrpcAdmissionControllerTest.java b/grpc/grpc-runtime/src/test/java/dev/caskeleton/grpc/server/GrpcAdmissionControllerTest.java index 895fdf5..34b9260 100644 --- a/grpc/grpc-runtime/src/test/java/dev/caskeleton/grpc/server/GrpcAdmissionControllerTest.java +++ b/grpc/grpc-runtime/src/test/java/dev/caskeleton/grpc/server/GrpcAdmissionControllerTest.java @@ -8,6 +8,7 @@ import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; class GrpcAdmissionControllerTest { @@ -24,6 +25,27 @@ class GrpcAdmissionControllerTest { assertThat(mostAdmitted).isEqualTo(2); } + @Test + void codeHoldingTheControllersOwnMonitorDoesNotStallAdmission() throws Exception { + // given + GrpcAdmissionController controller = new GrpcAdmissionController(1, 1); + ExecutorService caller = Executors.newSingleThreadExecutor(); + Future admitted; + try { + // The controller is a shared bean: anything that holds a reference can lock it. A decision + // that waits on that monitor stalls every call the server is trying to admit. + synchronized (controller) { + // when + admitted = caller.submit(() -> controller.tryAdmit().admitted()); + + // then + assertThat(admitted.get(5, TimeUnit.SECONDS)).isTrue(); + } + } finally { + caller.shutdownNow(); + } + } + /** Races {@link #CALLERS} callers at a 1-running, 1-queued controller, round after round. */ private static int mostAdmittedOverRounds() throws Exception { ExecutorService pool = Executors.newFixedThreadPool(CALLERS);