Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nexus-sdk/src/main/java/io/nexusrpc/handler/Handler.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Link> links = new ArrayList<>();
private final Instant deadline;

private OperationContext(
String service,
String operation,
Map<String, String> 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. */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -160,6 +172,7 @@ public static class Builder {
private @Nullable String operation;
private final SortedMap<String, String> headers;
private @Nullable OperationMethodCanceller methodCanceller;
private @Nullable Instant deadline;

private Builder() {
headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Expand Down Expand Up @@ -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");
Expand All @@ -216,7 +235,8 @@ public OperationContext build() {
service,
operation,
Collections.unmodifiableMap(new TreeMap<>(normalizedHeaders)),
methodCanceller);
methodCanceller,
deadline);
}
}
}
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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());
}
}
Loading