Skip to content

Commit

Permalink
WIP: writing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Bair <[email protected]>
  • Loading branch information
rbair23 committed Aug 21, 2024
1 parent 66066c7 commit f330c4f
Show file tree
Hide file tree
Showing 16 changed files with 535 additions and 313 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.hedera.pbj.grpc.helidon;

import edu.umd.cs.findbugs.annotations.NonNull;
import io.helidon.http.Header;
import io.helidon.http.Status;
import io.helidon.http.WritableHeaders;
import io.helidon.http.http2.Http2Headers;
import java.util.function.Consumer;

class FatalGrpcException extends Exception {
final Consumer<WritableHeaders<?>> headerCallback;

FatalGrpcException(final @NonNull Consumer<WritableHeaders<?>> headerCallback) {
this.headerCallback = headerCallback;
}

FatalGrpcException(final @NonNull Header grpcStatus) {
this(w -> {
w.set(Http2Headers.STATUS_NAME, Status.OK_200.code());
w.set(grpcStatus);
});
}

FatalGrpcException(final @NonNull Status status) {
this(w -> w.set(Http2Headers.STATUS_NAME, status.code()));
}

FatalGrpcException(final @NonNull Status status, final @NonNull Header grpcStatus) {
this(w -> {
w.set(Http2Headers.STATUS_NAME, status.code());
w.set(grpcStatus);
});
}

final Consumer<WritableHeaders<?>> headerCallback() {
return headerCallback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ abstract class BuilderBase<BUILDER extends BuilderBase<BUILDER, PROTOTYPE>, PROT
private Config config;
private int maxMessageSize = 10240;
private int maxResponseBufferSize = 10240;
private int maxIncomingBufferedMessages = 10;
private String name;

/**
Expand All @@ -101,6 +102,7 @@ protected BuilderBase() {
public BUILDER from(PbjConfig prototype) {
maxMessageSize(prototype.maxMessageSize());
maxResponseBufferSize(prototype.maxResponseBufferSize());
maxIncomingBufferedMessages(prototype.maxIncomingBufferedMessages());
name(prototype.name());
return self();
}
Expand All @@ -114,6 +116,7 @@ public BUILDER from(PbjConfig prototype) {
public BUILDER from(BuilderBase<?, ?> builder) {
maxMessageSize(builder.maxMessageSize());
maxResponseBufferSize(builder.maxResponseBufferSize());
maxIncomingBufferedMessages(builder.maxIncomingBufferedMessages());
builder.name().ifPresent(this::name);
return self();
}
Expand All @@ -131,6 +134,7 @@ public BUILDER config(Config config) {
this.config = config;
config.get("max-message-size").as(Integer.class).ifPresent(this::maxMessageSize);
config.get("max-response-buffer-size").as(Integer.class).ifPresent(this::maxResponseBufferSize);
config.get("max-incoming-buffered-messages").as(Integer.class).ifPresent(this::maxIncomingBufferedMessages);
return self();
}

Expand Down Expand Up @@ -160,6 +164,19 @@ public BUILDER maxResponseBufferSize(int maxResponseBufferSize) {
return self();
}

/**
* The maximum number of messages to buffer coming from the client until we start applying back pressure.
* Defaults to {@value #DEFAULT_MAX_INCOMING_BUFFERED_MESSAGES}.
*
* @param maxIncomingBufferedMessages the maximum number of incoming messages to buffer
* @return updated builder instance
* @see #maxIncomingBufferedMessages()
*/
public BUILDER maxIncomingBufferedMessages(int maxIncomingBufferedMessages) {
this.maxIncomingBufferedMessages = maxIncomingBufferedMessages;
return self();
}

/**
*
*
Expand Down Expand Up @@ -193,6 +210,16 @@ public int maxResponseBufferSize() {
return maxResponseBufferSize;
}

/**
* The maximum number of messages to buffer coming from the client until we start applying back pressure.
* Defaults to {@value #DEFAULT_MAX_INCOMING_BUFFERED_MESSAGES}.
*
* @return the max incoming messages that can be buffered
*/
public int maxIncomingBufferedMessages() {
return maxIncomingBufferedMessages;
}

/**
*
*
Expand Down Expand Up @@ -244,6 +271,7 @@ protected static class PbjConfigImpl implements PbjConfig {

private final int maxMessageSize;
private final int maxResponseBufferSize;
private final int maxIncomingBufferedMessages;
private final String name;

/**
Expand All @@ -254,6 +282,7 @@ protected static class PbjConfigImpl implements PbjConfig {
protected PbjConfigImpl(BuilderBase<?, ?> builder) {
this.maxMessageSize = builder.maxMessageSize();
this.maxResponseBufferSize = builder.maxResponseBufferSize();
this.maxIncomingBufferedMessages = builder.maxIncomingBufferedMessages();
this.name = builder.name().get();
}

Expand All @@ -267,6 +296,11 @@ public int maxResponseBufferSize() {
return maxResponseBufferSize;
}

@Override
public int maxIncomingBufferedMessages() {
return maxIncomingBufferedMessages;
}

@Override
public String name() {
return name;
Expand All @@ -277,6 +311,7 @@ public String toString() {
return "PbjConfig{"
+ "maxMessageSize=" + maxMessageSize + ","
+ "maxResponseBufferSize=" + maxResponseBufferSize + ","
+ "maxIncomingBufferedMessages=" + maxIncomingBufferedMessages + ","
+ "name=" + name
+ "}";
}
Expand All @@ -291,12 +326,13 @@ public boolean equals(Object o) {
}
return maxMessageSize == other.maxMessageSize()
&& maxResponseBufferSize == other.maxResponseBufferSize()
&& maxIncomingBufferedMessages == other.maxIncomingBufferedMessages()
&& Objects.equals(name, other.name());
}

@Override
public int hashCode() {
return Objects.hash(maxMessageSize, maxResponseBufferSize, name);
return Objects.hash(maxMessageSize, maxResponseBufferSize, maxIncomingBufferedMessages, name);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
@Prototype.Configured
@Prototype.Provides(ProtocolConfig.class)
interface PbjConfigBlueprint extends ProtocolConfig {
/**
* Default maximum number of messages to buffer coming from the client until we start applying back pressure.
*
* @see #maxIncomingBufferedMessages()
*/
int DEFAULT_MAX_INCOMING_BUFFERED_MESSAGES = 10;

/**
* Default maximum message size in bytes ({@value}).
*
Expand Down Expand Up @@ -51,4 +58,10 @@ interface PbjConfigBlueprint extends ProtocolConfig {
default String type() {
return PbjProtocolProvider.CONFIG_NAME;
}

/**
* The maximum number of messages to buffer coming from the client until we start applying back pressure.
* Defaults to {@value #DEFAULT_MAX_INCOMING_BUFFERED_MESSAGES}.
*/
int maxIncomingBufferedMessages();
}
Loading

0 comments on commit f330c4f

Please sign in to comment.