From 9c2c7f7c99af305c05e9c5e2758e137404ba9707 Mon Sep 17 00:00:00 2001 From: Quinn Klassen Date: Mon, 18 Aug 2025 08:05:58 -0700 Subject: [PATCH] Add deadline to context --- .../java/io/nexusrpc/handler/Handler.java | 2 +- .../handler/HandlerResultContent.java | 3 +-- .../io/nexusrpc/handler/OperationContext.java | 24 +++++++++++++++++-- .../handler/OperationFetchInfoDetails.java | 3 +-- .../handler/OperationContextTest.java | 15 +++++++++++- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/nexus-sdk/src/main/java/io/nexusrpc/handler/Handler.java b/nexus-sdk/src/main/java/io/nexusrpc/handler/Handler.java index 5d9595c..041fb60 100644 --- a/nexus-sdk/src/main/java/io/nexusrpc/handler/Handler.java +++ b/nexus-sdk/src/main/java/io/nexusrpc/handler/Handler.java @@ -1,9 +1,9 @@ package io.nexusrpc.handler; +import io.nexusrpc.Experimental; import io.nexusrpc.OperationException; import io.nexusrpc.OperationInfo; import io.nexusrpc.OperationStillRunningException; -import io.nexusrpc.Experimental; /** Top-level handler for service calls. */ public interface Handler { diff --git a/nexus-sdk/src/main/java/io/nexusrpc/handler/HandlerResultContent.java b/nexus-sdk/src/main/java/io/nexusrpc/handler/HandlerResultContent.java index ca84e96..b9ca2cb 100644 --- a/nexus-sdk/src/main/java/io/nexusrpc/handler/HandlerResultContent.java +++ b/nexus-sdk/src/main/java/io/nexusrpc/handler/HandlerResultContent.java @@ -1,10 +1,9 @@ package io.nexusrpc.handler; +import io.nexusrpc.Experimental; import java.io.InputStream; import java.util.*; import java.util.stream.Collectors; - -import io.nexusrpc.Experimental; import org.jspecify.annotations.Nullable; /** Content that can be fixed or streaming as a result of an operation. */ diff --git a/nexus-sdk/src/main/java/io/nexusrpc/handler/OperationContext.java b/nexus-sdk/src/main/java/io/nexusrpc/handler/OperationContext.java index 13a8180..3628e07 100644 --- a/nexus-sdk/src/main/java/io/nexusrpc/handler/OperationContext.java +++ b/nexus-sdk/src/main/java/io/nexusrpc/handler/OperationContext.java @@ -1,6 +1,7 @@ package io.nexusrpc.handler; import io.nexusrpc.Link; +import java.time.Instant; import java.util.*; import java.util.stream.Collectors; import org.jspecify.annotations.Nullable; @@ -23,16 +24,19 @@ public static Builder newBuilder(OperationContext context) { // This is not included in equals, hashCode, or toString private final @Nullable OperationMethodCanceller methodCanceller; private final List links = new ArrayList<>(); + private final Instant deadline; private OperationContext( String service, String operation, Map headers, - @Nullable OperationMethodCanceller methodCanceller) { + @Nullable OperationMethodCanceller methodCanceller, + Instant deadline) { this.service = service; this.operation = operation; this.headers = headers; this.methodCanceller = methodCanceller; + this.deadline = deadline; } /** Service name for the call. */ @@ -71,6 +75,14 @@ public boolean isMethodCancelled() { return methodCanceller == null ? null : methodCanceller.getCancellationReason(); } + /** + * Get the deadline for the operation handler method. This is the time by which the method should + * complete. This is not the operation's deadline. + */ + public @Nullable Instant getDeadline() { + return deadline; + } + /** * Add a listener for method cancellation. This will be invoked immediately before this function * returns if the method is already cancelled. The listener must not block. This is not reentrant @@ -160,6 +172,7 @@ public static class Builder { private @Nullable String operation; private final SortedMap headers; private @Nullable OperationMethodCanceller methodCanceller; + private @Nullable Instant deadline; private Builder() { headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); @@ -200,6 +213,12 @@ public Builder setMethodCanceller(OperationMethodCanceller methodCanceller) { return this; } + /** Set the deadline for the operation handler method. */ + public Builder setDeadline(Instant deadline) { + this.deadline = deadline; + return this; + } + /** Build the context. */ public OperationContext build() { Objects.requireNonNull(service, "Service required"); @@ -216,7 +235,8 @@ public OperationContext build() { service, operation, Collections.unmodifiableMap(new TreeMap<>(normalizedHeaders)), - methodCanceller); + methodCanceller, + deadline); } } } diff --git a/nexus-sdk/src/main/java/io/nexusrpc/handler/OperationFetchInfoDetails.java b/nexus-sdk/src/main/java/io/nexusrpc/handler/OperationFetchInfoDetails.java index 5fa72d4..f0703ab 100644 --- a/nexus-sdk/src/main/java/io/nexusrpc/handler/OperationFetchInfoDetails.java +++ b/nexus-sdk/src/main/java/io/nexusrpc/handler/OperationFetchInfoDetails.java @@ -1,8 +1,7 @@ package io.nexusrpc.handler; -import java.util.Objects; - import io.nexusrpc.Experimental; +import java.util.Objects; import org.jspecify.annotations.Nullable; /** Details for handling operation fetch info. */ diff --git a/nexus-sdk/src/test/java/io/nexusrpc/handler/OperationContextTest.java b/nexus-sdk/src/test/java/io/nexusrpc/handler/OperationContextTest.java index cf1cc78..95148ff 100644 --- a/nexus-sdk/src/test/java/io/nexusrpc/handler/OperationContextTest.java +++ b/nexus-sdk/src/test/java/io/nexusrpc/handler/OperationContextTest.java @@ -5,12 +5,13 @@ import io.nexusrpc.Link; import java.net.URI; import java.net.URISyntaxException; +import java.time.Instant; import java.util.Arrays; import org.junit.jupiter.api.Test; public class OperationContextTest { @Test - void LinkTest() throws URISyntaxException { + void linkTest() throws URISyntaxException { OperationContext octx = OperationContext.newBuilder().setService("service").setOperation("operation").build(); URI url = new URI("http://somepath?k=v"); @@ -21,4 +22,16 @@ void LinkTest() throws URISyntaxException { octx.setLinks(); assertEquals(octx.getLinks(), Arrays.asList()); } + + @Test + void deadlineTest() { + Instant deadline = Instant.now().plusMillis(1000); + OperationContext octx = + OperationContext.newBuilder() + .setService("service") + .setOperation("operation") + .setDeadline(deadline) + .build(); + assertEquals(deadline, octx.getDeadline()); + } }