Skip to content

Commit c7e058b

Browse files
refactor: add support for client interceptors to registry (#34)
1 parent 7c48fc2 commit c7e058b

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.hypertrace.core.grpcutils.client;
22

3+
import io.grpc.ClientInterceptor;
4+
import java.util.List;
35
import lombok.Builder;
6+
import lombok.Singular;
47
import lombok.Value;
58

69
@Value
7-
@Builder
10+
@Builder(toBuilder = true)
811
public class GrpcChannelConfig {
912
Integer maxInboundMessageSize;
13+
14+
@Singular List<ClientInterceptor> clientInterceptors;
1015
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,20 @@ private ManagedChannel buildNewChannel(
6969
return this.configureAndBuildChannel(builder, config);
7070
}
7171

72-
ManagedChannelBuilder<?> getBuilderForAddress(String host, int port) {
72+
protected ManagedChannelBuilder<?> getBuilderForAddress(String host, int port) {
7373
return ManagedChannelBuilder.forAddress(host, port);
7474
}
7575

76-
ManagedChannel configureAndBuildChannel(
76+
protected ManagedChannel configureAndBuildChannel(
7777
ManagedChannelBuilder<?> builder, GrpcChannelConfig config) {
7878
if (config.getMaxInboundMessageSize() != null) {
7979
builder.maxInboundMessageSize(config.getMaxInboundMessageSize());
8080
}
81-
return builder.build();
81+
return builder.intercept(config.getClientInterceptors()).build();
8282
}
8383

84-
String getChannelId(String host, int port, boolean isPlaintext, GrpcChannelConfig config) {
84+
protected String getChannelId(
85+
String host, int port, boolean isPlaintext, GrpcChannelConfig config) {
8586
String securePrefix = isPlaintext ? "plaintext" : "secure";
8687
return securePrefix + ":" + host + ":" + port + ":" + Objects.hash(config);
8788
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ public ManagedChannel forName(String name, GrpcChannelConfig config) {
4040
this.getInProcessChannelId(name, config), () -> this.buildInProcessChannel(name, config));
4141
}
4242

43-
String getChannelId(String host, int port, boolean isPlaintext, GrpcChannelConfig config) {
43+
protected String getChannelId(
44+
String host, int port, boolean isPlaintext, GrpcChannelConfig config) {
4445
return this.getInProcessOverrideNameForHostAndPort(host, port)
4546
.map(name -> this.getInProcessChannelId(name, config))
4647
.orElseGet(() -> super.getChannelId(host, port, isPlaintext, config));
4748
}
4849

4950
@Override
50-
ManagedChannelBuilder<?> getBuilderForAddress(String host, int port) {
51+
protected ManagedChannelBuilder<?> getBuilderForAddress(String host, int port) {
5152
return this.getInProcessOverrideNameForHostAndPort(host, port)
5253
.<ManagedChannelBuilder<?>>map(InProcessChannelBuilder::forName)
5354
.orElseGet(() -> super.getBuilderForAddress(host, port));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertNotSame;
55
import static org.junit.jupiter.api.Assertions.assertSame;
66
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
import static org.mockito.Answers.RETURNS_SELF;
78
import static org.mockito.ArgumentMatchers.anyInt;
89
import static org.mockito.ArgumentMatchers.anyString;
910
import static org.mockito.Mockito.inOrder;
@@ -16,6 +17,7 @@
1617
import io.grpc.Deadline.Ticker;
1718
import io.grpc.ManagedChannel;
1819
import io.grpc.ManagedChannelBuilder;
20+
import java.util.List;
1921
import java.util.concurrent.TimeUnit;
2022
import org.junit.jupiter.api.BeforeEach;
2123
import org.junit.jupiter.api.Test;
@@ -79,6 +81,7 @@ void shutdownAllChannelsOnShutdown() throws InterruptedException {
7981
.thenAnswer(
8082
invocation -> {
8183
ManagedChannelBuilder<?> mockBuilder = mock(ManagedChannelBuilder.class);
84+
when(mockBuilder.intercept(List.of())).then(RETURNS_SELF);
8285
when(mockBuilder.build()).thenReturn(mock(ManagedChannel.class));
8386
return mockBuilder;
8487
});

0 commit comments

Comments
 (0)