Skip to content

Commit 7df21a9

Browse files
fix: attempting to fix race for channels on startup (#53)
1 parent 3cc26e6 commit 7df21a9

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

hypertrace-core-graphql-grpc-utils/src/main/java/org/hypertrace/core/graphql/utils/grpc/DefaultGrpcChannelRegistry.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import io.grpc.ManagedChannel;
44
import io.grpc.ManagedChannelBuilder;
5-
import java.util.HashMap;
65
import java.util.Map;
6+
import java.util.concurrent.ConcurrentHashMap;
77
import javax.inject.Inject;
88
import javax.inject.Singleton;
99
import org.hypertrace.core.graphql.spi.lifecycle.GraphQlServiceLifecycle;
@@ -13,7 +13,7 @@
1313
@Singleton
1414
class DefaultGrpcChannelRegistry implements GrpcChannelRegistry {
1515
private static final Logger LOG = LoggerFactory.getLogger(DefaultGrpcChannelRegistry.class);
16-
private final Map<String, ManagedChannel> channelMap = new HashMap<>();
16+
private final Map<String, ManagedChannel> channelMap = new ConcurrentHashMap<>();
1717
private volatile boolean isShutdown = false;
1818

1919
@Inject
@@ -25,10 +25,7 @@ class DefaultGrpcChannelRegistry implements GrpcChannelRegistry {
2525
public ManagedChannel forAddress(String host, int port) {
2626
assert !this.isShutdown;
2727
String channelId = this.getChannelId(host, port);
28-
if (!this.channelMap.containsKey(channelId)) {
29-
this.channelMap.put(channelId, this.buildNewChannel(host, port));
30-
}
31-
return this.channelMap.get(channelId);
28+
return this.channelMap.computeIfAbsent(channelId, unused -> this.buildNewChannel(host, port));
3229
}
3330

3431
private ManagedChannel buildNewChannel(String host, int port) {

0 commit comments

Comments
 (0)