Skip to content

Commit 80fc7a9

Browse files
authored
feat(channel-registry): expose channel config (#23)
1 parent 66295c1 commit 80fc7a9

File tree

8 files changed

+86
-19
lines changed

8 files changed

+86
-19
lines changed

grpc-client-rx-utils/build.gradle.kts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ plugins {
77

88
dependencies {
99
api("io.reactivex.rxjava3:rxjava:3.0.6")
10-
api("io.grpc:grpc-stub:1.40.0")
10+
api("io.grpc:grpc-stub:1.42.0")
1111
api(project(":grpc-context-utils"))
12-
implementation("io.grpc:grpc-context:1.40.0")
12+
implementation("io.grpc:grpc-context:1.42.0")
1313

14+
constraints {
15+
implementation("com.google.code.gson:gson:2.8.9") {
16+
because("https://snyk.io/vuln/SNYK-JAVA-COMGOOGLECODEGSON-1730327")
17+
}
18+
}
19+
1420
testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")
1521
testImplementation("org.mockito:mockito-core:3.12.1")
1622
testImplementation("org.mockito:mockito-junit-jupiter:3.12.1")
1723
}
1824

1925
tasks.test {
2026
useJUnitPlatform()
21-
}
27+
}

grpc-client-utils/build.gradle.kts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ plugins {
66
}
77

88
dependencies {
9-
api("io.grpc:grpc-context:1.40.0")
10-
api("io.grpc:grpc-api:1.40.0")
9+
api("io.grpc:grpc-context:1.42.0")
10+
api("io.grpc:grpc-api:1.42.0")
1111

1212
implementation(project(":grpc-context-utils"))
1313
implementation("org.slf4j:slf4j-api:1.7.30")
1414

15+
annotationProcessor("org.projectlombok:lombok:1.18.18")
16+
compileOnly("org.projectlombok:lombok:1.18.18")
17+
18+
constraints {
19+
implementation("com.google.code.gson:gson:2.8.9") {
20+
because("https://snyk.io/vuln/SNYK-JAVA-COMGOOGLECODEGSON-1730327")
21+
}
22+
}
23+
1524
testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")
1625
testImplementation("org.mockito:mockito-core:3.12.1")
1726
testImplementation("org.mockito:mockito-inline:3.12.1")
18-
testRuntimeOnly("io.grpc:grpc-netty:1.40.0")
27+
testRuntimeOnly("io.grpc:grpc-netty:1.42.0")
1928
}
2029

2130
tasks.test {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hypertrace.core.grpcutils.client;
2+
3+
import lombok.Builder;
4+
import lombok.Value;
5+
6+
@Value
7+
@Builder
8+
public class GrpcChannelConfig {
9+
Integer maxInboundMessageSize;
10+
}

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.grpc.ManagedChannel;
77
import io.grpc.ManagedChannelBuilder;
88
import java.util.Map;
9+
import java.util.Objects;
910
import java.util.concurrent.ConcurrentHashMap;
1011
import java.util.concurrent.TimeUnit;
1112
import org.slf4j.Logger;
@@ -21,36 +22,50 @@ public class GrpcChannelRegistry {
2122
*/
2223
@Deprecated
2324
public ManagedChannel forAddress(String host, int port) {
24-
return this.forPlaintextAddress(host, port);
25+
return this.forPlaintextAddress(host, port, GrpcChannelConfig.builder().build());
2526
}
2627

2728
public ManagedChannel forSecureAddress(String host, int port) {
29+
return forSecureAddress(host, port, GrpcChannelConfig.builder().build());
30+
}
31+
32+
public ManagedChannel forSecureAddress(String host, int port, GrpcChannelConfig config) {
2833
assert !this.isShutdown;
29-
String channelId = this.getChannelId(host, port, false);
34+
String channelId = this.getChannelId(host, port, false, config);
3035
return this.channelMap.computeIfAbsent(
31-
channelId, unused -> this.buildNewChannel(host, port, false));
36+
channelId, unused -> this.buildNewChannel(host, port, false, config));
3237
}
3338

3439
public ManagedChannel forPlaintextAddress(String host, int port) {
40+
return forPlaintextAddress(host, port, GrpcChannelConfig.builder().build());
41+
}
42+
43+
public ManagedChannel forPlaintextAddress(String host, int port, GrpcChannelConfig config) {
3544
assert !this.isShutdown;
36-
String channelId = this.getChannelId(host, port, true);
45+
String channelId = this.getChannelId(host, port, true, config);
3746
return this.channelMap.computeIfAbsent(
38-
channelId, unused -> this.buildNewChannel(host, port, true));
47+
channelId, unused -> this.buildNewChannel(host, port, true, config));
3948
}
4049

41-
private ManagedChannel buildNewChannel(String host, int port, boolean isPlaintext) {
42-
LOG.info("Creating new channel {}", this.getChannelId(host, port, isPlaintext));
50+
private ManagedChannel buildNewChannel(
51+
String host, int port, boolean isPlaintext, GrpcChannelConfig config) {
52+
LOG.info("Creating new channel {}", this.getChannelId(host, port, isPlaintext, config));
4353

4454
ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(host, port);
4555
if (isPlaintext) {
4656
builder.usePlaintext();
4757
}
58+
59+
if (config.getMaxInboundMessageSize() != null) {
60+
builder.maxInboundMessageSize(config.getMaxInboundMessageSize());
61+
}
4862
return builder.build();
4963
}
5064

51-
private String getChannelId(String host, int port, boolean isPlaintext) {
65+
private String getChannelId(
66+
String host, int port, boolean isPlaintext, GrpcChannelConfig config) {
5267
String securePrefix = isPlaintext ? "plaintext" : "secure";
53-
return securePrefix + ":" + host + ":" + port;
68+
return securePrefix + ":" + host + ":" + port + ":" + Objects.hash(config);
5469
}
5570

5671
/**

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ void reusesChannelsForDuplicateRequests() {
5050
assertNotSame(firstChannelSecure, this.channelRegistry.forSecureAddress("bar", 1000));
5151
}
5252

53+
@Test
54+
void setsMaxInboundMessageSizeConfig() {
55+
Channel channel =
56+
this.channelRegistry.forPlaintextAddress(
57+
"foo", 1000, GrpcChannelConfig.builder().maxInboundMessageSize(100).build());
58+
// same message size
59+
assertSame(
60+
channel,
61+
this.channelRegistry.forPlaintextAddress(
62+
"foo", 1000, GrpcChannelConfig.builder().maxInboundMessageSize(100).build()));
63+
// different message size
64+
assertNotSame(
65+
channel,
66+
this.channelRegistry.forPlaintextAddress(
67+
"foo", 1000, GrpcChannelConfig.builder().maxInboundMessageSize(200).build()));
68+
}
69+
5370
@SuppressWarnings("rawtypes")
5471
@Test
5572
void shutdownAllChannelsOnShutdown() throws InterruptedException {

grpc-context-utils/build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tasks.test {
1111

1212
dependencies {
1313
// grpc
14-
implementation("io.grpc:grpc-core:1.40.0")
14+
implementation("io.grpc:grpc-core:1.42.0")
1515

1616
implementation("com.auth0:java-jwt:3.14.0")
1717
implementation("com.auth0:jwks-rsa:0.17.0")
@@ -21,6 +21,12 @@ dependencies {
2121
implementation("org.slf4j:slf4j-api:1.7.30")
2222
// End Logging
2323

24+
constraints {
25+
implementation("com.google.code.gson:gson:2.8.9") {
26+
because("https://snyk.io/vuln/SNYK-JAVA-COMGOOGLECODEGSON-1730327")
27+
}
28+
}
29+
2430
testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")
2531
testImplementation("org.mockito:mockito-core:3.12.1")
2632
}

grpc-server-rx-utils/build.gradle.kts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77

88
dependencies {
99
api("io.reactivex.rxjava3:rxjava:3.0.6")
10-
api("io.grpc:grpc-stub:1.40.0")
10+
api("io.grpc:grpc-stub:1.42.0")
1111

1212
annotationProcessor("org.projectlombok:lombok:1.18.18")
1313
compileOnly("org.projectlombok:lombok:1.18.18")
@@ -18,6 +18,10 @@ dependencies {
1818
implementation("com.google.guava:guava:30.1-jre") {
1919
because("https://snyk.io/vuln/SNYK-JAVA-COMGOOGLEGUAVA-1015415")
2020
}
21+
implementation("com.google.code.gson:gson:2.8.9") {
22+
because("https://snyk.io/vuln/SNYK-JAVA-COMGOOGLECODEGSON-1730327")
23+
}
24+
2125
}
2226
testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")
2327
testImplementation("org.mockito:mockito-core:3.12.1")

grpc-server-utils/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ tasks.test {
1010
}
1111

1212
dependencies {
13-
api("io.grpc:grpc-context:1.40.0")
14-
api("io.grpc:grpc-api:1.40.0")
13+
api("io.grpc:grpc-context:1.42.0")
14+
api("io.grpc:grpc-api:1.42.0")
1515

1616
implementation(project(":grpc-context-utils"))
1717
implementation("org.slf4j:slf4j-api:1.7.30")

0 commit comments

Comments
 (0)