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);
}
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);
}
};
}
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);
}
};
}
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);
};
}
Aggregations