Java Code Examples for io.grpc.CallOptions#getExecutor()

The following examples show how to use io.grpc.CallOptions#getExecutor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ManagedChannelImpl.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private Executor getCallExecutor(CallOptions callOptions) {
  Executor executor = callOptions.getExecutor();
  if (executor == null) {
    executor = this.executor;
  }
  return executor;
}
 
Example 2
Source File: OobChannel.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(
    MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
  return new ClientCallImpl<RequestT, ResponseT>(methodDescriptor,
      callOptions.getExecutor() == null ? executor : callOptions.getExecutor(),
      callOptions, transportProvider, deadlineCancellationExecutor, channelCallsTracer,
      false /* retryEnabled */);
}
 
Example 3
Source File: ManagedChannelImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private Executor getCallExecutor(CallOptions callOptions) {
  Executor executor = callOptions.getExecutor();
  if (executor == null) {
    executor = this.executor;
  }
  return executor;
}
 
Example 4
Source File: OobChannel.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(
    MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
  return new ClientCallImpl<>(methodDescriptor,
      callOptions.getExecutor() == null ? executor : callOptions.getExecutor(),
      callOptions, transportProvider, deadlineCancellationExecutor, channelCallsTracer,
      false /* retryEnabled */);
}
 
Example 5
Source File: DelayedClientTransport.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Use the picker to try picking a transport for every pending stream, proceed the stream if the
 * pick is successful, otherwise keep it pending.
 *
 * <p>This method may be called concurrently with {@code newStream()}, and it's safe.  All pending
 * streams will be served by the latest picker (if a same picker is given more than once, they are
 * considered different pickers) as soon as possible.
 *
 * <p>This method <strong>must not</strong> be called concurrently with itself.
 */
final void reprocess(@Nullable SubchannelPicker picker) {
  ArrayList<PendingStream> toProcess;
  synchronized (lock) {
    lastPicker = picker;
    lastPickerVersion++;
    if (picker == null || !hasPendingStreams()) {
      return;
    }
    toProcess = new ArrayList<>(pendingStreams);
  }
  ArrayList<PendingStream> toRemove = new ArrayList<>();

  for (final PendingStream stream : toProcess) {
    PickResult pickResult = picker.pickSubchannel(stream.args);
    CallOptions callOptions = stream.args.getCallOptions();
    final ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult,
        callOptions.isWaitForReady());
    if (transport != null) {
      Executor executor = defaultAppExecutor;
      // createRealStream may be expensive. It will start real streams on the transport. If
      // there are pending requests, they will be serialized too, which may be expensive. Since
      // we are now on transport thread, we need to offload the work to an executor.
      if (callOptions.getExecutor() != null) {
        executor = callOptions.getExecutor();
      }
      executor.execute(new Runnable() {
          @Override
          public void run() {
            stream.createRealStream(transport);
          }
        });
      toRemove.add(stream);
    }  // else: stay pending
  }

  synchronized (lock) {
    // Between this synchronized and the previous one:
    //   - Streams may have been cancelled, which may turn pendingStreams into emptiness.
    //   - shutdown() may be called, which may turn pendingStreams into null.
    if (!hasPendingStreams()) {
      return;
    }
    pendingStreams.removeAll(toRemove);
    // Because delayed transport is long-lived, we take this opportunity to down-size the
    // hashmap.
    if (pendingStreams.isEmpty()) {
      pendingStreams = new LinkedHashSet<PendingStream>();
    }
    if (!hasPendingStreams()) {
      // There may be a brief gap between delayed transport clearing in-use state, and first real
      // transport starting streams and setting in-use state.  During the gap the whole channel's
      // in-use state may be false. However, it shouldn't cause spurious switching to idleness
      // (which would shutdown the transports and LoadBalancer) because the gap should be shorter
      // than IDLE_MODE_DEFAULT_TIMEOUT_MILLIS (1 second).
      syncContext.executeLater(reportTransportNotInUse);
      if (shutdownStatus != null && reportTransportTerminated != null) {
        syncContext.executeLater(reportTransportTerminated);
        reportTransportTerminated = null;
      }
    }
  }
  syncContext.drain();
}
 
Example 6
Source File: ArmeriaClientCall.java    From armeria with Apache License 2.0 4 votes vote down vote up
ArmeriaClientCall(
        DefaultClientRequestContext ctx,
        EndpointGroup endpointGroup,
        PooledHttpClient httpClient,
        HttpRequestWriter req,
        MethodDescriptor<I, O> method,
        int maxOutboundMessageSizeBytes,
        int maxInboundMessageSizeBytes,
        CallOptions callOptions,
        CompressorRegistry compressorRegistry,
        DecompressorRegistry decompressorRegistry,
        SerializationFormat serializationFormat,
        @Nullable GrpcJsonMarshaller jsonMarshaller,
        boolean unsafeWrapResponseBuffers,
        String advertisedEncodingsHeader) {
    this.ctx = ctx;
    this.endpointGroup = endpointGroup;
    this.httpClient = httpClient;
    this.req = req;
    this.method = method;
    this.callOptions = callOptions;
    this.compressorRegistry = compressorRegistry;
    this.unsafeWrapResponseBuffers = unsafeWrapResponseBuffers;
    this.advertisedEncodingsHeader = advertisedEncodingsHeader;
    this.serializationFormat = serializationFormat;
    messageFramer = new ArmeriaMessageFramer(ctx.alloc(), maxOutboundMessageSizeBytes);
    marshaller = new GrpcMessageMarshaller<>(
            ctx.alloc(), serializationFormat, method, jsonMarshaller,
            unsafeWrapResponseBuffers);
    responseReader = new HttpStreamReader(
            decompressorRegistry,
            new ArmeriaMessageDeframer(this, maxInboundMessageSizeBytes, ctx.alloc()),
            this);
    executor = callOptions.getExecutor();

    req.whenComplete().handle((unused1, unused2) -> {
        if (!ctx.log().isAvailable(RequestLogProperty.REQUEST_CONTENT)) {
            // Can reach here if the request stream was empty.
            ctx.logBuilder().requestContent(GrpcLogUtil.rpcRequest(method), null);
        }
        return null;
    });
}
 
Example 7
Source File: DelayedClientTransport.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Use the picker to try picking a transport for every pending stream, proceed the stream if the
 * pick is successful, otherwise keep it pending.
 *
 * <p>This method may be called concurrently with {@code newStream()}, and it's safe.  All pending
 * streams will be served by the latest picker (if a same picker is given more than once, they are
 * considered different pickers) as soon as possible.
 *
 * <p>This method <strong>must not</strong> be called concurrently with itself.
 */
final void reprocess(@Nullable SubchannelPicker picker) {
  ArrayList<PendingStream> toProcess;
  synchronized (lock) {
    lastPicker = picker;
    lastPickerVersion++;
    if (picker == null || !hasPendingStreams()) {
      return;
    }
    toProcess = new ArrayList<>(pendingStreams);
  }
  ArrayList<PendingStream> toRemove = new ArrayList<>();

  for (final PendingStream stream : toProcess) {
    PickResult pickResult = picker.pickSubchannel(stream.args);
    CallOptions callOptions = stream.args.getCallOptions();
    final ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult,
        callOptions.isWaitForReady());
    if (transport != null) {
      Executor executor = defaultAppExecutor;
      // createRealStream may be expensive. It will start real streams on the transport. If
      // there are pending requests, they will be serialized too, which may be expensive. Since
      // we are now on transport thread, we need to offload the work to an executor.
      if (callOptions.getExecutor() != null) {
        executor = callOptions.getExecutor();
      }
      executor.execute(new Runnable() {
          @Override
          public void run() {
            stream.createRealStream(transport);
          }
        });
      toRemove.add(stream);
    }  // else: stay pending
  }

  synchronized (lock) {
    // Between this synchronized and the previous one:
    //   - Streams may have been cancelled, which may turn pendingStreams into emptiness.
    //   - shutdown() may be called, which may turn pendingStreams into null.
    if (!hasPendingStreams()) {
      return;
    }
    pendingStreams.removeAll(toRemove);
    // Because delayed transport is long-lived, we take this opportunity to down-size the
    // hashmap.
    if (pendingStreams.isEmpty()) {
      pendingStreams = new LinkedHashSet<>();
    }
    if (!hasPendingStreams()) {
      // There may be a brief gap between delayed transport clearing in-use state, and first real
      // transport starting streams and setting in-use state.  During the gap the whole channel's
      // in-use state may be false. However, it shouldn't cause spurious switching to idleness
      // (which would shutdown the transports and LoadBalancer) because the gap should be shorter
      // than IDLE_MODE_DEFAULT_TIMEOUT_MILLIS (1 second).
      syncContext.executeLater(reportTransportNotInUse);
      if (shutdownStatus != null && reportTransportTerminated != null) {
        syncContext.executeLater(reportTransportTerminated);
        reportTransportTerminated = null;
      }
    }
  }
  syncContext.drain();
}