Skip to content

Commit

Permalink
Rebuild into async only with sync API
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Apr 12, 2024
1 parent 464507a commit 520d24f
Show file tree
Hide file tree
Showing 142 changed files with 3,031 additions and 6,080 deletions.
21 changes: 1 addition & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,7 @@ $channel->qos(

### Asynchronous usage

Bunny supports both synchronous and asynchronous usage utilizing [ReactPHP](https://github.com/reactphp). The following example shows setting up a client and consuming a queue indefinitely.

```php
(new Async\Client($eventLoop, $options))->connect()->then(function (Async\Client $client) {
return $client->channel();
})->then(function (Channel $channel) {
return $channel->qos(0, 5)->then(function () use ($channel) {
return $channel;
});
})->then(function (Channel $channel) use ($event) {
$channel->consume(
function (Message $message, Channel $channel, Async\Client $client) use ($event) {
// Handle message

$channel->ack($message);
},
'queue_name'
);
});
```
**Node: Up to version `v0.5.x` Bunny had two different clients, one sync, and one async. As of `v0.6` both clients have been folder into one: An async client with a sync API.**

## AMQP interop

Expand Down
8 changes: 4 additions & 4 deletions benchmark/consumer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Bunny;

use Bunny\Message;

require_once __DIR__ . "/../vendor/autoload.php";

$c = new Client();
Expand All @@ -13,18 +15,16 @@
$t = null;
$count = 0;

$ch->run(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count) {
$ch->consume(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count) {
if ($t === null) {
$t = microtime(true);
}

if ($msg->content === "quit") {
printf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $count, microtime(true) - $t);
$c->stop();
$c->disconnect();
} else {
++$count;
}

}, "bench_queue", "", false, true);

$c->disconnect();
46 changes: 0 additions & 46 deletions benchmark/consumer_async.php

This file was deleted.

2 changes: 2 additions & 0 deletions benchmark/producer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Bunny;

use Bunny\Client;

require_once __DIR__ . "/../vendor/autoload.php";

$c = new Client();
Expand Down
60 changes: 0 additions & 60 deletions benchmark/producer_async.php

This file was deleted.

29 changes: 16 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bunny/bunny",
"type": "library",
"description": "Performant pure-PHP AMQP (RabbitMQ) sync/async (ReactPHP) library",
"description": "Performant pure-PHP AMQP (RabbitMQ) non-blocking ReactPHP library",
"keywords": [
"rabbitmq",
"rabbit",
Expand All @@ -25,29 +25,32 @@
}
],
"require": {
"php": "^7.1 || ^8.0",
"react/event-loop": "^1.0 || ^0.5 || ^0.4",
"react/promise": "~2.2"
"php": "^8.1",
"react/event-loop": "^1.5",
"react/promise": "^3.1",
"react/stream": "^1.3",
"react/async": "^4.2",
"react/socket": "^1.15",
"react/promise-timer": "^1.10",
"evenement/evenement": "^3",
"react/promise-stream": "^1.7"
},
"require-dev": {
"ext-pcntl": "*",
"phpunit/phpunit": "^9.5 || ^7.5.20",
"symfony/process": "^6.1 || ^4.4",
"phpstan/phpstan": "^1.10"
},
"suggest": {
"ext-pcntl": "For using synchronous AMQP/RabbitMQ client"
"phpunit/phpunit": "^9.6",
"phpstan/phpstan": "^1.10",
"react/child-process": "^0.6.5",
"wyrihaximus/react-phpunit-run-tests-in-fiber": "^1"
},
"autoload": {
"psr-4": {
"Bunny\\": "src/Bunny/"
"Bunny\\": "src/"
}
},
"autoload-dev": {
"files": ["test/Library/functions.php"],
"psr-4": {
"Bunny\\": "test/Bunny/",
"Bunny\\Test\\Library\\": "test/Library/"
"Bunny\\Test\\": "test/"
}
}
}
91 changes: 0 additions & 91 deletions examples/worker-async.php

This file was deleted.

66 changes: 66 additions & 0 deletions examples/worker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use Bunny\Client;
use Bunny\Channel;
use Bunny\Message;
use Bunny\Protocol\MethodBasicConsumeOkFrame;

use React\EventLoop\Loop;
use function React\Async\async;

require dirname(__DIR__) . '/vendor/autoload.php';

$channel = null;
$consumerTag = null;

// Capture signals - SIGINT = Ctrl+C; SIGTERM = `kill`
Loop::addSignal(SIGINT, function (int $signal) use (&$channel, &$consumerTag) {
print 'Consumer cancelled\n';
$channel->cancel($consumerTag);

Loop::addTimer(3, static function () {
Loop::stop();
});
});
Loop::addSignal(SIGTERM, function (int $signal) use (&$channel, &$consumerTag) {
print 'Consumer cancelled\n';
$channel->cancel($consumerTag);

Loop::addTimer(3, static function () {
Loop::stop();
});
});

$clientConfig = [
"host" => "rabbitmq.example.com",
"port" => 5672,
"vhost" => "/",
"user" => "appuser",
"password" => "apppass",
];

$client = new Client($clientConfig);
$channel = $client->channel();
$channel->qos(0, 13);
$channel->queueDeclare('hello', false, false, false, false);
$channelRef = $channel;
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$response = $channel->consume(
async(function (Message $message, Channel $channel) {
echo ' [x] Received ', $message->content, "\n";

// Do some work - we generate password hashes with a high cost
// sleep() gets interrupted by Ctrl+C so it's not very good for demos
// Performing multiple work units demonstrates that nothing is skipped
for ($i = 0; $i < 3; $i++) {
print 'WU {$i}\n';
password_hash(random_bytes(255), PASSWORD_BCRYPT, ['cost' => 15]);
}
echo ' [x] Done ', $message->content, "\n";

$channel->ack($message);
}),
'hello',
noAck: true,
);
$consumerTag = $response->consumerTag;
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</coverage>
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">test/Bunny</directory>
<directory suffix="Test.php">test/</directory>
</testsuite>
</testsuites>
</phpunit>
Loading

0 comments on commit 520d24f

Please sign in to comment.