public GrpcClientLimiterBuilder partitionByCallOption(CallOptions.Key<String> option) { return partitionResolver(context -> context.getCallOptions().getOption(option)); }
public static ResponseMetadataHandler getMetadataHandlerOption(CallOptions callOptions) { return callOptions.getOption(METADATA_HANDLER_CALL_OPTION_KEY); } }
public static ResponseMetadataHandler getMetadataHandlerOption(CallOptions callOptions) { return callOptions.getOption(METADATA_HANDLER_CALL_OPTION_KEY); } }
static Map<Key<String>, String> getDynamicHeadersOption(CallOptions callOptions) { return callOptions.getOption(DYNAMIC_HEADERS_CALL_OPTION_KEY); }
static Map<Key<String>, String> getDynamicHeadersOption(CallOptions callOptions) { return callOptions.getOption(DYNAMIC_HEADERS_CALL_OPTION_KEY); }
/** * Returns true if an RPC with the given properties should be counted when calculating the * in-use state of a transport. */ public static boolean shouldBeCountedForInUse(CallOptions callOptions) { return !Boolean.TRUE.equals(callOptions.getOption(CALL_OPTIONS_RPC_OWNED_BY_BALANCER)); }
/** {@inheritDoc} */ @Override @Nonnull public ApiTracer getTracer() { ApiTracer tracer = callOptions.getOption(TRACER_KEY); if (tracer == null) { tracer = NoopApiTracer.getInstance(); } return tracer; }
public static Map<String, Object> getAffinity(final GrpcURL refUrl) { String serviceName = refUrl.getServiceInterface(); return CACHEOPTIONS_CACHE.get(serviceName).getOption(CALLOPTIONS_CUSTOME_KEY); }
/** {@inheritDoc} */ @Override @Nonnull public ApiTracer getTracer() { ApiTracer tracer = callOptions.getOption(TRACER_KEY); if (tracer == null) { tracer = NoopApiTracer.getInstance(); } return tracer; }
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); }
@Override public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall( MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions ) { final String method = callOptions.getOption(CLARIFAI_METHOD_OPTION); if (method == null) { throw new ClarifaiException("The request doesn't have the CLARIFAI_METHOD_OPTION set."); } final String baseUrl = callOptions.getOption(CLARIFAI_BASE_URL_OPTION); if (baseUrl == null) { throw new ClarifaiException("The request doesn't have the CLARIFAI_BASE_URL_OPTION set."); } final String subUrl = callOptions.getOption(CLARIFAI_SUB_URL_OPTION); if (subUrl == null) { throw new ClarifaiException("The request doesn't have the CLARIFAI_SUB_URL_OPTION set."); } String url = baseUrl + subUrl; return new JsonClientCall<>(httpClient, method, url, methodDescriptor); }
private NameResolverNotify createNameResolverNotify() { Map<String, Object> affinity = callOptions.getOption(GrpcCallOptions.CALLOPTIONS_CUSTOME_KEY); NameResolverNotify nameResolverNotify = NameResolverNotify.newNameResolverNotify(); nameResolverNotify.refreshAffinity(affinity); return nameResolverNotify; }
private void statusError(Status status, Metadata trailers) { if (enabledRetry) { final NameResolverNotify nameResolverNotify = this.createNameResolverNotify(); boolean retryHaveDone = this.retryHaveDone(); if (retryHaveDone) { completionFuture.setException(status.asRuntimeException(trailers)); } else { nameResolverNotify.refreshChannel(); scheduleRetryService.execute(this); SocketAddress remoteAddress = (SocketAddress) callOptions.getOption(GrpcCallOptions.CALLOPTIONS_CUSTOME_KEY) .get(GrpcCallOptions.GRPC_CURRENT_ADDR_KEY); logger.error(String.format("Retrying failed call. Failure #%d,Failure Server: %s", currentRetries.get(), String.valueOf(remoteAddress))); currentRetries.getAndIncrement(); } } else { completionFuture.setException(status.asRuntimeException(trailers)); } }
@Override public PickResult pickSubchannel(PickSubchannelArgs args) { Map<String, Object> affinity = args.getCallOptions().getOption(GrpcCallOptions.CALLOPTIONS_CUSTOME_KEY); GrpcURL refUrl = (GrpcURL) affinity.get(GrpcCallOptions.GRPC_REF_URL); if (size > 0) { Subchannel subchannel = nextSubchannel(refUrl); affinity.put(GrpcCallOptions.GRPC_NAMERESOVER_ATTRIBUTES, nameResovleCache); return PickResult.withSubchannel(subchannel); } if (status != null) { return PickResult.withError(status); } return PickResult.withNoResult(); }
@Override public void onClose(Status status, Metadata trailers) { try { SocketAddress remoteServer = clientCall.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR); callOptions.getOption(GrpcCallOptions.CALLOPTIONS_CUSTOME_KEY) .put(GrpcCallOptions.GRPC_CURRENT_ADDR_KEY, remoteServer); } finally { if (status.isOk()) { statusOk(trailers); } else { statusError(status, trailers); } } }
@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)); }
@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; }