Skip to content

Commit

Permalink
fix: added default value for keepalive and keepalivetimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
mayankrai09 committed Dec 4, 2023
1 parent 14c20ee commit 3d6608c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class GrpcClient {

private ManagedChannel decoratedChannel;

private final long defaultKeepAliveTimeout = 20000L;

private final long defaultKeepAliveInterval = Long.MAX_VALUE;

/**
* Instantiates a new Grpc client.
*
Expand All @@ -47,12 +51,12 @@ public void addChannel() {
}

protected ManagedChannelBuilder<?> decorateManagedChannelBuilder(ManagedChannelBuilder<?> channelBuilder) {
if (StringUtils.isNotEmpty(grpcConfig.getGrpcArgKeepaliveTimeMs())) {
channelBuilder = channelBuilder.keepAliveTime(Long.parseLong(grpcConfig.getGrpcArgKeepaliveTimeMs()), TimeUnit.MILLISECONDS);
}
if (StringUtils.isNotEmpty(grpcConfig.getGrpcArgKeepaliveTimeoutMs())) {
channelBuilder = channelBuilder.keepAliveTimeout(Long.parseLong(grpcConfig.getGrpcArgKeepaliveTimeoutMs()), TimeUnit.MILLISECONDS);
}

long keepAliveInterval = StringUtils.isNotEmpty(grpcConfig.getGrpcArgKeepaliveTimeMs()) ? Long.parseLong(grpcConfig.getGrpcArgKeepaliveTimeMs()): defaultKeepAliveInterval;
long keepAliveTimeout = StringUtils.isNotEmpty(grpcConfig.getGrpcArgKeepaliveTimeoutMs()) ? Long.parseLong(grpcConfig.getGrpcArgKeepaliveTimeoutMs()): defaultKeepAliveTimeout;

channelBuilder = channelBuilder.keepAliveTime(keepAliveInterval, TimeUnit.MILLISECONDS).keepAliveTimeout(keepAliveTimeout, TimeUnit.MILLISECONDS);

if (grpcConfig.getHeaders() != null && !grpcConfig.getHeaders().isEmpty()) {
Metadata metadata = new Metadata();
for (Map.Entry<String, String> header : grpcConfig.getHeaders().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ public void channelShouldBeAddedForAHostAndPort() {

}

@Test
public void channelBuilderShouldNotBeDecoratedWithKeepaliveORTimeoutMS() {
GrpcClient grpcClient = new GrpcClient(grpcSourceConfig);
grpcClient.decorateManagedChannelBuilder(channelBuilder);
verify(channelBuilder, never()).keepAliveTime(anyLong(), any());
verify(channelBuilder, never()).keepAliveTimeout(anyLong(), any());
}

@Test
public void channelBuilderShouldBeDecoratedWithKeepaliveMS() {
when(grpcSourceConfig.getGrpcArgKeepaliveTimeMs()).thenReturn("1000");

GrpcClient grpcClient = new GrpcClient(grpcSourceConfig);
grpcClient.decorateManagedChannelBuilder(channelBuilder);
verify(channelBuilder, times(1)).keepAliveTime(Long.parseLong("1000"), TimeUnit.MILLISECONDS);
verify(channelBuilder, never()).keepAliveTimeout(anyLong(), any());
}

@Test
public void channelBuilderShouldBeDecoratedWithKeepaliveAndTimeOutMS() {
when(grpcSourceConfig.getGrpcArgKeepaliveTimeMs()).thenReturn("1000");
Expand Down

0 comments on commit 3d6608c

Please sign in to comment.