/** * {@inheritDoc} */ @Override public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( final MethodDescriptor<ReqT, RespT> method, final CallOptions callOptions, final Channel next ) { final String methodType = method.getType().toString(); final String methodName = method.getFullMethodName(); final int channelId = next.hashCode(); log.info("gRPC {} call: {} (channel: {})", methodType, methodName, channelId); return next.newCall(method, callOptions); } }
public static MethodDescriptor convertJsonDescriptor(MethodDescriptor oldDesc, Class req, Class res){ MethodDescriptor.Marshaller reqMarshaller = getJsonMarshaller(req); MethodDescriptor.Marshaller resMarshaller = getJsonMarshaller(res); if(reqMarshaller != null && resMarshaller != null){ return MethodDescriptor.create(oldDesc.getType(), oldDesc.getFullMethodName(), reqMarshaller, resMarshaller); } return null; }
try { if (messageReceived && method.getType() == MethodType.UNARY) { closeListener(Status.INTERNAL.withDescription( "More than one request messages for unary call or server streaming call"));
if (method.getType() != MethodType.UNARY) { return HttpResponse.of(HttpStatus.BAD_REQUEST, MediaType.PLAIN_TEXT_UTF_8,
MethodType methodType = methodDesc.getType(); switch (methodType) { case UNARY:
@Override public io.grpc.MethodDescriptor.MethodType getMethodType() { return methodDesc.getType(); }
/** * {@inheritDoc} */ @Override public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( final MethodDescriptor<ReqT, RespT> method, final CallOptions callOptions, final Channel next ) { final String methodType = method.getType().toString(); final String methodName = method.getFullMethodName(); final int channelId = next.hashCode(); log.info("gRPC {} call: {} (channel: {})", methodType, methodName, channelId); return next.newCall(method, callOptions); } }
ClientCallImpl( MethodDescriptor<ReqT, RespT> method, Executor executor, CallOptions callOptions, ClientTransportProvider clientTransportProvider, ScheduledExecutorService deadlineCancellationExecutor, CallTracer channelCallsTracer, boolean retryEnabled) { this.method = method; // If we know that the executor is a direct executor, we don't need to wrap it with a // SerializingExecutor. This is purely for performance reasons. // See https://github.com/grpc/grpc-java/issues/368 this.callExecutor = executor == directExecutor() ? new SerializeReentrantCallsDirectExecutor() : new SerializingExecutor(executor); this.channelCallsTracer = channelCallsTracer; // Propagate the context from the thread which initiated the call to all callbacks. this.context = Context.current(); this.unaryRequest = method.getType() == MethodType.UNARY || method.getType() == MethodType.SERVER_STREAMING; this.callOptions = callOptions; this.clientTransportProvider = clientTransportProvider; this.deadlineCancellationExecutor = deadlineCancellationExecutor; this.retryEnabled = retryEnabled; }
@Override public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { // Only UNARY calls can be retried, streaming call errors should be handled by user. if (method.getType() != MethodDescriptor.MethodType.UNARY || config.isDisabled()) { return next.newCall(method, callOptions); } return new RetryingCall<ReqT, RespT>(method, callOptions, next, Context.current(), config); }
/** Creates a {@link ClientStats} for the supplied method. */ <ReqT, RespT> ClientStats createMetricsForMethod(MethodDescriptor<ReqT, RespT> methodDescriptor, StatsLogger statsLogger) { String fullMethodName = methodDescriptor.getFullMethodName(); String serviceName = MethodDescriptor.extractFullServiceName(fullMethodName); String methodName = fullMethodName.substring(serviceName.length() + 1); MethodType type = methodDescriptor.getType(); return new ClientStats( statsLogger.scope(methodName), includeLatencyHistograms, type == MethodType.CLIENT_STREAMING || type == MethodType.BIDI_STREAMING, type == MethodType.SERVER_STREAMING || type == MethodType.BIDI_STREAMING); } }
/** Creates a {@link ServerStats} for the supplied method. */ <ReqT, RespT> ServerStats createMetricsForMethod(MethodDescriptor<ReqT, RespT> methodDescriptor, StatsLogger statsLogger) { String fullMethodName = methodDescriptor.getFullMethodName(); String serviceName = MethodDescriptor.extractFullServiceName(fullMethodName); String methodName = fullMethodName.substring(serviceName.length() + 1); MethodType type = methodDescriptor.getType(); return new ServerStats( statsLogger.scope(methodName), includeLatencyHistograms, type == MethodType.CLIENT_STREAMING || type == MethodType.BIDI_STREAMING, type == MethodType.SERVER_STREAMING || type == MethodType.BIDI_STREAMING); } }
ClientCallImpl( MethodDescriptor<ReqT, RespT> method, Executor executor, CallOptions callOptions, ClientTransportProvider clientTransportProvider, ScheduledExecutorService deadlineCancellationExecutor, CallTracer channelCallsTracer, boolean retryEnabled) { this.method = method; // If we know that the executor is a direct executor, we don't need to wrap it with a // SerializingExecutor. This is purely for performance reasons. // See https://github.com/grpc/grpc-java/issues/368 this.callExecutor = executor == directExecutor() ? new SerializeReentrantCallsDirectExecutor() : new SerializingExecutor(executor); this.channelCallsTracer = channelCallsTracer; // Propagate the context from the thread which initiated the call to all callbacks. this.context = Context.current(); this.unaryRequest = method.getType() == MethodType.UNARY || method.getType() == MethodType.SERVER_STREAMING; this.callOptions = callOptions; this.clientTransportProvider = clientTransportProvider; this.deadlineCancellationExecutor = deadlineCancellationExecutor; this.retryEnabled = retryEnabled; }
@Override public void close(Status status, Metadata trailers) { checkState(!closeCalled, "call already closed"); try { closeCalled = true; if (status.isOk() && method.getType().serverSendsOneMessage() && !messageSent) { internalClose(Status.INTERNAL.withDescription(MISSING_RESPONSE)); return; } stream.close(status, trailers); } finally { serverCallTracer.reportCallEnded(status.isOk()); } }
@Override public ServerCall.Listener<ReqT> startCall(ServerCall<ReqT, RespT> call, Metadata headers) { Preconditions.checkArgument( call.getMethodDescriptor().getType().clientSendsOneMessage(), "asyncUnaryRequestCall is only for clientSendsOneMessage methods"); ServerCallStreamObserverImpl<ReqT, RespT> responseObserver = new ServerCallStreamObserverImpl<ReqT, RespT>(call); // We expect only 1 request, but we ask for 2 requests here so that if a misbehaving client // sends more than 1 requests, ServerCall will catch it. Note that disabling auto // inbound flow control has no effect on unary calls. call.request(2); return new UnaryServerCallListener(responseObserver, call); }
@Override public void close(Status status, Metadata trailers) { checkState(!closeCalled, "call already closed"); try { closeCalled = true; if (status.isOk() && method.getType().serverSendsOneMessage() && !messageSent) { internalClose(Status.INTERNAL.withDescription(MISSING_RESPONSE)); return; } stream.close(status, trailers); } finally { serverCallTracer.reportCallEnded(status.isOk()); } }
@Override public void sendMessage(RespT message) { checkState(sendHeadersCalled, "sendHeaders has not been called"); checkState(!closeCalled, "call is closed"); if (method.getType().serverSendsOneMessage() && messageSent) { internalClose(Status.INTERNAL.withDescription(TOO_MANY_RESPONSES)); return; } messageSent = true; try { InputStream resp = method.streamResponse(message); stream.writeMessage(resp); stream.flush(); } catch (RuntimeException e) { close(Status.fromThrowable(e), new Metadata()); } catch (Error e) { close( Status.CANCELLED.withDescription("Server sendMessage() failed with Error"), new Metadata()); throw e; } }
try { if (messageReceived && method.getType() == MethodType.UNARY) { closeListener(Status.INTERNAL.withDescription( "More than one request messages for unary call or server streaming call"));
private static ServerMethodDefinition<?, ?> createProxyServerMethodDefinition( MethodDescriptor<?, ?> methodDesc, ServerCallHandler<InputStream, InputStream> handler) { MethodDescriptor<InputStream, InputStream> methodDescriptor = MethodDescriptor.newBuilder( IdentityInputStreamMarshaller.of(), IdentityInputStreamMarshaller.of()) .setFullMethodName(methodDesc.getFullMethodName()) .setType(methodDesc.getType()) .setIdempotent(methodDesc.isIdempotent()) .setSafe(methodDesc.isSafe()) .build(); return ServerMethodDefinition.create(methodDescriptor, handler); }
@Override public void sendMessage(RespT message) { checkState(sendHeadersCalled, "sendHeaders has not been called"); checkState(!closeCalled, "call is closed"); if (method.getType().serverSendsOneMessage() && messageSent) { internalClose(Status.INTERNAL.withDescription(TOO_MANY_RESPONSES)); return; } messageSent = true; try { InputStream resp = method.streamResponse(message); stream.writeMessage(resp); stream.flush(); } catch (RuntimeException e) { close(Status.fromThrowable(e), new Metadata()); } catch (Error e) { close( Status.CANCELLED.withDescription("Server sendMessage() failed with Error"), new Metadata()); throw e; } }
if (method.getType() != MethodType.UNARY) { return HttpResponse.of(HttpStatus.BAD_REQUEST, MediaType.PLAIN_TEXT_UTF_8,