Skip to content

Conversation

@nathan-muir
Copy link

Hi,

This PR adds a php function pg_wait_notify similar to https://bugs.php.net/bug.php?id=44348, except that it uses select instead of poll [differences seem moot for this use-case?]

It's also based upon postgresql example code (http://www.postgresql.org/docs/9.3/static/libpq-example.html #2).

Issue:
If you're waiting for async notifications. You must continually call pg_get_notify, with interleaved calls to usleep; which is inefficient.
Example of current code (wait until any notifications arrive, and dump them. Only wait up to 30 seconds):

<?php
$conn = pg_connect("dbname=test");
pg_query($conn, "LISTEN test_channel");
// in another thread/connection, query "NOTIFY test_channel" 
// waits up to 30seconds for a notification before terminating
$waited = 0;
$anyResults = false;
while($waited < 300){ // 30 seconds
    while(($x = pg_get_notify($conn)!= false)){
        $anyResults = true;
        var_dump($x);
    }
    if ($anyResults) break;
    usleep(100000);
    $waited++;
}

Solution:
Use something like poll or select with optional timeouts to save on resources.

Example usage (wait until any notifications arrive, and dump them. Only wait up to 30 seconds)::

<?php
$conn = pg_connect("dbname=test");
pg_query($conn, "LISTEN test_channel");
// in another thread/connection, query "NOTIFY test_channel" 
// waits up to 30seconds for a notification before terminating
if(pg_wait_notify($conn, 30)){
   while(($x = pg_get_notify($conn)) !== false){
     var_dump($x);
   }
}

I'm making this requests against PHP-5.4, as it's backwards compatible - and if successful, I'll merge/port it forwards to 5.5, 5.6/devel

…ncoming notifications on the connection. They can then be retrieved with `pg_get_notify`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants