review-followup Wave 3 라운드 3(P19): 3-D 공통 유틸 — Sha256·HmacSha256·Backoff·MdcKeys·TenantId, caller 트랜잭션 guard 하나
- shared-contract `Sha256`(약 65벌 흡수, notification `MessageDigestPort` 삭제), `HmacSha256`(서명 헬퍼), `Backoff`("full jitter" 뜻 하나),
`MdcKeys`(gRPC·STOMP에도 상관관계), registry edge application-notification → shared-contract
- application-core `TenantId` 하나로 web·GraphQL tenant 문법 통일, web operation 경로에 tenant 해석(JWT `tenant_id`)
- persistence-jpa `CallerTransaction` 하나(복사본 4벌 + `JpaStreamScope` 흡수), 범위 없는 멱등성·outbox 리포지토리 메서드 제거(E-6b)
- 주입된 `Clock` 우회 제거(objectstorage·websocket·mongo·web stream), GraphQL 오류가 HTTP request id 사용, notification UUIDv7은 uuid-creator
- 호출처 없는 `WebRequestContext`·argument resolver 삭제
- 결함 수정: `WebStreamRegistry` 상한 경합(ORCH-new-3 — bin 하나만 잠그는 lock 안에서 크기 확인, 원자 카운터로 슬롯 예약)
- 현황표: 소유자 결정 33(persistence-notification·fileserver → persistence-jpa edge), 34(id 포트 domain 전환 보류), 35(X2-P2-6b 나머지 보류)
검증: ./gradlew check architectureCheck verifyDependencyLocks verifyPublicPathSnapshot :app-bootstrap:verifyEnvKeys configContractCheck
:app-bootstrap:integrationTest :sample-portfolio:integrationTest :sample-portfolio:systemTest graphql·websocket systemTest
persistence-jpa jpaPlatformContractTest·postgresql{Idempotency,Outbox*}IntegrationTest --continue 통과(695 task)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4f4bd1fd34
commit
09e2dd63d6
+8
-3
@@ -1,11 +1,13 @@
|
||||
package dev.caskeleton.adapter.inbound.websocket.stomp;
|
||||
|
||||
import dev.caskeleton.domain.stereotype.DomainEvent;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.event.EventListener;
|
||||
@@ -39,14 +41,17 @@ public class LiveEventStompBroadcaster {
|
||||
private final SimpMessagingTemplate messaging;
|
||||
private final WebSocketSettings properties;
|
||||
private final List<LiveEventProjector<?>> projectors;
|
||||
private final Clock clock;
|
||||
|
||||
public LiveEventStompBroadcaster(
|
||||
SimpMessagingTemplate messaging,
|
||||
WebSocketSettings properties,
|
||||
List<LiveEventProjector<?>> projectors) {
|
||||
List<LiveEventProjector<?>> projectors,
|
||||
Clock clock) {
|
||||
this.messaging = messaging;
|
||||
this.properties = properties;
|
||||
this.projectors = List.copyOf(projectors);
|
||||
this.clock = Objects.requireNonNull(clock, "clock");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,13 +95,13 @@ public class LiveEventStompBroadcaster {
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> LiveEvent project(LiveEventProjector<T> projector, Object event) {
|
||||
private <T> LiveEvent project(LiveEventProjector<T> projector, Object event) {
|
||||
String eventType = projector.eventType();
|
||||
Map<String, String> fields = projector.project(projector.sourceType().cast(event));
|
||||
if (!validEventType(eventType) || !validFields(fields)) {
|
||||
return null;
|
||||
}
|
||||
return new LiveEvent(eventType, fields, Instant.now());
|
||||
return new LiveEvent(eventType, fields, clock.instant());
|
||||
}
|
||||
|
||||
private static boolean validEventType(String eventType) {
|
||||
|
||||
+10
-3
@@ -1,6 +1,8 @@
|
||||
package dev.caskeleton.adapter.inbound.websocket.stomp;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.util.List;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
@@ -49,6 +51,7 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
private final SafeStompSubProtocolErrorHandler errorHandler =
|
||||
new SafeStompSubProtocolErrorHandler();
|
||||
private final WebSocketInboundAuthorizationInterceptor inboundAuthorization;
|
||||
private final WebSocketCorrelationInterceptor correlation = new WebSocketCorrelationInterceptor();
|
||||
|
||||
public WebSocketConfig(WebSocketSettings properties) {
|
||||
this.properties = properties;
|
||||
@@ -67,7 +70,8 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureClientInboundChannel(ChannelRegistration registration) {
|
||||
bounded(registration).interceptors(inboundAuthorization);
|
||||
// Correlation first, so an authorization refusal is logged with the frame's request id.
|
||||
bounded(registration).interceptors(correlation, inboundAuthorization);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -106,7 +110,10 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Bean
|
||||
LiveEventStompBroadcaster liveEventStompBroadcaster(
|
||||
SimpMessagingTemplate messaging, List<LiveEventProjector<?>> projectors) {
|
||||
return new LiveEventStompBroadcaster(messaging, properties, projectors);
|
||||
SimpMessagingTemplate messaging,
|
||||
List<LiveEventProjector<?>> projectors,
|
||||
ObjectProvider<Clock> clocks) {
|
||||
return new LiveEventStompBroadcaster(
|
||||
messaging, properties, projectors, clocks.getIfAvailable(Clock::systemUTC));
|
||||
}
|
||||
}
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package dev.caskeleton.adapter.inbound.websocket.stomp;
|
||||
|
||||
import dev.caskeleton.shared.observability.MdcKeys;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.support.ExecutorChannelInterceptor;
|
||||
|
||||
/**
|
||||
* Puts an inbound STOMP message's correlation on MDC under the keys the HTTP path uses (review
|
||||
* X2-P2-5).
|
||||
*
|
||||
* <p>Correlation used to stop at the servlet filter, so a log line written while a STOMP frame was
|
||||
* handled could not be joined to anything. Each inbound message gets its own {@code request_id},
|
||||
* minted here; {@code correlation_id} is the server-assigned session id, which joins every frame of
|
||||
* one connection. Neither is taken from a client header.
|
||||
*
|
||||
* <p>An {@link ExecutorChannelInterceptor} because the frames are handled on the inbound channel's
|
||||
* executor threads: the entries go on for the handling and the thread's previous MDC comes back
|
||||
* after it.
|
||||
*/
|
||||
final class WebSocketCorrelationInterceptor implements ExecutorChannelInterceptor {
|
||||
|
||||
private static final ThreadLocal<Map<String, String>> PREVIOUS = new ThreadLocal<>();
|
||||
|
||||
@Override
|
||||
public Message<?> beforeHandle(
|
||||
Message<?> message, MessageChannel channel, MessageHandler handler) {
|
||||
PREVIOUS.set(MDC.getCopyOfContextMap());
|
||||
MDC.put(MdcKeys.REQUEST_ID, UUID.randomUUID().toString());
|
||||
String session = SimpMessageHeaderAccessor.getSessionId(message.getHeaders());
|
||||
if (session != null) {
|
||||
MDC.put(MdcKeys.CORRELATION_ID, session);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterMessageHandled(
|
||||
Message<?> message, MessageChannel channel, MessageHandler handler, Exception failure) {
|
||||
Map<String, String> restored = PREVIOUS.get();
|
||||
PREVIOUS.remove();
|
||||
if (restored == null) {
|
||||
MDC.clear();
|
||||
} else {
|
||||
MDC.setContextMap(restored);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -11,6 +11,9 @@ import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
import dev.caskeleton.adapter.inbound.websocket.stomp.LiveEventStompBroadcaster.LiveEvent;
|
||||
import dev.caskeleton.domain.stereotype.DomainEvent;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -144,7 +147,8 @@ class LiveEventStompBroadcasterTest {
|
||||
}
|
||||
|
||||
private LiveEventStompBroadcaster broadcaster(List<LiveEventProjector<?>> projectors) {
|
||||
return new LiveEventStompBroadcaster(messaging, properties, projectors);
|
||||
return new LiveEventStompBroadcaster(
|
||||
messaging, properties, projectors, Clock.fixed(Instant.EPOCH, ZoneOffset.UTC));
|
||||
}
|
||||
|
||||
private static LiveEventProjector<SampleReserved> sampleProjector() {
|
||||
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package dev.caskeleton.adapter.inbound.websocket.stomp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import dev.caskeleton.shared.observability.MdcKeys;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.simp.stomp.StompCommand;
|
||||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
class WebSocketCorrelationInterceptorTest {
|
||||
|
||||
private final WebSocketCorrelationInterceptor interceptor = new WebSocketCorrelationInterceptor();
|
||||
|
||||
@AfterEach
|
||||
void clearMdc() {
|
||||
MDC.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("frame을 처리하는 동안 MDC에 request_id와 session id(correlation_id)가 있다")
|
||||
void aFrameIsHandledWithItsCorrelationOnMdc() {
|
||||
// when
|
||||
interceptor.beforeHandle(frame("session-1"), null, null);
|
||||
|
||||
// then
|
||||
assertThat(MDC.getCopyOfContextMap())
|
||||
.containsEntry(MdcKeys.CORRELATION_ID, "session-1")
|
||||
.containsKey(MdcKeys.REQUEST_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theThreadsMdcIsRestoredAfterTheFrame() {
|
||||
// given
|
||||
MDC.put("unrelated", "kept");
|
||||
Message<byte[]> frame = frame("session-1");
|
||||
interceptor.beforeHandle(frame, null, null);
|
||||
|
||||
// when
|
||||
interceptor.afterMessageHandled(frame, null, null, null);
|
||||
|
||||
// then
|
||||
assertThat(MDC.getCopyOfContextMap()).containsExactlyEntriesOf(Map.of("unrelated", "kept"));
|
||||
}
|
||||
|
||||
private static Message<byte[]> frame(String session) {
|
||||
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
|
||||
accessor.setSessionId(session);
|
||||
accessor.setDestination("/app/echo");
|
||||
return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user