Skip to content
Merged
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
@@ -1,44 +1,23 @@
package org.hypertrace.core.graphql.utils.grpc;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.hypertrace.core.graphql.spi.lifecycle.GraphQlServiceLifecycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
class DefaultGrpcChannelRegistry implements GrpcChannelRegistry {
private static final Logger LOG = LoggerFactory.getLogger(DefaultGrpcChannelRegistry.class);
private final Map<String, ManagedChannel> channelMap = new ConcurrentHashMap<>();
private volatile boolean isShutdown = false;

private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry delegate =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: style

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed this in the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what specifically about the style (this was formatted by spotless)? the FQN?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. never mind. i see it now.

new org.hypertrace.core.grpcutils.client.GrpcChannelRegistry();

@Inject
DefaultGrpcChannelRegistry(GraphQlServiceLifecycle serviceLifecycle) {
serviceLifecycle.shutdownCompletion().thenRun(this::shutdown);
serviceLifecycle.shutdownCompletion().thenRun(this.delegate::shutdown);
}

@Override
public ManagedChannel forAddress(String host, int port) {
assert !this.isShutdown;
String channelId = this.getChannelId(host, port);
return this.channelMap.computeIfAbsent(channelId, unused -> this.buildNewChannel(host, port));
}

private ManagedChannel buildNewChannel(String host, int port) {
LOG.info("Creating new channel for {}:{}", host, port);
return ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
}

private String getChannelId(String host, int port) {
return host + ":" + port;
}

private void shutdown() {
channelMap.values().forEach(ManagedChannel::shutdown);
this.isShutdown = true;
return this.delegate.forPlaintextAddress(host, port);
}
}