use of com.google.api.codegen.grpc.MethodConfig.Name in project toolkit by googleapis.
the class GrpcGapicRetryMapping method create.
public static GrpcGapicRetryMapping create(ServiceConfig config, ImmutableMap<String, Interface> protoInterfaces) {
Map<String, String> methodCodesMap = new HashMap<>();
Map<String, String> methodParamsMap = new HashMap<>();
Map<String, RetryCodesDefinitionProto> codesDefMap = new HashMap<>();
Map<String, RetryParamsDefinitionProto> paramsDefMap = new HashMap<>();
// add no_retry configs for unknown/unspecified methods
RetryParamsDefinitionProto.Builder defaultParamsBuilder = retryPolicyToParamsBuilder(RetryPolicy.getDefaultInstance(), 0, "no_retry_params");
paramsDefMap.putIfAbsent(defaultParamsBuilder.getName(), defaultParamsBuilder.build());
RetryCodesDefinitionProto.Builder defaultCodesBuilder = retryPolicyToCodesBuilder(RetryPolicy.getDefaultInstance(), "no_retry_codes");
codesDefMap.putIfAbsent(defaultCodesBuilder.getName(), defaultCodesBuilder.build());
// build retry-to-interface mapping from gRPC ServiceConfig
int retryIndex = 1;
int noRetryIndex = 1;
for (com.google.api.codegen.grpc.MethodConfig methodConfig : config.getMethodConfigList()) {
RetryPolicy retryPolicy = methodConfig.getRetryPolicy();
String policyName;
if (methodConfig.getRetryOrHedgingPolicyCase() == RetryOrHedgingPolicyCase.RETRY_POLICY) {
policyName = "retry_policy_" + retryIndex++;
} else {
// make "unique" no_retry configs because the MethodConfig timeout may differ
policyName = "no_retry_" + noRetryIndex++;
}
long timeout = Durations.toMillis(methodConfig.getTimeout());
// construct retry params from RetryPolicy
RetryParamsDefinitionProto.Builder paramsBuilder = retryPolicyToParamsBuilder(retryPolicy, timeout, policyName + "_params");
paramsDefMap.putIfAbsent(paramsBuilder.getName(), paramsBuilder.build());
// construct retry codes from RetryPolicy
RetryCodesDefinitionProto.Builder codesBuilder = retryPolicyToCodesBuilder(retryPolicy, policyName + "_codes");
codesDefMap.putIfAbsent(codesBuilder.getName(), codesBuilder.build());
// apply the MethodConfig.RetryPolicy to names
for (Name name : methodConfig.getNameList()) {
applyRetryPolicyToName(name, codesBuilder.getName(), paramsBuilder.getName(), methodCodesMap, methodParamsMap, protoInterfaces);
}
}
// make immutable
ImmutableMap<String, String> codes = ImmutableMap.copyOf(methodCodesMap);
ImmutableMap<String, String> params = ImmutableMap.copyOf(methodParamsMap);
ImmutableMap<String, RetryCodesDefinitionProto> codesProto = ImmutableMap.copyOf(codesDefMap);
ImmutableMap<String, RetryParamsDefinitionProto> paramsProto = ImmutableMap.copyOf(paramsDefMap);
return new AutoValue_GrpcGapicRetryMapping.Builder().setMethodCodesMap(codes).setMethodParamsMap(params).setCodesDefMap(codesProto).setParamsDefMap(paramsProto).build();
}
Aggregations