refactor: 빌드 로직 개선, gradle 파일 경량화

This commit is contained in:
donghyeon-ka
2026-09-18 15:22:30 +09:00
parent df3c7838b6
commit f412efff73
8 changed files with 176 additions and 96 deletions
@@ -13,6 +13,7 @@ import dev.caskeleton.messaging.api.publish.RoutingOutcome;
import dev.caskeleton.messaging.policy.DestinationProfile;
import dev.caskeleton.messaging.policy.MessagingAdmissionController;
import dev.caskeleton.messaging.schema.EncodedMessage;
import dev.caskeleton.messaging.schema.EncodedMessagePublisher;
import dev.caskeleton.messaging.schema.MessageCodec;
import dev.caskeleton.messaging.schema.MessageCodecRegistry;
import dev.caskeleton.messaging.security.DestinationAccessPolicy;
@@ -52,7 +53,7 @@ import java.util.function.LongSupplier;
* cancellation. A permit or lease that leaks on the failure path is a limiter that shrinks by one
* per failure until it stops accepting anything.
*/
public final class DefaultMessagePublisher implements MessagePublisher {
public final class DefaultMessagePublisher implements MessagePublisher, EncodedMessagePublisher {
private final DestinationProfileRegistry destinations;
@@ -168,25 +169,53 @@ public final class DefaultMessagePublisher implements MessagePublisher {
profile = destinations.require(destination.name());
requireSupportedOptions(profile, options);
if (!access.mayPublish(destination.name())) {
// Before encoding: an unauthorized publish must not serialise the payload, because the
// encoded bytes are what a claim-check or a log would then be holding.
return rejected(
"PUBLISH_FORBIDDEN",
"this application may not publish to '" + destination.name().value() + '\'',
"this application may not publish to '" + destination.name().value() + "'",
startedAt);
}
encoded = encode(message);
} catch (RuntimeException beforeTheWire) {
// Nothing left this process, so the outcome is definite. Reporting it as ambiguous would send
// the caller into reconciliation for a message no broker ever saw.
return rejected("PUBLISH_PREPARATION_FAILED", sanitized(beforeTheWire), startedAt);
}
return sendEncoded(destination, profile, encoded, options, startedAt);
}
@Override
public CompletionStage<PublishResult> publishEncoded(
MessageDestination<EncodedMessage> destination,
MessageEnvelope<EncodedMessage> message,
PublishOptions options) {
Objects.requireNonNull(destination, "destination must not be null");
Objects.requireNonNull(message, "message must not be null");
Objects.requireNonNull(options, "options must not be null");
long startedAt = nanoTime.getAsLong();
DestinationProfile profile;
try {
profile = destinations.require(destination.name());
requireSupportedOptions(profile, options);
if (!access.mayPublish(destination.name())) {
return rejected(
"PUBLISH_FORBIDDEN",
"this application may not publish to '" + destination.name().value() + "'",
startedAt);
}
requireEncodedEnvelope(message);
} catch (RuntimeException beforeTheWire) {
return rejected("PUBLISH_PREPARATION_FAILED", sanitized(beforeTheWire), startedAt);
}
return sendEncoded(destination, profile, message, options, startedAt);
}
private CompletionStage<PublishResult> sendEncoded(
MessageDestination<?> destination,
DestinationProfile profile,
MessageEnvelope<EncodedMessage> encoded,
PublishOptions options,
long startedAt) {
Duration remaining = remainingBudget(options.timeout(), startedAt);
if (remaining.isZero() || remaining.isNegative()) {
// The budget was spent resolving and encoding, so nothing has been transmitted and the
// outcome is still definite. Sending anyway would start a message the caller has already
// stopped waiting for.
return rejected(
"PUBLISH_DEADLINE_EXCEEDED",
"the "
@@ -204,13 +233,9 @@ public final class DefaultMessagePublisher implements MessagePublisher {
transport.publish(new TransportPublishRequest(profile, encoded, options)), remaining)
.<PublishResult>handle(
(result, failure) -> {
// One release per acquisition, whatever happened. A permit that leaks on the
// failure path is a limiter that shrinks by one per failure until it accepts
// nothing.
held.close();
admission.complete(destination.name().value());
if (failure != null) {
// The request was on the wire when this failed, so the broker may hold it.
PublishResult ambiguous =
isDeadline(failure)
? ambiguousResult(
@@ -236,6 +261,13 @@ public final class DefaultMessagePublisher implements MessagePublisher {
}
}
private static void requireEncodedEnvelope(MessageEnvelope<EncodedMessage> message) {
if (!message.contentType().equals(message.payload().contentType())) {
throw new IllegalArgumentException(
"pre-encoded envelope content type must match its encoded payload content type");
}
}
/**
* Refuses options the destination cannot honour, before anything is encoded or sent.
*
@@ -26,6 +26,7 @@ import dev.caskeleton.messaging.policy.MessagingAdmissionController;
import dev.caskeleton.messaging.policy.PayloadLimitGuard;
import dev.caskeleton.messaging.policy.PayloadPolicy;
import dev.caskeleton.messaging.schema.EncodedMessage;
import dev.caskeleton.messaging.schema.EncodedMessagePublisher;
import dev.caskeleton.messaging.schema.MessageCodec;
import dev.caskeleton.messaging.schema.MessageCodecRegistry;
import dev.caskeleton.messaging.security.DestinationAccessPolicy;
@@ -85,6 +86,39 @@ class DefaultMessagePublisherTest {
.isZero();
}
@Test
@DisplayName("a pre-encoded publish skips codecs but still runs admission, runtime and transport")
void aPreEncodedPublishPreservesWireBytesWithoutBypassingThePipeline() {
SingleCodecRegistry codecs = new SingleCodecRegistry();
DefaultMessagePublisher publisher =
new DefaultMessagePublisher(
registry(),
allowOrders(),
codecs,
admission,
new FixedRuntimeRegistry(transport),
transport);
byte[] wireBytes = "{\"already\":\"encoded\"}".getBytes(StandardCharsets.UTF_8);
MessageEnvelope<EncodedMessage> encoded =
envelope().withPayload(new EncodedMessage(wireBytes, ContentType.JSON, Optional.empty()));
PublishResult result =
((EncodedMessagePublisher) publisher)
.publishEncoded(
new MessageDestination<>(
ORDERS, new MessageType("order.created"), EncodedMessage.class),
encoded,
PublishOptions.defaults())
.toCompletableFuture()
.join();
assertThat(result.completion()).isEqualTo(PublishCompletion.CONFIRMED);
assertThat(codecs.encodes()).isZero();
assertThat(transport.lastRequest().envelope().payload().bytes()).containsExactly(wireBytes);
assertThat(transport.lastRequest().envelope().payload().contentType()).isEqualTo(ContentType.JSON);
assertThat(admission.inFlight()).isZero();
}
@Test
@DisplayName("an unregistered destination is rejected before anything is encoded")
void anUnregisteredDestinationIsRejected() {
@@ -431,6 +465,8 @@ class DefaultMessagePublisherTest {
private boolean stalled;
private TransportPublishRequest lastRequest;
private void failWith(RuntimeException failure) {
this.failure = failure;
}
@@ -442,6 +478,7 @@ class DefaultMessagePublisherTest {
@Override
public CompletionStage<TransportPublishResult> publish(TransportPublishRequest request) {
this.lastRequest = request;
if (failure != null) {
return CompletableFuture.failedFuture(failure);
}
@@ -488,6 +525,10 @@ class DefaultMessagePublisherTest {
private int published() {
return published.get();
}
private TransportPublishRequest lastRequest() {
return lastRequest;
}
}
/** A registry with no installed generation, which is what a rotation gap looks like. */