From 914ae38d60de29a4f600f7ad39b593da3d9276e0 Mon Sep 17 00:00:00 2001 From: Daniel Fiala Date: Sat, 20 Apr 2024 12:13:58 +0200 Subject: [PATCH] feat(transcoding): removed unnecessary encoding --- .../transcoding/GrpcTranscodingResponse.java | 14 +++++++------- .../transcoding/TranscodingMessageDecoder.java | 7 ++----- .../transcoding/TranscodingMessageEncoder.java | 11 ++++------- .../transcoding/TranscodingReadStreamAdapter.java | 3 +-- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/GrpcTranscodingResponse.java b/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/GrpcTranscodingResponse.java index bcf4e9bff3500..4e8b627179071 100644 --- a/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/GrpcTranscodingResponse.java +++ b/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/GrpcTranscodingResponse.java @@ -13,7 +13,6 @@ import io.vertx.core.buffer.Buffer; import io.vertx.core.http.HttpServerResponse; import io.vertx.grpc.common.*; -import io.vertx.grpc.common.impl.GrpcMessageImpl; import io.vertx.grpc.common.impl.Utils; import io.vertx.grpc.server.GrpcServerResponse; @@ -154,6 +153,11 @@ private Future writeMessage(GrpcMessage message, boolean end) { throw new IllegalStateException(); } + Buffer encoded = encode(message); + if (encoded == null) { + throw new IllegalStateException("The message is null"); + } + boolean trailersOnly = status != GrpcStatus.OK && !headersSent && end; MultiMap responseHeaders = httpResponse.headers(); @@ -165,6 +169,7 @@ private Future writeMessage(GrpcMessage message, boolean end) { } } responseHeaders.set("content-type", "application/json"); + responseHeaders.set("content-length", String.valueOf(encoded.length())); } if (end) { @@ -195,16 +200,11 @@ private Future writeMessage(GrpcMessage message, boolean end) { responseTrailers.remove("grpc-message"); } if (message != null) { - return httpResponse.end(encode(message)); + return httpResponse.end(encoded); } else { return httpResponse.end(); } } else { - Buffer encoded = encode(message); - // Write Content-Length header - if (encoded != null) { - responseHeaders.set("content-length", String.valueOf(encoded.length())); - } return httpResponse.write(encoded); } } diff --git a/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingMessageDecoder.java b/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingMessageDecoder.java index 6fc0340b34def..f9abad865ad65 100644 --- a/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingMessageDecoder.java +++ b/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingMessageDecoder.java @@ -1,14 +1,11 @@ package io.quarkus.grpc.runtime.transcoding; -import io.grpc.Decompressor; +import java.io.ByteArrayInputStream; + import io.grpc.MethodDescriptor; import io.vertx.grpc.common.GrpcMessage; import io.vertx.grpc.common.GrpcMessageDecoder; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; - public class TranscodingMessageDecoder implements GrpcMessageDecoder { private MethodDescriptor.Marshaller marshaller; diff --git a/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingMessageEncoder.java b/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingMessageEncoder.java index 67c642e9b3264..8ab8543baeba8 100644 --- a/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingMessageEncoder.java +++ b/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingMessageEncoder.java @@ -1,17 +1,13 @@ package io.quarkus.grpc.runtime.transcoding; -import io.grpc.Compressor; -import io.grpc.Drainable; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + import io.grpc.MethodDescriptor; import io.vertx.core.buffer.Buffer; import io.vertx.grpc.common.GrpcMessage; import io.vertx.grpc.common.GrpcMessageEncoder; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - public class TranscodingMessageEncoder implements GrpcMessageEncoder { private MethodDescriptor.Marshaller marshaller; @@ -24,6 +20,7 @@ public TranscodingMessageEncoder(MethodDescriptor.Marshaller marshaller) { public GrpcMessage encode(T msg) { return new GrpcMessage() { private Buffer encoded; + @Override public String encoding() { return "identity"; diff --git a/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingReadStreamAdapter.java b/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingReadStreamAdapter.java index 8ec232ae4238d..0e31fd41b2f1c 100644 --- a/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingReadStreamAdapter.java +++ b/extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/transcoding/TranscodingReadStreamAdapter.java @@ -1,7 +1,6 @@ package io.quarkus.grpc.runtime.transcoding; import io.vertx.grpc.common.GrpcReadStream; -import io.vertx.grpc.common.impl.BridgeMessageDecoder; public class TranscodingReadStreamAdapter { @@ -49,4 +48,4 @@ public final void request(int num) { request += num; } } -} \ No newline at end of file +}