2121#include < grpc/support/port_platform.h>
2222#include < inttypes.h>
2323
24+ #include < cstdint>
2425#include < functional>
2526#include < memory>
2627#include < optional>
4445#include " src/core/lib/surface/call.h"
4546#include " src/core/lib/transport/transport.h"
4647#include " src/core/telemetry/call_tracer.h"
48+ #include " src/core/transport/message_size_service_config.h"
4749#include " src/core/util/grpc_check.h"
4850#include " src/core/util/latent_see.h"
4951#include " absl/status/status.h"
@@ -75,8 +77,6 @@ ServerCompressionFilter::Create(const ChannelArgs& args, ChannelFilter::Args) {
7577
7678ChannelCompression::ChannelCompression (const ChannelArgs& args)
7779 : max_recv_size_(GetMaxRecvSizeFromChannelArgs(args)),
78- message_size_service_config_parser_index_ (
79- MessageSizeParser::ParserIndex ()),
8080 default_compression_algorithm_ (
8181 DefaultCompressionAlgorithmFromChannelArgs (args).value_or(
8282 GRPC_COMPRESS_NONE )),
@@ -118,26 +118,25 @@ MessageHandle ChannelCompression::CompressMessage(
118118 return message;
119119 }
120120 // Try to compress the payload.
121- SliceBuffer tmp;
122- SliceBuffer* payload = message->payload ();
123- bool did_compress = grpc_msg_compress (algorithm, payload->c_slice_buffer (),
124- tmp.c_slice_buffer ());
121+ std::optional<SliceBuffer> compressed =
122+ MessageCompress (algorithm, *message->payload ());
123+
125124 // If we achieved compression send it as compressed, otherwise send it as (to
126125 // avoid spending cycles on the receiver decompressing).
127- if (did_compress ) {
126+ if (compressed. has_value () ) {
128127 if (GRPC_TRACE_FLAG_ENABLED (compression)) {
129128 const char * algo_name;
130- const size_t before_size = payload->Length ();
131- const size_t after_size = tmp.Length ();
129+ GRPC_CHECK (grpc_compression_algorithm_name (algorithm, &algo_name));
130+ const size_t before_size = message->payload ()->Length ();
131+ const size_t after_size = compressed->Length ();
132132 const float savings_ratio = 1 .0f - (static_cast <float >(after_size) /
133133 static_cast <float >(before_size));
134- GRPC_CHECK (grpc_compression_algorithm_name (algorithm, &algo_name));
135134 LOG (INFO ) << absl::StrFormat (
136135 " Compressed[%s] %" PRIuPTR " bytes vs. %" PRIuPTR
137136 " bytes (%.2f%% savings)" ,
138137 algo_name, before_size, after_size, 100 * savings_ratio);
139138 }
140- tmp. Swap ( payload);
139+ *message-> payload () = std::move (*compressed );
141140 flags |= GRPC_WRITE_INTERNAL_COMPRESS ;
142141 if (call_tracer != nullptr ) {
143142 call_tracer->RecordSendCompressedMessage (*message);
@@ -148,7 +147,7 @@ MessageHandle ChannelCompression::CompressMessage(
148147 GRPC_CHECK (grpc_compression_algorithm_name (algorithm, &algo_name));
149148 LOG (INFO ) << " Algorithm '" << algo_name
150149 << " ' enabled but decided not to compress. Input size: "
151- << payload->Length ();
150+ << message-> payload () ->Length ();
152151 }
153152 }
154153 return message;
@@ -180,15 +179,17 @@ absl::StatusOr<MessageHandle> ChannelCompression::DecompressMessage(
180179 return std::move (message);
181180 }
182181 // Try to decompress the payload.
183- SliceBuffer decompressed_slices;
184- if (grpc_msg_decompress (args.algorithm , message->payload ()->c_slice_buffer (),
185- decompressed_slices.c_slice_buffer ()) == 0 ) {
186- return absl::InternalError (
187- absl::StrCat (" Unexpected error decompressing data for algorithm " ,
188- CompressionAlgorithmAsString (args.algorithm )));
182+ std::optional<uint32_t > max_output_size = IsMessageSizeRefactoringEnabled ()
183+ ? args.max_recv_message_length
184+ : std::nullopt ;
185+ absl::StatusOr<SliceBuffer> decompressed_slices =
186+ MessageDecompress (args.algorithm , *message->payload (), max_output_size);
187+ if (!decompressed_slices.ok ()) {
188+ return decompressed_slices.status ();
189189 }
190- // Swap the decompressed slices into the message.
191- message->payload ()->Swap (&decompressed_slices);
190+
191+ // Move the decompressed slices into the message.
192+ *message->payload () = std::move (*decompressed_slices);
192193 message->mutable_flags () &= ~GRPC_WRITE_INTERNAL_COMPRESS ;
193194 message->mutable_flags () |= GRPC_WRITE_INTERNAL_TEST_ONLY_WAS_COMPRESSED ;
194195 if (call_tracer != nullptr ) {
@@ -213,18 +214,10 @@ grpc_compression_algorithm ChannelCompression::HandleOutgoingMetadata(
213214ChannelCompression::DecompressArgs ChannelCompression::HandleIncomingMetadata (
214215 const grpc_metadata_batch& incoming_metadata) {
215216 // Configure max receive size.
216- auto max_recv_message_length = max_recv_size_;
217- const MessageSizeParsedConfig* limits =
218- MessageSizeParsedConfig::GetFromCallContext (
219- GetContext<Arena>(), message_size_service_config_parser_index_);
220- if (limits != nullptr && limits->max_recv_size ().has_value () &&
221- (!max_recv_message_length.has_value () ||
222- *limits->max_recv_size () < *max_recv_message_length)) {
223- max_recv_message_length = limits->max_recv_size ();
224- }
225- return DecompressArgs{incoming_metadata.get (GrpcEncodingMetadata ())
226- .value_or (GRPC_COMPRESS_NONE ),
227- max_recv_message_length};
217+ return DecompressArgs{
218+ incoming_metadata.get (GrpcEncodingMetadata ())
219+ .value_or (GRPC_COMPRESS_NONE ),
220+ GetMaxRecvSizeFromCallContext (GetContext<Arena>(), max_recv_size_)};
228221}
229222
230223void ClientCompressionFilter::Call::OnClientInitialMetadata (
0 commit comments