bschmitt / laravel-amqp

AMQP wrapper for Laravel and Lumen to publish and consume messages
MIT License
270 stars 86 forks source link

[Question]open and close connections or channels every time when publish message #92

Closed jackzhiii closed 4 years ago

jackzhiii commented 4 years ago

when I read a article(url: https://www.cloudamqp.com/blog/2018-01-19-part4-rabbitmq-13-common-errors.html), I get a advice of that "Don’t open and close connections or channels repeatedly"

But I read this library source code

    public function publish($routing, $message, array $properties = [])
    {
        $properties['routing'] = $routing;

        /* @var Publisher $publisher */
        $publisher = app()->make('Bschmitt\Amqp\Publisher');
        $publisher
            ->mergeProperties($properties)
            ->setup();

        if (is_string($message)) {
            $message = new Message($message, ['content_type' => 'text/plain', 'delivery_mode' => 2]);
        }

        $publisher->publish($routing, $message);
        Request::shutdown($publisher->getChannel(), $publisher->getConnection());
    }

I see than close connection and channel every time when call publish method. relevant code

 public static function shutdown(AMQPChannel $channel, AMQPStreamConnection $connection)
    {
        $channel->close();
        $connection->close();
    }

So, it is a bad practice for this way of publish message implements?

stevenklar commented 4 years ago

As far as I'm concerned you should always closing connections because php run request wise. Would even be worse to let the connection open.

Not sure about the newest php meta/versions though.