Skip to content

Commit 50b95aa

Browse files
committed
Rebuild into async only with sync API
1 parent 10e34de commit 50b95aa

File tree

135 files changed

+2681
-5208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+2681
-5208
lines changed

Diff for: benchmark/consumer.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Bunny;
33

4+
use Bunny\Message;
5+
46
require_once __DIR__ . "/../vendor/autoload.php";
57

68
$c = new Client();
@@ -13,18 +15,16 @@
1315
$t = null;
1416
$count = 0;
1517

16-
$ch->run(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count) {
18+
$ch->consume(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count) {
1719
if ($t === null) {
1820
$t = microtime(true);
1921
}
2022

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

2830
}, "bench_queue", "", false, true);
29-
30-
$c->disconnect();

Diff for: benchmark/consumer_async.php

-46
This file was deleted.

Diff for: benchmark/producer.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Bunny;
33

4+
use Bunny\Client;
5+
46
require_once __DIR__ . "/../vendor/autoload.php";
57

68
$c = new Client();

Diff for: benchmark/producer_async.php

-60
This file was deleted.

Diff for: composer.json

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bunny/bunny",
33
"type": "library",
4-
"description": "Performant pure-PHP AMQP (RabbitMQ) sync/async (ReactPHP) library",
4+
"description": "Performant pure-PHP AMQP (RabbitMQ) non-blocking ReactPHP library",
55
"keywords": [
66
"rabbitmq",
77
"rabbit",
@@ -25,22 +25,28 @@
2525
}
2626
],
2727
"require": {
28-
"php": "^7.1 || ^8.0",
29-
"react/event-loop": "^1.0 || ^0.5 || ^0.4",
30-
"react/promise": "~2.2"
28+
"php": "^8.1",
29+
"react/event-loop": "^1.5",
30+
"react/promise": "^3.1",
31+
"react/stream": "^1.3",
32+
"react/async": "^4.2",
33+
"react/socket": "^1.15",
34+
"react/promise-timer": "^1.10",
35+
"evenement/evenement": "^3"
3136
},
3237
"require-dev": {
3338
"ext-pcntl": "*",
34-
"phpunit/phpunit": "^9.5 || ^7.5.20",
35-
"symfony/process": "^6.1 || ^4.4",
36-
"phpstan/phpstan": "^1.10"
39+
"phpunit/phpunit": "^9.6",
40+
"symfony/process": "^6.4",
41+
"phpstan/phpstan": "^1.10",
42+
"wyrihaximus/react-phpunit-run-tests-in-fiber": "^1"
3743
},
3844
"suggest": {
3945
"ext-pcntl": "For using synchronous AMQP/RabbitMQ client"
4046
},
4147
"autoload": {
4248
"psr-4": {
43-
"Bunny\\": "src/Bunny/"
49+
"Bunny\\": "src/"
4450
}
4551
},
4652
"autoload-dev": {

Diff for: examples/worker-async.php

-91
This file was deleted.

Diff for: examples/worker.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use Bunny\Client;
4+
use Bunny\Channel;
5+
use Bunny\Message;
6+
use Bunny\Protocol\MethodBasicConsumeOkFrame;
7+
8+
use React\EventLoop\Loop;
9+
use function React\Async\async;
10+
11+
require dirname(__DIR__) . '/vendor/autoload.php';
12+
13+
$channel = null;
14+
$consumerTag = null;
15+
16+
// Capture signals - SIGINT = Ctrl+C; SIGTERM = `kill`
17+
Loop::addSignal(SIGINT, function (int $signal) use (&$channel, &$consumerTag) {
18+
print 'Consumer cancelled\n';
19+
$channel->cancel($consumerTag);
20+
21+
Loop::addTimer(3, static function () {
22+
Loop::stop();
23+
});
24+
});
25+
Loop::addSignal(SIGTERM, function (int $signal) use (&$channel, &$consumerTag) {
26+
print 'Consumer cancelled\n';
27+
$channel->cancel($consumerTag);
28+
29+
Loop::addTimer(3, static function () {
30+
Loop::stop();
31+
});
32+
});
33+
34+
$clientConfig = [
35+
"host" => "rabbitmq.example.com",
36+
"port" => 5672,
37+
"vhost" => "/",
38+
"user" => "appuser",
39+
"password" => "apppass",
40+
];
41+
42+
$client = new Client($clientConfig);
43+
$channel = $client->channel();
44+
$channel->qos(0, 13);
45+
$channel->queueDeclare('hello', false, false, false, false);
46+
$channelRef = $channel;
47+
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
48+
$response = $channel->consume(
49+
async(function (Message $message, Channel $channel) {
50+
echo ' [x] Received ', $message->content, "\n";
51+
52+
// Do some work - we generate password hashes with a high cost
53+
// sleep() gets interrupted by Ctrl+C so it's not very good for demos
54+
// Performing multiple work units demonstrates that nothing is skipped
55+
for ($i = 0; $i < 3; $i++) {
56+
print 'WU {$i}\n';
57+
password_hash(random_bytes(255), PASSWORD_BCRYPT, ['cost' => 15]);
58+
}
59+
echo ' [x] Done ', $message->content, "\n";
60+
61+
$channel->ack($message);
62+
}),
63+
'hello',
64+
noAck: true,
65+
);
66+
$consumerTag = $response->consumerTag;

Diff for: spec/generate.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Bunny;
33

4+
use Bunny\Constants;
45
use Bunny\Protocol\ContentHeaderFrame;
56

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

0 commit comments

Comments
 (0)