[rabbitmq-discuss] How can I set socket timeouts in PHP using the AMQP PHP extension?

Keith Krueger keith at krue.gr
Wed Jun 29 21:03:23 BST 2011


Thanks for all the replies. At this time, I am not interested in connection
pooling for my application.  I just would like to set some lower client
timeouts on the socket connections made to RabbitMQ in case of server
overload, etc.

I do like the look of Robin's amqphp library, although I'm restricted to PHP
5.2.4 for the foreseeable future and can't make use of it.

The source code for the C language rabbitmq-c library I'm using is the
following:

  http://hg.rabbitmq.com/rabbitmq-c/rev/3c549bb09c16 rabbitmq-c

In the C code, the connection is described here:
http://hg.rabbitmq.com/rabbitmq-c/file/3c549bb09c16/librabbitmq/amqp_connection.c

The source code for the PHP extension implemented on top of that library
(documented here: http://www.php.net/manual/en/book.amqp.php) is this:

   http://hg.rabbitmq.com/rabbitmq-codegen/rev/f8b34141e6cb

I don't see any property or configuration setting in the AMQPConnection
constructor.  I tried the following construction call, but I think it's
ignoring the two timeout args (as if I even know what to call them without
documentation).

        $connectionConfig = array(
            'host' => RABBITMQ_HOST,
            'port' => RABBITMQ_PORT,
            'vhost' => RABBITMQ_VHOST,
            'login' => RABBITMQ_LOGIN,
            'password' => RABBITMQ_PASSWORD,

            'connection_timeout' => 10,
            'read_write_timeout' => 3
            );

        $this->connection = new AMQPConnection($connectionConfig);

It sure would be nice if those timeout configurations would work and could
be passed into the underlying C implementation.

-Keith

On Wed, Jun 29, 2011 at 9:20 AM, Robin Harvey <harvey.robin at gmail.com>wrote:

> Cool, I'll check this out and see what I can do.
>
> --Robin
>
>
> On Wed, Jun 29, 2011 at 4:42 PM, Gavin M. Roy <gmr at myyearbook.com> wrote:
>
>> http://php.net/manual/en/function.pfsockopen.php
>>
>> The important thing of course will be wrapping this in an intelligent way
>> keeping track of your channels and connections. In your class structure
>> you'd want to know about the connection and authenticated session, channels
>> and such for subsequent requests for reuse
>>
>> Regards,
>>
>> Gavin
>>
>> On Wed, Jun 29, 2011 at 10:56 AM, Robin Harvey <harvey.robin at gmail.com>wrote:
>>
>>> OK, I understand.  AFAIK, there's no way to do that in PHP without
>>> writing an extension in C, is there?
>>>
>>>
>>>
>>> On Wed, Jun 29, 2011 at 2:47 PM, Gavin M. Roy <gmr at myyearbook.com>wrote:
>>>
>>>> Cool, I meant persistent, such as with the mysql_pconnect or
>>>> pgsql_pconnect.
>>>>
>>>>  Thanks for the work on amqphp, will look deeper at it for use in other
>>>> areas. I'm glad there is an active, maintained client!
>>>>
>>>> Gavin
>>>>
>>>>
>>>> On Wed, Jun 29, 2011 at 4:08 AM, Robin Harvey <harvey.robin at gmail.com>wrote:
>>>>
>>>>> BTW, Gavin, on this point:
>>>>>
>>>>>
>>>>> > iirc the existing implementations do not have connection pooling
>>>>>
>>>>> I've recently updated amqphp so that you can manage multiple
>>>>> connections at the same time, so for example you can connect to and consume
>>>>> from 2 or more different brokers at the same time.  There's a demo here:
>>>>>
>>>>>
>>>>> https://github.com/BraveSirRobin/amqphp/blob/master/demos/demo-multi-consumer.php
>>>>>
>>>>> Thanks,
>>>>> --Robin
>>>>>
>>>>> On Wed, Jun 29, 2011 at 4:08 AM, Gavin M. Roy <gmr at myyearbook.com>wrote:
>>>>>
>>>>>> While not directly related to your question and you've not specified
>>>>>> your use case, I have found trying to use native AMQP drivers in my PHP
>>>>>> application as having too much overhead for connection management and iirc
>>>>>> the existing implementations do not have connection pooling. This means lots
>>>>>> of startup time and overhead on every request. To get around this we created
>>>>>> a HTTP library that talks to the RabbitMQ JSON RPC Channel plugin. If all
>>>>>> you are doing is publishing, you might want to check out VorpalBunny:
>>>>>> https://github.com/myyearbook/VorpalBunny/
>>>>>> As far as the underlying implementation, it's worth noting that the
>>>>>> rabbitmq-c library is considered experimental. While we use it in our C
>>>>>> applications in production, I can't say that we use the PHP extension on top
>>>>>> of it. The PHP site has fatal errors for me right now so I can't look up to
>>>>>> see which PHP extension the code is based on, otherwise I'd poke into the
>>>>>> code and let you know if there are any timeout settings.
>>>>>> Gavin
>>>>>> On Tuesday, June 28, 2011 at 6:44 PM, Keith Krueger wrote:
>>>>>> > I am using the PHP AMQP extension, as described here:
>>>>>> >
>>>>>> > http://php.net/manual/en/book.amqp.php
>>>>>> >
>>>>>> > This PHP extension uses the rabbitmq-c library as its underlying
>>>>>> implementation.
>>>>>> >
>>>>>> > I would like to set socket timeouts for connection establishment and
>>>>>> for each of my enqueues and dequeues. I want to do this in case our server
>>>>>> performance degrades, to allow the clients to fail quickly rather than hang.
>>>>>> >
>>>>>> > Does anyone have experience doing this in a PHP environment? I have
>>>>>> tried setting the PHP socket timeout as follows.
>>>>>> >
>>>>>> >  ini_set('default_socket_timeout', 5);
>>>>>> >
>>>>>> > This appears to have no impact on the PHP extension's timeout
>>>>>> settings. I am still able to enqueue a multi-megabyte MQ message to rabbitmq
>>>>>> without losing the socket connection. It takes about 75 seconds to enqueue
>>>>>> this message, but the socket is kept open the entire time.
>>>>>> >
>>>>>> > Is there any support either in the PHP AMQP extension or in a
>>>>>> different PHP setting that I can use to reduce the timeouts for our
>>>>>> interactions with RabbitMQ? I'd rather not live with a potential problem of
>>>>>> hanging client processes if I can avoid it.
>>>>>> >
>>>>>> > Thanks,
>>>>>> >
>>>>>> > -Keith
>>>>>> >
>>>>>> > _______________________________________________
>>>>>> > rabbitmq-discuss mailing list
>>>>>> > rabbitmq-discuss at lists.rabbitmq.com (mailto:
>>>>>> rabbitmq-discuss at lists.rabbitmq.com)
>>>>>> >
>>>>>> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> rabbitmq-discuss mailing list
>>>>>> rabbitmq-discuss at lists.rabbitmq.com
>>>>>> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20110629/6a3c121b/attachment.htm>


More information about the rabbitmq-discuss mailing list