Skip to content

Add ManagedChannelContainer to JsonToGrpcGatewayFilterFactory. #3120 #3122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import javax.net.ssl.SSLException;
Expand Down Expand Up @@ -182,9 +183,12 @@ class GRPCResponseDecorator extends ServerHttpResponseDecorator {

private final ObjectNode objectNode;

private final ConcurrentHashMap<String, ManagedChannel> managedChannelContainer;

GRPCResponseDecorator(ServerWebExchange exchange, Config config) {
super(exchange.getResponse());
this.exchange = exchange;
this.managedChannelContainer = new ConcurrentHashMap<>();
try {
Resource descriptorFile = resourceLoader.getResource(config.getProtoDescriptor());
Resource protoFile = resourceLoader.getResource(config.getProtoFile());
Expand Down Expand Up @@ -304,15 +308,17 @@ private Function<Object, DataBuffer> wrapGRPCResponse() {
};
}

// We are creating this on every call, should optimize?
private ManagedChannel createChannelChannel(String host, int port) {
NettyChannelBuilder nettyChannelBuilder = NettyChannelBuilder.forAddress(host, port);
try {
return grpcSslConfigurer.configureSsl(nettyChannelBuilder);
}
catch (SSLException e) {
throw new RuntimeException(e);
}
String key = host + ":" + port;
return managedChannelContainer.computeIfAbsent(key, k -> {
try {
NettyChannelBuilder nettyChannelBuilder = NettyChannelBuilder.forAddress(host, port);
return grpcSslConfigurer.configureSsl(nettyChannelBuilder);
}
catch (SSLException e) {
throw new RuntimeException(e);
}
});
}

}
Expand Down