Search in sources :

Example 1 with BufferDecoderGroup

use of io.servicetalk.encoding.api.BufferDecoderGroup in project servicetalk by apple.

the class ProtocolCompatibilityTest method serviceTalkServer.

private static TestServerContext serviceTalkServer(final ErrorMode errorMode, final boolean ssl, final GrpcExecutionStrategy strategy, @Nullable final String compression, @Nullable final Duration timeout, Queue<Throwable> reqStreamError) throws Exception {
    final Compat.CompatService compatService = new Compat.CompatService() {

        @Override
        public Publisher<CompatResponse> bidirectionalStreamingCall(final GrpcServiceContext ctx, final Publisher<CompatRequest> pub) {
            reqStreamError.add(SERVER_PROCESSED_TOKEN);
            maybeThrowFromRpc(errorMode);
            return pub.map(req -> response(req.getId())).beforeFinally(errorConsumer());
        }

        @Override
        public Single<CompatResponse> clientStreamingCall(final GrpcServiceContext ctx, final Publisher<CompatRequest> pub) {
            reqStreamError.add(SERVER_PROCESSED_TOKEN);
            maybeThrowFromRpc(errorMode);
            return pub.collect(() -> 0, (sum, req) -> sum + req.getId()).map(this::response).beforeFinally(errorConsumer());
        }

        @Override
        public Single<CompatResponse> scalarCall(final GrpcServiceContext ctx, final CompatRequest req) {
            maybeThrowFromRpc(errorMode);
            return succeeded(response(req.getId()));
        }

        @Override
        public Publisher<CompatResponse> serverStreamingCall(final GrpcServiceContext ctx, final CompatRequest req) {
            maybeThrowFromRpc(errorMode);
            return Publisher.fromIterable(() -> IntStream.range(0, req.getId()).iterator()).map(this::response);
        }

        private CompatResponse response(final int value) {
            if (errorMode == ErrorMode.SIMPLE_IN_RESPONSE) {
                throwGrpcStatusException();
            } else if (errorMode == ErrorMode.STATUS_IN_RESPONSE) {
                throwGrpcStatusExceptionWithStatus();
            }
            return computeResponse(value);
        }

        private TerminalSignalConsumer errorConsumer() {
            return new TerminalSignalConsumer() {

                @Override
                public void onComplete() {
                }

                @Override
                public void onError(final Throwable throwable) {
                    reqStreamError.add(throwable);
                }

                @Override
                public void cancel() {
                    reqStreamError.add(new IOException("cancelled"));
                }
            };
        }
    };
    final ServiceFactory serviceFactory = new ServiceFactory.Builder().bufferEncoders(serviceTalkCompressions(compression)).bufferDecoderGroup(serviceTalkDecompression(compression)).bidirectionalStreamingCall(strategy, compatService).clientStreamingCall(strategy, compatService).scalarCall(strategy, compatService).serverStreamingCall(strategy, compatService).build();
    final ServerContext serverContext = serviceTalkServerBuilder(errorMode, ssl, timeout, b -> b.executionStrategy(strategy)).listenAndAwait(serviceFactory);
    return TestServerContext.fromServiceTalkServerContext(serverContext);
}
Also used : Arrays(java.util.Arrays) CompatResponse(io.servicetalk.grpc.netty.CompatProto.ResponseContainer.CompatResponse) EmptyBufferDecoderGroup(io.servicetalk.encoding.api.EmptyBufferDecoderGroup) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) GrpcClientMetadata(io.servicetalk.grpc.api.GrpcClientMetadata) TerminalSignalConsumer(io.servicetalk.concurrent.api.TerminalSignalConsumer) NettyServerBuilder(io.grpc.netty.NettyServerBuilder) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) BidirectionalStreamingCallMetadata(io.servicetalk.grpc.netty.CompatProto.Compat.BidirectionalStreamingCallMetadata) Future(java.util.concurrent.Future) StatusProto(io.grpc.protobuf.StatusProto) Arrays.asList(java.util.Arrays.asList) Duration(java.time.Duration) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) Status(io.grpc.Status) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) HttpProtocolConfigs.h2(io.servicetalk.http.netty.HttpProtocolConfigs.h2) DefaultTestCerts.loadServerKey(io.servicetalk.test.resources.DefaultTestCerts.loadServerKey) GrpcStatus(io.servicetalk.grpc.api.GrpcStatus) BlockingCompatClient(io.servicetalk.grpc.netty.CompatProto.Compat.BlockingCompatClient) BlockingQueue(java.util.concurrent.BlockingQueue) Processors.newSingleProcessor(io.servicetalk.concurrent.api.Processors.newSingleProcessor) Arguments(org.junit.jupiter.params.provider.Arguments) DefaultTestCerts.serverPemHostname(io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname) BlockingIterable(io.servicetalk.concurrent.BlockingIterable) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientStreamingCallMetadata(io.servicetalk.grpc.netty.CompatProto.Compat.ClientStreamingCallMetadata) GRPC_TIMEOUT_HEADER_KEY(io.servicetalk.grpc.internal.DeadlineUtils.GRPC_TIMEOUT_HEADER_KEY) Compat(io.servicetalk.grpc.netty.CompatProto.Compat) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Any(com.google.protobuf.Any) Matchers.is(org.hamcrest.Matchers.is) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) Codec(io.grpc.Codec) GrpcClientBuilder(io.servicetalk.grpc.api.GrpcClientBuilder) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Compressor(io.grpc.Compressor) BlockingCompatService(io.servicetalk.grpc.netty.CompatProto.Compat.BlockingCompatService) ArrayList(java.util.ArrayList) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) CANCELLED(io.servicetalk.grpc.api.GrpcStatusCode.CANCELLED) Decompressor(io.grpc.Decompressor) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Server(io.grpc.Server) Nullable(javax.annotation.Nullable) SslContext(io.netty.handler.ssl.SslContext) Single(io.servicetalk.concurrent.api.Single) Completable(io.servicetalk.concurrent.api.Completable) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) NettyBufferEncoders(io.servicetalk.encoding.netty.NettyBufferEncoders) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) GrpcExecutionStrategy(io.servicetalk.grpc.api.GrpcExecutionStrategy) DefaultTestCerts.loadServerPem(io.servicetalk.test.resources.DefaultTestCerts.loadServerPem) ArrayDeque(java.util.ArrayDeque) HttpServers(io.servicetalk.http.netty.HttpServers) GrpcSslContexts(io.grpc.netty.GrpcSslContexts) CompatRequest(io.servicetalk.grpc.netty.CompatProto.RequestContainer.CompatRequest) SocketAddress(java.net.SocketAddress) ManagedChannel(io.grpc.ManagedChannel) GrpcStatusCode(io.servicetalk.grpc.api.GrpcStatusCode) DEFAULT_TIMEOUT_SECONDS(io.servicetalk.concurrent.internal.TestTimeoutConstants.DEFAULT_TIMEOUT_SECONDS) GrpcStatusException(io.servicetalk.grpc.api.GrpcStatusException) GrpcExecutionStrategies.defaultStrategy(io.servicetalk.grpc.api.GrpcExecutionStrategies.defaultStrategy) StreamObserver(io.grpc.stub.StreamObserver) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) MethodSource(org.junit.jupiter.params.provider.MethodSource) ScalarCallMetadata(io.servicetalk.grpc.netty.CompatProto.Compat.ScalarCallMetadata) BufferDecoderGroupBuilder(io.servicetalk.encoding.api.BufferDecoderGroupBuilder) GrpcExecutionStrategies.offloadNever(io.servicetalk.grpc.api.GrpcExecutionStrategies.offloadNever) DEADLINE_EXCEEDED(io.servicetalk.grpc.api.GrpcStatusCode.DEADLINE_EXCEEDED) PublisherSource(io.servicetalk.concurrent.PublisherSource) Collection(java.util.Collection) GrpcPayloadWriter(io.servicetalk.grpc.api.GrpcPayloadWriter) InetSocketAddress(java.net.InetSocketAddress) GrpcServiceContext(io.servicetalk.grpc.api.GrpcServiceContext) Identity(io.servicetalk.encoding.api.Identity) CompatClient(io.servicetalk.grpc.netty.CompatProto.Compat.CompatClient) List(java.util.List) GrpcExecutionContext(io.servicetalk.grpc.api.GrpcExecutionContext) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) CompressorRegistry(io.grpc.CompressorRegistry) Queue(java.util.Queue) Duration.ofMillis(java.time.Duration.ofMillis) IntStream(java.util.stream.IntStream) Publisher.never(io.servicetalk.concurrent.api.Publisher.never) CsvSource(org.junit.jupiter.params.provider.CsvSource) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Publisher(io.servicetalk.concurrent.api.Publisher) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Processor(io.servicetalk.concurrent.SingleSource.Processor) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) OPENSSL(io.servicetalk.transport.api.SslProvider.OPENSSL) DecompressorRegistry(io.grpc.DecompressorRegistry) ServerStreamingCallMetadata(io.servicetalk.grpc.netty.CompatProto.Compat.ServerStreamingCallMetadata) GrpcServerBuilder(io.servicetalk.grpc.api.GrpcServerBuilder) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServiceFactory(io.servicetalk.grpc.netty.CompatProto.Compat.ServiceFactory) ServerContext(io.servicetalk.transport.api.ServerContext) Processors.newPublisherProcessor(io.servicetalk.concurrent.api.Processors.newPublisherProcessor) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) Any.pack(com.google.protobuf.Any.pack) Collections(java.util.Collections) HttpClients(io.servicetalk.http.netty.HttpClients) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TerminalSignalConsumer(io.servicetalk.concurrent.api.TerminalSignalConsumer) ServiceFactory(io.servicetalk.grpc.netty.CompatProto.Compat.ServiceFactory) Compat(io.servicetalk.grpc.netty.CompatProto.Compat) Publisher(io.servicetalk.concurrent.api.Publisher) IOException(java.io.IOException) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingCompatService(io.servicetalk.grpc.netty.CompatProto.Compat.BlockingCompatService) CompatResponse(io.servicetalk.grpc.netty.CompatProto.ResponseContainer.CompatResponse) GrpcServiceContext(io.servicetalk.grpc.api.GrpcServiceContext) CompatRequest(io.servicetalk.grpc.netty.CompatProto.RequestContainer.CompatRequest)

Example 2 with BufferDecoderGroup

use of io.servicetalk.encoding.api.BufferDecoderGroup in project servicetalk by apple.

the class DefaultGrpcClientCallFactory method newBlockingStreamingCall.

@Override
public <Req, Resp> BlockingStreamingClientCall<Req, Resp> newBlockingStreamingCall(final MethodDescriptor<Req, Resp> methodDescriptor, final BufferDecoderGroup decompressors) {
    GrpcStreamingSerializer<Req> serializerIdentity = streamingSerializer(methodDescriptor);
    GrpcStreamingDeserializer<Resp> deserializerIdentity = streamingDeserializer(methodDescriptor);
    List<GrpcStreamingDeserializer<Resp>> deserializers = streamingDeserializers(methodDescriptor, decompressors.decoders());
    CharSequence acceptedEncoding = decompressors.advertisedMessageEncoding();
    CharSequence requestContentType = grpcContentType(methodDescriptor.requestDescriptor().serializerDescriptor().contentType());
    CharSequence responseContentType = grpcContentType(methodDescriptor.responseDescriptor().serializerDescriptor().contentType());
    final BlockingStreamingHttpClient client = streamingHttpClient.asBlockingStreamingClient();
    return (metadata, request) -> {
        Duration timeout = timeoutForRequest(metadata.timeout());
        GrpcStreamingSerializer<Req> serializer = streamingSerializer(methodDescriptor, serializerIdentity, metadata.requestCompressor());
        String mdPath = methodDescriptor.httpPath();
        BlockingStreamingHttpRequest httpRequest = client.post(UNKNOWN_PATH.equals(mdPath) ? metadata.path() : mdPath);
        initRequest(httpRequest, requestContentType, serializer.messageEncoding(), acceptedEncoding, timeout);
        httpRequest.payloadBody(serializer.serialize(request, streamingHttpClient.executionContext().bufferAllocator()));
        try {
            assignStrategy(httpRequest, metadata);
            final BlockingStreamingHttpResponse response = client.request(httpRequest);
            return validateResponseAndGetPayload(response.toStreamingResponse(), responseContentType, client.executionContext().bufferAllocator(), readGrpcMessageEncodingRaw(response.headers(), deserializerIdentity, deserializers, GrpcStreamingDeserializer::messageEncoding)).toIterable();
        } catch (Throwable cause) {
            throw toGrpcException(cause);
        }
    };
}
Also used : GrpcUtils.serializerDeserializer(io.servicetalk.grpc.api.GrpcUtils.serializerDeserializer) Publisher(io.servicetalk.concurrent.api.Publisher) DefaultMethodDescriptor(io.servicetalk.grpc.api.GrpcUtils.DefaultMethodDescriptor) GrpcUtils.decompressors(io.servicetalk.grpc.api.GrpcUtils.decompressors) GrpcUtils.readGrpcMessageEncodingRaw(io.servicetalk.grpc.api.GrpcUtils.readGrpcMessageEncodingRaw) GrpcUtils.defaultToInt(io.servicetalk.grpc.api.GrpcUtils.defaultToInt) AsyncContext(io.servicetalk.concurrent.api.AsyncContext) HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) GRPC_CONTENT_TYPE_PROTO_SUFFIX(io.servicetalk.grpc.api.GrpcHeaderValues.GRPC_CONTENT_TYPE_PROTO_SUFFIX) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) GrpcUtils.toGrpcException(io.servicetalk.grpc.api.GrpcUtils.toGrpcException) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Nullable(javax.annotation.Nullable) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Completable(io.servicetalk.concurrent.api.Completable) HttpResponse(io.servicetalk.http.api.HttpResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Identity.identityEncoder(io.servicetalk.encoding.api.Identity.identityEncoder) GrpcUtils.grpcContentType(io.servicetalk.grpc.api.GrpcUtils.grpcContentType) GrpcUtils.validateResponseAndGetPayload(io.servicetalk.grpc.api.GrpcUtils.validateResponseAndGetPayload) HTTP_EXECUTION_STRATEGY_KEY(io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) GrpcUtils.initRequest(io.servicetalk.grpc.api.GrpcUtils.initRequest) BlockingIterables.singletonBlockingIterable(io.servicetalk.concurrent.internal.BlockingIterables.singletonBlockingIterable) GRPC_DEADLINE_KEY(io.servicetalk.grpc.internal.DeadlineUtils.GRPC_DEADLINE_KEY) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) Duration(java.time.Duration) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse)

Example 3 with BufferDecoderGroup

use of io.servicetalk.encoding.api.BufferDecoderGroup in project servicetalk by apple.

the class DefaultGrpcClientCallFactory method newBlockingCall.

@Override
public <Req, Resp> BlockingClientCall<Req, Resp> newBlockingCall(final MethodDescriptor<Req, Resp> methodDescriptor, final BufferDecoderGroup decompressors) {
    final BlockingHttpClient client = streamingHttpClient.asBlockingClient();
    GrpcSerializer<Req> serializerIdentity = serializer(methodDescriptor);
    GrpcDeserializer<Resp> deserializerIdentity = deserializer(methodDescriptor);
    List<GrpcDeserializer<Resp>> deserializers = deserializers(methodDescriptor, decompressors.decoders());
    CharSequence acceptedEncoding = decompressors.advertisedMessageEncoding();
    CharSequence requestContentType = grpcContentType(methodDescriptor.requestDescriptor().serializerDescriptor().contentType());
    CharSequence responseContentType = grpcContentType(methodDescriptor.responseDescriptor().serializerDescriptor().contentType());
    return (metadata, request) -> {
        Duration timeout = timeoutForRequest(metadata.timeout());
        GrpcSerializer<Req> serializer = serializer(methodDescriptor, serializerIdentity, metadata.requestCompressor());
        String mdPath = methodDescriptor.httpPath();
        HttpRequest httpRequest = client.post(UNKNOWN_PATH.equals(mdPath) ? metadata.path() : mdPath);
        initRequest(httpRequest, requestContentType, serializer.messageEncoding(), acceptedEncoding, timeout);
        httpRequest.payloadBody(serializer.serialize(request, client.executionContext().bufferAllocator()));
        try {
            assignStrategy(httpRequest, metadata);
            final HttpResponse response = client.request(httpRequest);
            return validateResponseAndGetPayload(response, responseContentType, client.executionContext().bufferAllocator(), readGrpcMessageEncodingRaw(response.headers(), deserializerIdentity, deserializers, GrpcDeserializer::messageEncoding));
        } catch (Throwable cause) {
            throw toGrpcException(cause);
        }
    };
}
Also used : GrpcUtils.serializerDeserializer(io.servicetalk.grpc.api.GrpcUtils.serializerDeserializer) Publisher(io.servicetalk.concurrent.api.Publisher) DefaultMethodDescriptor(io.servicetalk.grpc.api.GrpcUtils.DefaultMethodDescriptor) GrpcUtils.decompressors(io.servicetalk.grpc.api.GrpcUtils.decompressors) GrpcUtils.readGrpcMessageEncodingRaw(io.servicetalk.grpc.api.GrpcUtils.readGrpcMessageEncodingRaw) GrpcUtils.defaultToInt(io.servicetalk.grpc.api.GrpcUtils.defaultToInt) AsyncContext(io.servicetalk.concurrent.api.AsyncContext) HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) GRPC_CONTENT_TYPE_PROTO_SUFFIX(io.servicetalk.grpc.api.GrpcHeaderValues.GRPC_CONTENT_TYPE_PROTO_SUFFIX) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) GrpcUtils.toGrpcException(io.servicetalk.grpc.api.GrpcUtils.toGrpcException) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Nullable(javax.annotation.Nullable) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Completable(io.servicetalk.concurrent.api.Completable) HttpResponse(io.servicetalk.http.api.HttpResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Identity.identityEncoder(io.servicetalk.encoding.api.Identity.identityEncoder) GrpcUtils.grpcContentType(io.servicetalk.grpc.api.GrpcUtils.grpcContentType) GrpcUtils.validateResponseAndGetPayload(io.servicetalk.grpc.api.GrpcUtils.validateResponseAndGetPayload) HTTP_EXECUTION_STRATEGY_KEY(io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) GrpcUtils.initRequest(io.servicetalk.grpc.api.GrpcUtils.initRequest) BlockingIterables.singletonBlockingIterable(io.servicetalk.concurrent.internal.BlockingIterables.singletonBlockingIterable) GRPC_DEADLINE_KEY(io.servicetalk.grpc.internal.DeadlineUtils.GRPC_DEADLINE_KEY) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) HttpResponse(io.servicetalk.http.api.HttpResponse) Duration(java.time.Duration)

Example 4 with BufferDecoderGroup

use of io.servicetalk.encoding.api.BufferDecoderGroup in project servicetalk by apple.

the class DefaultGrpcClientCallFactory method newCall.

@Override
public <Req, Resp> ClientCall<Req, Resp> newCall(final MethodDescriptor<Req, Resp> methodDescriptor, final BufferDecoderGroup decompressors) {
    final HttpClient client = streamingHttpClient.asClient();
    GrpcSerializer<Req> serializerIdentity = serializer(methodDescriptor);
    GrpcDeserializer<Resp> deserializerIdentity = deserializer(methodDescriptor);
    List<GrpcDeserializer<Resp>> deserializers = deserializers(methodDescriptor, decompressors.decoders());
    CharSequence acceptedEncoding = decompressors.advertisedMessageEncoding();
    CharSequence requestContentType = grpcContentType(methodDescriptor.requestDescriptor().serializerDescriptor().contentType());
    CharSequence responseContentType = grpcContentType(methodDescriptor.responseDescriptor().serializerDescriptor().contentType());
    return (metadata, request) -> {
        Duration timeout = timeoutForRequest(metadata.timeout());
        GrpcSerializer<Req> serializer = serializer(methodDescriptor, serializerIdentity, metadata.requestCompressor());
        String mdPath = methodDescriptor.httpPath();
        HttpRequest httpRequest = client.post(UNKNOWN_PATH.equals(mdPath) ? metadata.path() : mdPath);
        initRequest(httpRequest, requestContentType, serializer.messageEncoding(), acceptedEncoding, timeout);
        httpRequest.payloadBody(serializer.serialize(request, client.executionContext().bufferAllocator()));
        assignStrategy(httpRequest, metadata);
        return client.request(httpRequest).map(response -> validateResponseAndGetPayload(response, responseContentType, client.executionContext().bufferAllocator(), readGrpcMessageEncodingRaw(response.headers(), deserializerIdentity, deserializers, GrpcDeserializer::messageEncoding))).onErrorMap(GrpcUtils::toGrpcException);
    };
}
Also used : GrpcUtils.serializerDeserializer(io.servicetalk.grpc.api.GrpcUtils.serializerDeserializer) Publisher(io.servicetalk.concurrent.api.Publisher) DefaultMethodDescriptor(io.servicetalk.grpc.api.GrpcUtils.DefaultMethodDescriptor) GrpcUtils.decompressors(io.servicetalk.grpc.api.GrpcUtils.decompressors) GrpcUtils.readGrpcMessageEncodingRaw(io.servicetalk.grpc.api.GrpcUtils.readGrpcMessageEncodingRaw) GrpcUtils.defaultToInt(io.servicetalk.grpc.api.GrpcUtils.defaultToInt) AsyncContext(io.servicetalk.concurrent.api.AsyncContext) HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) GRPC_CONTENT_TYPE_PROTO_SUFFIX(io.servicetalk.grpc.api.GrpcHeaderValues.GRPC_CONTENT_TYPE_PROTO_SUFFIX) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) GrpcUtils.toGrpcException(io.servicetalk.grpc.api.GrpcUtils.toGrpcException) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Nullable(javax.annotation.Nullable) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Completable(io.servicetalk.concurrent.api.Completable) HttpResponse(io.servicetalk.http.api.HttpResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Identity.identityEncoder(io.servicetalk.encoding.api.Identity.identityEncoder) GrpcUtils.grpcContentType(io.servicetalk.grpc.api.GrpcUtils.grpcContentType) GrpcUtils.validateResponseAndGetPayload(io.servicetalk.grpc.api.GrpcUtils.validateResponseAndGetPayload) HTTP_EXECUTION_STRATEGY_KEY(io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) GrpcUtils.initRequest(io.servicetalk.grpc.api.GrpcUtils.initRequest) BlockingIterables.singletonBlockingIterable(io.servicetalk.concurrent.internal.BlockingIterables.singletonBlockingIterable) GRPC_DEADLINE_KEY(io.servicetalk.grpc.internal.DeadlineUtils.GRPC_DEADLINE_KEY) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) Duration(java.time.Duration) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) HttpClient(io.servicetalk.http.api.HttpClient) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient)

Aggregations

Completable (io.servicetalk.concurrent.api.Completable)4 Publisher (io.servicetalk.concurrent.api.Publisher)4 BufferDecoderGroup (io.servicetalk.encoding.api.BufferDecoderGroup)4 BufferEncoder (io.servicetalk.encoding.api.BufferEncoder)4 BlockingIterator (io.servicetalk.concurrent.BlockingIterator)3 AsyncContext (io.servicetalk.concurrent.api.AsyncContext)3 BlockingIterables.singletonBlockingIterable (io.servicetalk.concurrent.internal.BlockingIterables.singletonBlockingIterable)3 BufferDecoder (io.servicetalk.encoding.api.BufferDecoder)3 Identity.identityEncoder (io.servicetalk.encoding.api.Identity.identityEncoder)3 GRPC_CONTENT_TYPE_PROTO_SUFFIX (io.servicetalk.grpc.api.GrpcHeaderValues.GRPC_CONTENT_TYPE_PROTO_SUFFIX)3 DefaultMethodDescriptor (io.servicetalk.grpc.api.GrpcUtils.DefaultMethodDescriptor)3 GrpcUtils.decompressors (io.servicetalk.grpc.api.GrpcUtils.decompressors)3 GrpcUtils.defaultToInt (io.servicetalk.grpc.api.GrpcUtils.defaultToInt)3 GrpcUtils.grpcContentType (io.servicetalk.grpc.api.GrpcUtils.grpcContentType)3 GrpcUtils.initRequest (io.servicetalk.grpc.api.GrpcUtils.initRequest)3 GrpcUtils.readGrpcMessageEncodingRaw (io.servicetalk.grpc.api.GrpcUtils.readGrpcMessageEncodingRaw)3 GrpcUtils.serializerDeserializer (io.servicetalk.grpc.api.GrpcUtils.serializerDeserializer)3 GrpcUtils.toGrpcException (io.servicetalk.grpc.api.GrpcUtils.toGrpcException)3 GrpcUtils.validateResponseAndGetPayload (io.servicetalk.grpc.api.GrpcUtils.validateResponseAndGetPayload)3 GRPC_DEADLINE_KEY (io.servicetalk.grpc.internal.DeadlineUtils.GRPC_DEADLINE_KEY)3