Skip to content

Commit

Permalink
rpc: add support for request timeouts in metadata headers
Browse files Browse the repository at this point in the history
  • Loading branch information
mostroverkhov committed Mar 5, 2024
1 parent 093ffc7 commit 6197582
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions rsocket-messages/src/main/java/com/jauntsdn/rsocket/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@
public final class Headers {
public static int HEADER_LENGTH_MAX = 8192;

private static final Headers EMPTY = new Headers(false, Collections.emptyList(), 0);
private static final Headers DEFAULT_SERVICE = new Headers(true, Collections.emptyList(), 0);
private static final Headers EMPTY = new Headers(false, 0, Collections.emptyList(), 0);
private static final Headers DEFAULT_SERVICE = new Headers(true, 0, Collections.emptyList(), 0);

private final boolean isDefaultService;
private final int serializedSize;
private final long timeoutMillis;
private final List<String> keyValues;
private volatile ByteBuf cache;

private Headers(boolean isDefaultService, List<String> keyValues, int serializedSize) {
private Headers(
boolean isDefaultService, long timeoutMillis, List<String> keyValues, int serializedSize) {
this.isDefaultService = isDefaultService;
this.timeoutMillis = timeoutMillis;
this.keyValues = keyValues;
this.serializedSize = serializedSize;
}
Expand All @@ -52,6 +55,10 @@ public boolean isDefaultService() {
return isDefaultService;
}

public long timeoutMillis() {
return timeoutMillis;
}

public String header(String name) {
if (!isValidKeySize(name)) {
return null;
Expand Down Expand Up @@ -155,7 +162,7 @@ public static Headers create(boolean isDefaultService, String... headers) {
if (headers.length == 0) {
return isDefaultService ? DEFAULT_SERVICE : EMPTY;
}
return new Headers(isDefaultService, Arrays.asList(headers), serializedSize);
return new Headers(isDefaultService, 0, Arrays.asList(headers), serializedSize);
}

public static Headers empty() {
Expand All @@ -166,6 +173,14 @@ public static Headers withDefaultService() {
return DEFAULT_SERVICE;
}

public static Headers withTimeout(long timeoutMillis) {
requireNonNegative(timeoutMillis, "timeoutMillis");
if (timeoutMillis == 0) {
return EMPTY;
}
return new Headers(false, timeoutMillis, Collections.emptyList(), 0);
}

public static Headers.Builder newBuilder() {
return new Builder(4, Collections.emptyList());
}
Expand All @@ -179,7 +194,7 @@ static Headers create(List<String> headers) {
if (headers.isEmpty()) {
return EMPTY;
}
return new Headers(false, headers, serializedSize);
return new Headers(false, 0, headers, serializedSize);
}

ByteBuf cache() {
Expand All @@ -203,6 +218,7 @@ public int serializedSize() {
public static final class Builder {
private final List<String> nameValues;
private boolean isDefaultService;
private long timeoutMillis;
private int serializedSize;

private Builder(int size, List<String> headers) {
Expand All @@ -225,6 +241,11 @@ public Builder defaultService(boolean isDefaultService) {
return this;
}

public Builder timeout(long timeoutMillis) {
this.timeoutMillis = requireNonNegative(timeoutMillis, "timeoutMillis");
return this;
}

public Builder add(String name, String value) {
requireValidKeySize(name, " name");
requireValidValueSize(value, " value");
Expand Down Expand Up @@ -278,7 +299,7 @@ public Builder remove(String name, String value) {
}

public Headers build() {
return new Headers(isDefaultService, nameValues, serializedSize);
return new Headers(isDefaultService, timeoutMillis, nameValues, serializedSize);
}
}

Expand Down Expand Up @@ -316,6 +337,13 @@ private static int requireValid(List<String> keyValues, String message) {
return size;
}

private static long requireNonNegative(long value, String message) {
if (value < 0) {
throw new IllegalArgumentException(message + " must be non-negative");
}
return value;
}

private static int requireValid(String[] keyValues, String message) {
Objects.requireNonNull(keyValues, "keyValues");
int length = keyValues.length;
Expand Down

0 comments on commit 6197582

Please sign in to comment.