Skip to content

Commit

Permalink
Issue #132: Use amqp-ext as a transport
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-nuwber committed Jul 3, 2024
1 parent bbd0a94 commit d7efff8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Once again, the RabbitEvents library helps you to publish an event and handle it
* [rabbitevents:listen](#command-listen) - listen to an event
* [rabbitevents:list](#command-list) - display list of registered events
1. [Examples](/examples)
1. [Speeding up RabbitEvents](#speeding-up-rabbitevents)
1. [Non-standard use](#non-standard-use)
1. [License](#license)

## Installation via Composer<a name="installation"></a>
You may use Composer to install RabbitEvents into your Laravel project:
Expand Down Expand Up @@ -161,6 +163,23 @@ To get the list of all registered events please use the command `rabbitevents:li
php artisan rabbitevents:list
```

## Speeding up RabbitEvents<a name="speeding-up-rabbitevents"></a>
To enhance the performance of RabbitEvents, consider installing the `php-amqp` extension along with the `enqueue/amqp-ext` package.
By doing so, RabbitEvents will utilize the `enqueue/amqp-ext` package instead of the default `enqueue/amqp-lib` package.
This substitution is advantageous because the C-written `php-amqp` package significantly outperforms the PHP-written `enqueue/amqp-lib` package.

You can install the `php-amqp` extension using the following command:
```bash
pecl install amqp
```
or use the way you prefer. More about it can be found [here](https://pecl.php.net/package/amqp).

Next, install the `enqueue/amqp-ext` package with the following command:
```bash
composer require enqueue/amqp-ext
```
No additional configuration is required.

## Non-standard use <a name="#non-standard-use"></a>

If you're using only one of parts of RabbitEvents, you should know a few things:
Expand All @@ -181,3 +200,7 @@ If you're using only one of parts of RabbitEvents, you should know a few things:

There'e 3 elements of an array, so 3 variables will be passed to a Listener (array, string and integer).
If an associative array is being passed, the Dispatcher wraps this array by itself.

# License <a name="license"></a>

RabbitEvents is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nuwber/rabbitevents",
"description": "The Nuwber RabbitEvents package",
"keywords": ["laravel", "rabbitmq", "events", "broadcast", "publish", "pub", "sub"],
"keywords": ["laravel", "rabbitmq", "events", "broadcast", "publish", "pub", "sub", "amqp"],
"type": "library",
"license": "MIT",
"authors": [
Expand Down Expand Up @@ -56,7 +56,8 @@
},
"suggest": {
"ext-pcntl": "Required to use all features of the worker.",
"vlucas/phpdotenv": "Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically."
"ext-amqp": "Using this extension makes your app faster. If you're using it you need to install enqueue/amqp-ext.",
"enqueue/amqp-ext": "This package is necessary if you want to use `ext-amqp`."
},
"config": {
"sort-packages": true
Expand Down
13 changes: 12 additions & 1 deletion src/RabbitEvents/Foundation/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public function getConfig($key = null, $default = null): mixed
*/
protected function factory(): AmqpConnectionFactory
{
$factory = new AmqpConnectionFactory([
$connectionFactoryClass = $this->getConnectionFactoryClass();

$factory = new $connectionFactoryClass([
'dsn' => $this->getConfig('dsn'),
'host' => $this->getConfig('host', '127.0.0.1'),
'port' => $this->getConfig('port', 5672),
Expand Down Expand Up @@ -116,4 +118,13 @@ protected function factory(): AmqpConnectionFactory

return $factory;
}

private function getConnectionFactoryClass(): string
{
if (extension_loaded('amqp') && class_exists('Enqueue\AmqpExt\AmqpConnectionFactory')) {
return \Enqueue\AmqpExt\AmqpConnectionFactory::class;
} else {
return \Enqueue\AmqpLib\AmqpConnectionFactory::class;
}
}
}
2 changes: 1 addition & 1 deletion src/RabbitEvents/Foundation/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function receiveMessage(int $timeout = 0): ?AmqpMessage
{
try {
return $this->amqpConsumer->receive($timeout);
} catch (AMQPRuntimeException $exception) {
} catch (\Throwable $exception) {
throw new ConnectionLostException($exception);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/RabbitEvents/Listener/Commands/ListenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class ListenCommand extends Command
*/
public function handle(Context $context, Worker $worker)
{
$this->checkExtLoaded();

$options = $this->gatherProcessingOptions();

$this->registerLogWriters($options->connectionName);
Expand Down Expand Up @@ -156,4 +158,12 @@ private function parseLoggingConfiguration(string $connection = 'rabbitmq'): arr
Arr::get($config, "connections.$connection.logging.channel", null),
];
}

private function checkExtLoaded(): void
{
if (extension_loaded('amqp') && !class_exists('Enqueue\AmqpExt\AmqpConnectionFactory')) {
$this->info('You have ext-amqp extension installed. Require enqueue/amqp-ext package to use it.');
$this->info("The package can be installed via 'composer require enqueue/amqp-ext' command.");
}
}
}
3 changes: 2 additions & 1 deletion tests/Foundation/Support/ReleaserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ protected function setUp(): void

$this->producer = m::mock(AmqpProducer::class);
$this->producer->shouldReceive()
->send($this->queue, $amqpMessage);
->send($this->queue, $amqpMessage)
->once();
}

public function testDeliveryDelay()
Expand Down
3 changes: 2 additions & 1 deletion tests/Publisher/PublisherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function testPublish(): void
->make($event)
->andReturn($messageMock);
$sender = m::mock(Transport::class);
$sender->shouldReceive('send');
$sender->shouldReceive('send')
->once();

$publisher = new Publisher($messageFactory, $sender);
$publisher->publish($event);
Expand Down

0 comments on commit d7efff8

Please sign in to comment.