Skip to content

Commit c372c51

Browse files
refactor: add registry support for secure channels (#18)
* refactor: add registry support for secure channels * docs: finish writing javadoc comment
1 parent a86db35 commit c372c51

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

grpc-client-utils/src/main/java/org/hypertrace/core/grpcutils/client/GrpcChannelRegistry.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,41 @@ public class GrpcChannelRegistry {
1212
private final Map<String, ManagedChannel> channelMap = new ConcurrentHashMap<>();
1313
private volatile boolean isShutdown = false;
1414

15+
/**
16+
* Use either {@link #forSecureAddress(String, int)} or {@link #forPlaintextAddress(String, int)}
17+
*/
18+
@Deprecated
1519
public ManagedChannel forAddress(String host, int port) {
20+
return this.forPlaintextAddress(host, port);
21+
}
22+
23+
public ManagedChannel forSecureAddress(String host, int port) {
24+
assert !this.isShutdown;
25+
String channelId = this.getChannelId(host, port, false);
26+
return this.channelMap.computeIfAbsent(
27+
channelId, unused -> this.buildNewChannel(host, port, false));
28+
}
29+
30+
public ManagedChannel forPlaintextAddress(String host, int port) {
1631
assert !this.isShutdown;
17-
String channelId = this.getChannelId(host, port);
18-
return this.channelMap.computeIfAbsent(channelId, unused -> this.buildNewChannel(host, port));
32+
String channelId = this.getChannelId(host, port, true);
33+
return this.channelMap.computeIfAbsent(
34+
channelId, unused -> this.buildNewChannel(host, port, true));
1935
}
2036

21-
private ManagedChannel buildNewChannel(String host, int port) {
22-
LOG.info("Creating new channel for {}:{}", host, port);
23-
return ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
37+
private ManagedChannel buildNewChannel(String host, int port, boolean isPlaintext) {
38+
LOG.info("Creating new channel {}", this.getChannelId(host, port, isPlaintext));
39+
40+
ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(host, port);
41+
if (isPlaintext) {
42+
builder.usePlaintext();
43+
}
44+
return builder.build();
2445
}
2546

26-
private String getChannelId(String host, int port) {
27-
return host + ":" + port;
47+
private String getChannelId(String host, int port, boolean isPlaintext) {
48+
String securePrefix = isPlaintext ? "plaintext" : "secure";
49+
return securePrefix + ":" + host + ":" + port;
2850
}
2951

3052
public void shutdown() {

grpc-client-utils/src/test/java/org/hypertrace/core/grpcutils/client/GrpcChannelRegistryTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,26 @@ void beforeEach() {
2323

2424
@Test
2525
void createsNewChannelsAsRequested() {
26-
assertNotNull(this.channelRegistry.forAddress("foo", 1000));
26+
assertNotNull(this.channelRegistry.forPlaintextAddress("foo", 1000));
2727
}
2828

2929
@Test
3030
void reusesChannelsForDuplicateRequests() {
31-
Channel firstChannel = this.channelRegistry.forAddress("foo", 1000);
32-
assertSame(firstChannel, this.channelRegistry.forAddress("foo", 1000));
33-
assertNotSame(firstChannel, this.channelRegistry.forAddress("foo", 1001));
34-
assertNotSame(firstChannel, this.channelRegistry.forAddress("bar", 1000));
31+
Channel firstChannel = this.channelRegistry.forPlaintextAddress("foo", 1000);
32+
assertSame(firstChannel, this.channelRegistry.forPlaintextAddress("foo", 1000));
33+
Channel firstChannelSecure = this.channelRegistry.forSecureAddress("foo", 1000);
34+
assertSame(firstChannelSecure, this.channelRegistry.forSecureAddress("foo", 1000));
35+
assertNotSame(firstChannel, firstChannelSecure);
36+
assertNotSame(firstChannel, this.channelRegistry.forPlaintextAddress("foo", 1001));
37+
assertNotSame(firstChannel, this.channelRegistry.forPlaintextAddress("foo", 1001));
38+
assertNotSame(firstChannelSecure, this.channelRegistry.forSecureAddress("bar", 1000));
39+
assertNotSame(firstChannelSecure, this.channelRegistry.forSecureAddress("bar", 1000));
3540
}
3641

3742
@Test
3843
void shutdownAllChannelsOnShutdown() {
39-
ManagedChannel firstChannel = this.channelRegistry.forAddress("foo", 1000);
40-
ManagedChannel secondChannel = this.channelRegistry.forAddress("foo", 1002);
44+
ManagedChannel firstChannel = this.channelRegistry.forPlaintextAddress("foo", 1000);
45+
ManagedChannel secondChannel = this.channelRegistry.forSecureAddress("foo", 1002);
4146
assertFalse(firstChannel.isShutdown());
4247
assertFalse(secondChannel.isShutdown());
4348
this.channelRegistry.shutdown();
@@ -48,6 +53,7 @@ void shutdownAllChannelsOnShutdown() {
4853
@Test
4954
void throwsIfNewChannelRequestedAfterShutdown() {
5055
this.channelRegistry.shutdown();
51-
assertThrows(AssertionError.class, () -> this.channelRegistry.forAddress("foo", 1000));
56+
assertThrows(AssertionError.class, () -> this.channelRegistry.forPlaintextAddress("foo", 1000));
57+
assertThrows(AssertionError.class, () -> this.channelRegistry.forSecureAddress("foo", 1000));
5258
}
5359
}

0 commit comments

Comments
 (0)