Skip to content

Commit 461fe95

Browse files
refactor: use common channel registry (#77)
1 parent d941520 commit 461fe95

File tree

1 file changed

+5
-26
lines changed

1 file changed

+5
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,23 @@
11
package org.hypertrace.core.graphql.utils.grpc;
22

33
import io.grpc.ManagedChannel;
4-
import io.grpc.ManagedChannelBuilder;
5-
import java.util.Map;
6-
import java.util.concurrent.ConcurrentHashMap;
74
import javax.inject.Inject;
85
import javax.inject.Singleton;
96
import org.hypertrace.core.graphql.spi.lifecycle.GraphQlServiceLifecycle;
10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
127

138
@Singleton
149
class DefaultGrpcChannelRegistry implements GrpcChannelRegistry {
15-
private static final Logger LOG = LoggerFactory.getLogger(DefaultGrpcChannelRegistry.class);
16-
private final Map<String, ManagedChannel> channelMap = new ConcurrentHashMap<>();
17-
private volatile boolean isShutdown = false;
10+
11+
private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry delegate =
12+
new org.hypertrace.core.grpcutils.client.GrpcChannelRegistry();
1813

1914
@Inject
2015
DefaultGrpcChannelRegistry(GraphQlServiceLifecycle serviceLifecycle) {
21-
serviceLifecycle.shutdownCompletion().thenRun(this::shutdown);
16+
serviceLifecycle.shutdownCompletion().thenRun(this.delegate::shutdown);
2217
}
2318

2419
@Override
2520
public ManagedChannel forAddress(String host, int port) {
26-
assert !this.isShutdown;
27-
String channelId = this.getChannelId(host, port);
28-
return this.channelMap.computeIfAbsent(channelId, unused -> this.buildNewChannel(host, port));
29-
}
30-
31-
private ManagedChannel buildNewChannel(String host, int port) {
32-
LOG.info("Creating new channel for {}:{}", host, port);
33-
return ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
34-
}
35-
36-
private String getChannelId(String host, int port) {
37-
return host + ":" + port;
38-
}
39-
40-
private void shutdown() {
41-
channelMap.values().forEach(ManagedChannel::shutdown);
42-
this.isShutdown = true;
21+
return this.delegate.forPlaintextAddress(host, port);
4322
}
4423
}

0 commit comments

Comments
 (0)