Skip to content

[Messenger] [Amqp-messenger] Support content encoding and compression #47592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Draft: implement compression/decompression using a middleware
  • Loading branch information
glengemann committed Oct 21, 2022
commit ecaf750a5f4c49663eb0b21cca9e803b1ba8a06d
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
['id' => 'failed_message_processing_middleware'],
],
'after' => [
['id' => 'compress_middleware'],
['id' => 'send_message'],
['id' => 'handle_message'],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
use Symfony\Component\Messenger\EventListener\StopWorkerOnSigtermSignalListener;
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware;
use Symfony\Component\Messenger\Middleware\CompressMiddleware;
use Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware;
use Symfony\Component\Messenger\Middleware\FailedMessageProcessingMiddleware;
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
Expand Down Expand Up @@ -110,6 +111,9 @@
service('router'),
])

->set('messenger.middleware.compress_middleware', CompressMiddleware::class)
->abstract()

// Discovery
->set('messenger.receiver_locator', ServiceLocator::class)
->args([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ private function getEnvelope(string $queueName): iterable
$body = $amqpEnvelope->getBody();

try {
/*
$contentEncoding = $amqpEnvelope->getContentEncoding();
if ($contentEncoding) {
$compressor = CompressorFactory::createCompressor($contentEncoding);
$body = $compressor->decompress($body);
}
*/

$envelope = $this->serializer->decode([
'body' => false === $body ? '' : $body, // workaround https://github.com/pdezwart/php-amqp/issues/351
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ private function publishOnExchange(\AMQPExchange $exchange, string $body, string
$attributes['timestamp'] ??= time();

$this->lastActivityTime = time();

/*
if (isset($attributes['content_encoding'])) {
$compressor = CompressorFactory::createCompressor($attributes['content_encoding']);
$body = $compressor->compress($body);
}

*/
$exchange->publish(
$body,
$routingKey,
Expand Down
61 changes: 61 additions & 0 deletions src/Symfony/Component/Messenger/Middleware/CompressMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Symfony\Component\Messenger\Middleware;

use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpReceivedStamp;
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpStamp;
use Symfony\Component\Messenger\Envelope;

class CompressMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
if ($amqpReceivedStamp = $envelope->last(AmqpReceivedStamp::class)) {
$contentEncoding ??= $amqpReceivedStamp->getAmqpEnvelope()->getContentEncoding();
if (!$contentEncoding) {
return $stack->next()->handle($envelope, $stack);
}

$message = $envelope->getMessage();
$compress = $this->decompress($contentEncoding, $message);
$envelope = new Envelope(
$compress,
$envelope->all()
);
} else {
$amqpStamp = $envelope->last(AmqpStamp::class);
$contentEncoding = $amqpStamp->getAttributes()['content_encoding'] ?? null;
if (!$contentEncoding) {
return $stack->next()->handle($envelope, $stack);
}
$message = $envelope->getMessage();
// We need a string, but in this point we have an object
$compress = $this->compress($contentEncoding, $message);
$envelope = new Envelope(
$compress,
$envelope->all()
);
}

return $stack->next()->handle($envelope, $stack);
}

public function compress(string $contentEncoding, mixed $data): string
{
return match ($contentEncoding) {
'gzip' => gzencode($data),
'deflate' => gzdeflate($data),
default => throw new InvalidArgumentException(sprintf('The MIME content encoding of the message cannot be decompressed "%s".', $contentEncoding)),
};
}

public function decompress(string $contentEncoding, mixed $data): mixed
{
return match ($contentEncoding) {
'gzip' => gzdecode($data) ?: $data,
'deflate' => gzinflate($data) ?: $data,
};

return $data;
}
}