-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Jeckel-Lab/feature/send-millions-messages
Send millions of messages in RabbitMQ
- Loading branch information
Showing
15 changed files
with
222 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ | |
/phpstan.neon | ||
/phpunit.xml | ||
|
||
/.idea | ||
/.idea | ||
/.castor.stub.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at: 30/10/2023 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace php; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Castor\Attribute\AsTask; | ||
use PhpAmqpLib\Connection\AMQPStreamConnection; | ||
use PhpAmqpLib\Message\AMQPMessage; | ||
|
||
use function Castor\run; | ||
|
||
#[AsTask(description: 'Execute console command')] | ||
function console( | ||
string $consoleCommand | ||
): void { | ||
run( | ||
'docker-compose -f docker-compose.yml exec demo php -d max_execution_time=120 console.php ' . $consoleCommand | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
use function Castor\import; | ||
|
||
import(__DIR__ . '/castor-commands'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
/** | ||
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at: 30/10/2023 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
use JeckelLab\IpcSharedMemoryDemo\Console\LoadMessages; | ||
use Symfony\Component\Console\Application; | ||
|
||
$application = new Application(); | ||
|
||
$application->add(new LoadMessages()); | ||
|
||
$application->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
/** | ||
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at: 29/10/2023 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
echo "Hello World!\n"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/** | ||
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at: 30/10/2023 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeckelLab\IpcSharedMemoryDemo\Console; | ||
|
||
use Exception; | ||
use JeckelLab\IpcSharedMemoryDemo\Message\Message; | ||
use PhpAmqpLib\Channel\AMQPChannel; | ||
use PhpAmqpLib\Connection\AMQPStreamConnection; | ||
use PhpAmqpLib\Exception\AMQPConnectionBlockedException; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
#[AsCommand(name: 'demo:load-messages')] | ||
class LoadMessages extends Command | ||
{ | ||
private ?AMQPStreamConnection $connection = null; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* @throws Exception | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$channel = $this->getChannel(); | ||
|
||
$exchange = (string) getenv('RABBITMQ_EXCHANGE'); | ||
|
||
$batch = 100; | ||
$max = 1000 * 1000; | ||
for ($i = 0; $i < $max; $i++) { | ||
$message = $this->getMessage(); | ||
$channel->batch_basic_publish($message, $exchange, $message->getRoutingKey()); | ||
|
||
if ($i % $batch === 0) { | ||
try { | ||
$channel->publish_batch(); | ||
} catch (AMQPConnectionBlockedException) { | ||
do { | ||
sleep(10); | ||
} while ($this->getConnection()->isBlocked()); | ||
$channel->publish_batch(); | ||
} | ||
$output->writeln(sprintf('Published %d messages', $i)); | ||
} | ||
} | ||
|
||
$channel->publish_batch(); | ||
$output->writeln(sprintf('Published %d messages', $max)); | ||
return Command::SUCCESS; | ||
} | ||
|
||
/** | ||
* @return AMQPChannel | ||
* @throws Exception | ||
*/ | ||
protected function getChannel(): AMQPChannel | ||
{ | ||
return ($this->getConnection())->channel(); | ||
} | ||
|
||
/** | ||
* @return Message | ||
* @throws Exception | ||
*/ | ||
protected function getMessage(): Message | ||
{ | ||
$shard = random_int(0, 9); | ||
return (new Message( | ||
sprintf('Hello world, dummy message for shard %s', $shard), | ||
[ | ||
'content_type' => 'text/plain' | ||
] | ||
))->setRoutingKey(sprintf('shard-%d', $shard)); | ||
} | ||
|
||
/** | ||
* @return AMQPStreamConnection | ||
* @throws Exception | ||
*/ | ||
protected function getConnection(): AMQPStreamConnection | ||
{ | ||
if (null === $this->connection) { | ||
$this->connection = new AMQPStreamConnection( | ||
(string) getenv('RABBITMQ_HOST'), | ||
(int) getenv('RABBITMQ_PORT'), | ||
(string) getenv('RABBITMQ_USER'), | ||
(string) getenv('RABBITMQ_PASS') | ||
); | ||
} | ||
return $this->connection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr> | ||
* Created at: 30/10/2023 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeckelLab\IpcSharedMemoryDemo\Message; | ||
|
||
use PhpAmqpLib\Message\AMQPMessage; | ||
|
||
class Message extends AMQPMessage | ||
{ | ||
private string $routingKey = ''; | ||
|
||
public function getRoutingKey(): string | ||
{ | ||
return $this->routingKey; | ||
} | ||
|
||
public function setRoutingKey(string $routingKey): self | ||
{ | ||
$this->routingKey = $routingKey; | ||
return $this; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.