Search in sources :

Example 1 with RetryPolicy

use of io.grpc.serviceconfig.MethodConfig.RetryPolicy in project gapic-generator-java by googleapis.

the class RetrySettingsComposer method createRetrySettingsExprs.

private static List<Expr> createRetrySettingsExprs(String settingsName, GapicRetrySettings settings, VariableExpr settingsVarExpr, VariableExpr definitionsVarExpr) {
    Expr settingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("RetrySettings")).setMethodName("newBuilder").build();
    RetryPolicy retryPolicy = settings.retryPolicy();
    if (settings.kind().equals(GapicRetrySettings.Kind.FULL)) {
        Preconditions.checkState(retryPolicy.hasInitialBackoff(), String.format("initialBackoff not found for setting %s", settingsName));
        settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("setInitialRetryDelay").setArguments(createDurationOfMillisExpr(toValExpr(retryPolicy.getInitialBackoff()))).build();
        settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("setRetryDelayMultiplier").setArguments(toValExpr(retryPolicy.getBackoffMultiplier())).build();
        Preconditions.checkState(retryPolicy.hasMaxBackoff(), String.format("maxBackoff not found for setting %s", settingsName));
        settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("setMaxRetryDelay").setArguments(createDurationOfMillisExpr(toValExpr(retryPolicy.getMaxBackoff()))).build();
    }
    if (!settings.kind().equals(GapicRetrySettings.Kind.NONE)) {
        settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("setInitialRpcTimeout").setArguments(createDurationOfMillisExpr(toValExpr(settings.timeout()))).build();
    }
    // This will always be done, no matter the type of the retry settings object.
    settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("setRpcTimeoutMultiplier").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.DOUBLE).setValue("1.0").build())).build();
    if (!settings.kind().equals(GapicRetrySettings.Kind.NONE)) {
        for (String setterMethodName : Arrays.asList("setMaxRpcTimeout", "setTotalTimeout")) {
            settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName(setterMethodName).setArguments(createDurationOfMillisExpr(toValExpr(settings.timeout()))).build();
        }
    }
    settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("build").setReturnType(settingsVarExpr.type()).build();
    Expr settingsAssignExpr = AssignmentExpr.builder().setVariableExpr(settingsVarExpr).setValueExpr(settingsBuilderExpr).build();
    Expr definitionsPutExpr = MethodInvocationExpr.builder().setExprReferenceExpr(definitionsVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(StringObjectValue.withValue(settingsName)), settingsVarExpr).build();
    return Arrays.asList(settingsAssignExpr, definitionsPutExpr);
}
Also used : EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) RetryPolicy(io.grpc.serviceconfig.MethodConfig.RetryPolicy)

Aggregations

AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)1 Expr (com.google.api.generator.engine.ast.Expr)1 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)1 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)1 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)1 RetryPolicy (io.grpc.serviceconfig.MethodConfig.RetryPolicy)1