static CallOptions putMetadataHandlerOption( CallOptions callOptions, ResponseMetadataHandler handler) { Preconditions.checkNotNull(callOptions); Preconditions.checkNotNull(handler); return callOptions.withOption(METADATA_HANDLER_CALL_OPTION_KEY, handler); }
static CallOptions putMetadataHandlerOption( CallOptions callOptions, ResponseMetadataHandler handler) { Preconditions.checkNotNull(callOptions); Preconditions.checkNotNull(handler); return callOptions.withOption(METADATA_HANDLER_CALL_OPTION_KEY, handler); }
/** {@inheritDoc} */ @Override public GrpcCallContext withTracer(@Nonnull ApiTracer tracer) { Preconditions.checkNotNull(tracer); return withCallOptions(callOptions.withOption(TRACER_KEY, tracer)); }
/** {@inheritDoc} */ @Override public GrpcCallContext withTracer(@Nonnull ApiTracer tracer) { Preconditions.checkNotNull(tracer); return withCallOptions(callOptions.withOption(TRACER_KEY, tracer)); }
public static CallOptions createCallOptions(final GrpcURL refUrl) { String serviceName = refUrl.getServiceInterface(); CallOptions options = CACHEOPTIONS_CACHE.get(serviceName); if (options == null) { ConcurrentHashMap<String, Object> customOptions = new ConcurrentHashMap<String, Object>(); customOptions.put(GRPC_REF_URL, refUrl); options = CallOptions.DEFAULT.withOption(CALLOPTIONS_CUSTOME_KEY, customOptions); CACHEOPTIONS_CACHE.put(serviceName, options); return options; } else { return options; } }
static CallOptions putRequestParamsDynamicHeaderOption( CallOptions callOptions, String requestParams) { if (callOptions == null || requestParams.isEmpty()) { return callOptions; } Map<Key<String>, String> dynamicHeadersOption = callOptions.getOption(DYNAMIC_HEADERS_CALL_OPTION_KEY); // This will fail, if REQUEST_PARAMS_HEADER_KEY is already there dynamicHeadersOption = ImmutableMap.<Key<String>, String>builder() .putAll(dynamicHeadersOption) .put(REQUEST_PARAMS_HEADER_KEY, requestParams) .build(); return callOptions.withOption(DYNAMIC_HEADERS_CALL_OPTION_KEY, dynamicHeadersOption); }
static CallOptions putRequestParamsDynamicHeaderOption( CallOptions callOptions, String requestParams) { if (callOptions == null || requestParams.isEmpty()) { return callOptions; } Map<Key<String>, String> dynamicHeadersOption = callOptions.getOption(DYNAMIC_HEADERS_CALL_OPTION_KEY); // This will fail, if REQUEST_PARAMS_HEADER_KEY is already there dynamicHeadersOption = ImmutableMap.<Key<String>, String>builder() .putAll(dynamicHeadersOption) .put(REQUEST_PARAMS_HEADER_KEY, requestParams) .build(); return callOptions.withOption(DYNAMIC_HEADERS_CALL_OPTION_KEY, dynamicHeadersOption); }
/** * Sets a custom option to be passed to client interceptors on the channel * {@link io.grpc.ClientInterceptor} via the CallOptions parameter. * * @since 1.0.0 * @param key the option being set * @param value the value for the key */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1869") public final <T> S withOption(CallOptions.Key<T> key, T value) { return build(channel, callOptions.withOption(key, value)); }
callOptions.withOption(GrpcUtil.CALL_OPTIONS_RPC_OWNED_BY_BALANCER, Boolean.TRUE), transportProvider, deadlineCancellationExecutor, callsTracer, false /* retryEnabled */);
.withOption(RETRY_POLICY_KEY, new ImmediateRetryPolicyProvider()) .withOption(HEDGING_POLICY_KEY, new ImmediateHedgingPolicyProvider()); } else { final class DelayedRetryPolicyProvider implements RetryPolicy.Provider { .withOption(RETRY_POLICY_KEY, new DelayedRetryPolicyProvider()) .withOption(HEDGING_POLICY_KEY, new DelayedHedgingPolicyProvider());
newCallOptions = newCallOptions.withOption(TRACER_KEY, newTracer);
newCallOptions = newCallOptions.withOption(TRACER_KEY, newTracer);
@VisibleForTesting CallOptions patchOptions(CallOptions baseOptions) { CallOptions patchedOptions = baseOptions; patchedOptions = patchOption(patchedOptions, CallOptions::getAuthority, CallOptions::withAuthority); patchedOptions = patchOption(patchedOptions, CallOptions::getCredentials, CallOptions::withCallCredentials); patchedOptions = patchOption(patchedOptions, CallOptions::getCompressor, CallOptions::withCompression); patchedOptions = patchOption(patchedOptions, CallOptions::getDeadline, CallOptions::withDeadline); patchedOptions = patchOption(patchedOptions, CallOptions::isWaitForReady, (callOptions, waitForReady) -> waitForReady ? callOptions.withWaitForReady() : callOptions.withoutWaitForReady()); patchedOptions = patchOption(patchedOptions, CallOptions::getMaxInboundMessageSize, CallOptions::withMaxInboundMessageSize); patchedOptions = patchOption(patchedOptions, CallOptions::getMaxOutboundMessageSize, CallOptions::withMaxOutboundMessageSize); patchedOptions = patchOption(patchedOptions, CallOptions::getExecutor, CallOptions::withExecutor); for (ClientStreamTracer.Factory factory : defaultOptions.getStreamTracerFactories()) { patchedOptions = patchedOptions.withStreamTracerFactory(factory); } for (CallOptions.Key<Object> key : customOptionKeys(defaultOptions)) { patchedOptions = patchOption(patchedOptions, co -> co.getOption(key), (co, o) -> co.withOption(key, o)); } return patchedOptions; }
@VisibleForTesting CallOptions patchOptions(CallOptions baseOptions) { CallOptions patchedOptions = baseOptions; patchedOptions = patchOption(patchedOptions, CallOptions::getAuthority, CallOptions::withAuthority); patchedOptions = patchOption(patchedOptions, CallOptions::getCredentials, CallOptions::withCallCredentials); patchedOptions = patchOption(patchedOptions, CallOptions::getCompressor, CallOptions::withCompression); patchedOptions = patchOption(patchedOptions, CallOptions::getDeadline, CallOptions::withDeadline); patchedOptions = patchOption(patchedOptions, CallOptions::isWaitForReady, (callOptions, waitForReady) -> waitForReady ? callOptions.withWaitForReady() : callOptions.withoutWaitForReady()); patchedOptions = patchOption(patchedOptions, CallOptions::getMaxInboundMessageSize, CallOptions::withMaxInboundMessageSize); patchedOptions = patchOption(patchedOptions, CallOptions::getMaxOutboundMessageSize, CallOptions::withMaxOutboundMessageSize); patchedOptions = patchOption(patchedOptions, CallOptions::getExecutor, CallOptions::withExecutor); for (ClientStreamTracer.Factory factory : defaultOptions.getStreamTracerFactories()) { patchedOptions = patchedOptions.withStreamTracerFactory(factory); } for (CallOptions.Key<Object> key : customOptionKeys(defaultOptions)) { patchedOptions = patchOption(patchedOptions, co -> co.getOption(key), (co, o) -> co.withOption(key, o)); } return patchedOptions; }
@Test public void testMergeWithCustomCallOptions() { CallOptions.Key<String> key = CallOptions.Key.createWithDefault("somekey", "somedefault"); GrpcCallContext ctx1 = GrpcCallContext.createDefault(); GrpcCallContext ctx2 = GrpcCallContext.createDefault() .withCallOptions(CallOptions.DEFAULT.withOption(key, "somevalue")); GrpcCallContext merged = (GrpcCallContext) ctx1.merge(ctx2); Truth.assertThat(merged.getCallOptions().getOption(key)) .isNotEqualTo(ctx1.getCallOptions().getOption(key)); Truth.assertThat(merged.getCallOptions().getOption(key)) .isEqualTo(ctx2.getCallOptions().getOption(key)); }
callOptions = callOptions.withOption(RPCTracingHelpers.REQUEST_DESCRIPTOR_CALL_OPTION, requestDescriptor) .withOption(RPCTracingHelpers.REQUEST_ID_CALL_OPTION, String.valueOf(requestId)); clientInterceptor.interceptCall(method, callOptions, channel).start(listener, headers); assertEquals(requestDescriptor, headers.get(RPCTracingHelpers.DESCRIPTOR_HEADER));