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 25, 2024
1 parent 464507a commit a7fcf62
Show file tree
Hide file tree
Showing 143 changed files with 3,621 additions and 6,283 deletions.
203 changes: 203 additions & 0 deletions 0.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
Breaking changes:
* Raised minimum PHP version to 8.1+ to be able to use fibers
* Dropped Event Loop injection and replaced it with the global loop accessor
```diff
<?php

-use Bunny\Async\Client;
+use Bunny\Client;
-use React\EventLoop\Factory;

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

-$loop = Factory::create();

-(new Client($loop))->connect();
+$client = new Client();

-$loop->run();
```

* Merged `Async` and `Sync` clients into `Client` utilizing fibers

### Sync

`receive.php`:
```diff
<?php

use Bunny\Channel;
use Bunny\Client;
use Bunny\Message;

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

-$client = (new Client())->connect();
+$client = new Client();
$channel = $client->channel();

$channel->queueDeclare('hello', false, false, false, false);

echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

-$channel->run(
+$channel->consume(
function (Message $message, Channel $channel) {
echo " [x] Received ", $message->content, "\n";
},
'hello',
'',
false,
true,
);
```

`send.php`:
```diff
<?php

use Bunny\Client;

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

-$client = (new Client())->connect();
+$client = new Client();
$channel = $client->channel();
$channel->queueDeclare('hello', false, false, false, false);
$channel->close();

$channel->publish('Hello World!', [], '', 'hello');
echo " [x] Sent 'Hello World!'\n";

$channel->close();
$client->disconnect();
```

### Async

`receive-async.php`:
```diff
<?php

use Bunny\Channel;
-use Bunny\Async\Client;
+use Bunny\Client;
use Bunny\Message;
-use React\EventLoop\Factory;

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

-$loop = Factory::create();

-(new Client($loop))->connect()
+$client = new Client();
-->then(function (Client $client) {
- return $client->channel();
-})
+$channel = $client->channel();

-->then(function (Channel $channel) {
- return $channel->queueDeclare('hello', false, false, false, false)->then(function () use ($channel) {
- return $channel;
- });
-})
+$channel->queueDeclare('hello', false, false, false, false);

-->then(function (Channel $channel) {
- echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
- $channel->consume(
- function (Message $message, Channel $channel, Client $client) {
- echo " [x] Received ", $message->content, "\n";
- },
- 'hello',
- '',
- false,
- true
- );
-});
+echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

+$channel->consume(
+ function (Message $message, Channel $channel) {
+ echo " [x] Received ", $message->content, "\n";
+ },
+ 'hello',
+ '',
+ false,
+ true,
+);

-$loop->run();
```

`send-async.php`:
```diff
<?php

-use Bunny\Channel;
-use Bunny\Async\Client;
+use Bunny\Client;
-use React\EventLoop\Factory;

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

-$loop = Factory::create();

-(new Client($loop))->connect()
+$client = new Client();

-->then(function (Client $client) {
- return $client->channel();
-})
+$channel = $client->channel();

-->then(function (Channel $channel) {
- return $channel->queueDeclare('hello', false, false, false, false)->then(function () use ($channel) {
- return $channel;
- });
-})
+$channel->queueDeclare('hello', false, false, false, false);

-->then(function (Channel $channel) {
- echo " [x] Sending 'Hello World!'\n";
- return $channel->publish('Hello World!', [], '', 'hello')->then(function () use ($channel) {
- return $channel;
- });
-})
+$channel->publish('Hello World!', [], '', 'hello');

-->then(function (Channel $channel) {
- echo " [x] Sent 'Hello World!'\n";
- $client = $channel->getClient();
- return $channel->close()->then(function () use ($client) {
- return $client;
- });
-})
+echo " [x] Sent 'Hello World!'\n";
+$channel->close();

-->then(function (Client $client) {
- $client->disconnect();
-});
+$client->disconnect();
```

* Channel::queueBind arguments `string $queue` and `string $exchange` switched argument locations
```diff
<?php

use Bunny\Channel;
use Bunny\Client;
use Bunny\Message;

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


$client = new Client();
$channel = $client->channel();

$channel->exchangeDeclare('logs', 'fanout');
$queue = $channel->queueDeclare('', false, false, true, false);
-$channel->queueBind($queue->queue, 'logs');
+$channel->queueBind('logs', $queue->queue);
```
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
10 changes: 5 additions & 5 deletions benchmark/consumer.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<?php
namespace Bunny;

use Bunny\Message;

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

$c = new Client();
$ch = $c->connect()->channel();

$ch->queueDeclare("bench_queue");
$ch->exchangeDeclare("bench_exchange");
$ch->queueBind("bench_queue", "bench_exchange");
$ch->queueBind("bench_exchange", "bench_queue");

$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.

4 changes: 3 additions & 1 deletion 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 All @@ -9,7 +11,7 @@

$ch->queueDeclare("bench_queue");
$ch->exchangeDeclare("bench_exchange");
$ch->queueBind("bench_queue", "bench_exchange");
$ch->queueBind("bench_exchange", "bench_queue");

$body = <<<EOT
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
Expand Down
60 changes: 0 additions & 60 deletions benchmark/producer_async.php

This file was deleted.

Loading

0 comments on commit a7fcf62

Please sign in to comment.