Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public class GrpcChannelConfig {
Integer maxInboundMessageSize;

@Singular List<ClientInterceptor> clientInterceptors;

GrpcServiceConfig serviceConfig;
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ protected ManagedChannel configureAndBuildChannel(
if (config.getMaxInboundMessageSize() != null) {
builder.maxInboundMessageSize(config.getMaxInboundMessageSize());
}
if (config.getServiceConfig() != null) {
builder.defaultServiceConfig(config.getServiceConfig().toMap()).enableRetry();
Comment thread
aaron-steinfeld marked this conversation as resolved.
Outdated
}
this.registryConfig.getDefaultInterceptors().forEach(builder::intercept);
return builder.intercept(config.getClientInterceptors()).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.hypertrace.core.grpcutils.client;

import static java.util.stream.Collectors.toUnmodifiableList;

import io.grpc.Status;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Singular;
import lombok.Value;

@Value
@Builder
public class GrpcRetryPolicy {
private static final String MAX_ATTEMPTS = "maxAttempts";
private static final String INITIAL_BACKOFF = "initialBackoff";
private static final String MAX_BACKOFF = "maxBackoff";
private static final String BACKOFF_MULTIPLIER = "backoffMultiplier";
private static final String RETRYABLE_STATUS_CODES = "retryableStatusCodes";

int maxAttempts;
Duration initialBackoff;
Duration maxBackoff;
double backoffMultiplier;
@Singular List<Status.Code> retryableStatusCodes;

Map<String, Object> toMap() {
return Map.of(
MAX_ATTEMPTS,
(double) maxAttempts,
INITIAL_BACKOFF,
initialBackoff.toMillis() / 1000.0 + "s",
Comment thread
aaron-steinfeld marked this conversation as resolved.
Outdated
MAX_BACKOFF,
maxBackoff.toMillis() / 1000.0 + "s",
BACKOFF_MULTIPLIER,
backoffMultiplier,
RETRYABLE_STATUS_CODES,
retryableStatusCodes.stream().map(Enum::name).collect(toUnmodifiableList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.hypertrace.core.grpcutils.client;

import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Value;

@Value
@Builder
public class GrpcServiceConfig {
private static final String METHOD_CONFIG = "methodConfig";
private static final String NAME = "name";
private static final String RETRY_POLICY = "retryPolicy";

GrpcRetryPolicy retryPolicy;

Map<String, Object> toMap() {
return Map.of(
METHOD_CONFIG, List.of(Map.of(NAME, List.of(Map.of()), RETRY_POLICY, retryPolicy.toMap())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.grpc.Deadline.Ticker;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Status;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -145,6 +147,31 @@ void copyConstructorReusesExistingChannels() {
assertSame(firstChannel, new GrpcChannelRegistry(firstRegistry).forSecureAddress("foo", 1000));
}

@Test
void createsDistinctChannelsForDifferentServiceConfigs() {
GrpcServiceConfig serviceConfig =
GrpcServiceConfig.builder()
.retryPolicy(
GrpcRetryPolicy.builder()
.maxAttempts(3)
.initialBackoff(Duration.ofMillis(100))
.maxBackoff(Duration.ofSeconds(1))
.backoffMultiplier(2.0)
.retryableStatusCode(Status.Code.UNAVAILABLE)
.build())
.build();
Channel channelWithServiceConfig =
this.channelRegistry.forSecureAddress(
"foo", 1000, GrpcChannelConfig.builder().serviceConfig(serviceConfig).build());

assertNotNull(channelWithServiceConfig);
assertNotSame(channelWithServiceConfig, this.channelRegistry.forSecureAddress("foo", 1000));
assertSame(
Comment thread
aaron-steinfeld marked this conversation as resolved.
Outdated
channelWithServiceConfig,
this.channelRegistry.forSecureAddress(
"foo", 1000, GrpcChannelConfig.builder().serviceConfig(serviceConfig).build()));
}

@Test
void registersRegistryInterceptors() {
try (MockedStatic<ManagedChannelBuilder> mockedBuilderStatic =
Expand Down
Loading