Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebuild into async only with sync API #147

Merged
merged 1 commit into from
May 10, 2024
Merged

Conversation

WyriHaximus
Copy link
Collaborator

@WyriHaximus WyriHaximus commented Jan 28, 2024

Non-Breaking Changes:

  • Merged clients into one
  • Marked Client and Channel final through doc block and introduced interfaces for them for unit testing
  • Dropped build in event loop, socket, and stream handling switching to react/event-loop, react/socket, and react/stream for that
  • Partially introduced strict typing and (return) type hints due to all the changes in the code
  • Updated certain generated traits into generated classes
  • Ensured performance didn't regress with these changes
  • Updated all examples, tutorials, and the benchmark. Dropping the async variants due to the merge

Breaking changes:

  • Raised minimum PHP version to 8.1+ unlocking the use of fibers
  • Swapped react/promise v2 with v3
  • Dropped Event Loop injection and replaced it with the global loop accessor
<?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 through react/async

Sync

receive.php:

<?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:

<?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:

<?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:

<?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
<?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);

@WyriHaximus WyriHaximus added this to the v0.6.0 milestone Jan 28, 2024
@WyriHaximus WyriHaximus force-pushed the rebuild-into-async-only branch 5 times, most recently from 67ab6a6 to c7e3abe Compare February 2, 2024 14:37
@WyriHaximus WyriHaximus force-pushed the rebuild-into-async-only branch 2 times, most recently from 74f5609 to 50b95aa Compare February 16, 2024 19:24
@WyriHaximus WyriHaximus force-pushed the rebuild-into-async-only branch 15 times, most recently from f468186 to 3db5748 Compare March 10, 2024 20:21
@WyriHaximus WyriHaximus force-pushed the rebuild-into-async-only branch 2 times, most recently from 13b1999 to 2a27865 Compare March 20, 2024 21:48
@WyriHaximus WyriHaximus force-pushed the rebuild-into-async-only branch 5 times, most recently from eb219ce to 520d24f Compare April 12, 2024 05:49
@WyriHaximus WyriHaximus force-pushed the rebuild-into-async-only branch 3 times, most recently from ebddb44 to 11efa3f Compare April 19, 2024 06:08
@WyriHaximus WyriHaximus force-pushed the rebuild-into-async-only branch 6 times, most recently from 73d9b8d to 249867c Compare April 26, 2024 16:13
@WyriHaximus WyriHaximus changed the title [WIP] Rebuild into async only with sync API Rebuild into async only with sync API Apr 26, 2024
@WyriHaximus WyriHaximus force-pushed the rebuild-into-async-only branch 2 times, most recently from 28c8c28 to e50f6f7 Compare May 5, 2024 21:15
@WyriHaximus WyriHaximus changed the base branch from master to 0.6.x May 10, 2024 09:34
Non-Breaking Changes:
* Merged clients into one
* Marked `Client` and `Channel` `final` through doc block and introduced interfaces for them for unit testing
* Dropped build in event loop, socket, and stream handling switching to [`react/event-loop`](https://reactphp.org/event-loop/), [`react/socket`](https://reactphp.org/socket/), and [`react/stream`](https://reactphp.org/stream/) for that
* Partially introduced strict typing and (return) type hints due to all the changes in the code
* Updated certain generated traits into generated classes
* Ensured performance didn't regress with these changes
* Updated all examples, tutorials, and the benchmark. Dropping the async variants due to the merge

Breaking changes:
* Raised minimum PHP version to 8.1+ unlocking the use of fibers
* Swapped [`react/promise`](https://reactphp.org/promise/) v2 with v3
* 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 through [`react/async`](https://reactphp.org/async/)

`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();
```

`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);
```
@WyriHaximus WyriHaximus merged commit b4d6781 into 0.6.x May 10, 2024
56 checks passed
@WyriHaximus WyriHaximus deleted the rebuild-into-async-only branch May 10, 2024 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant