m6w6 / ext-pq

PostgreSQL client library (libpq) binding
BSD 2-Clause "Simplified" License
39 stars 7 forks source link

Restore listeners and statements, after Connection::reset #15

Closed ParkFramework closed 8 years ago

ParkFramework commented 8 years ago

We plan to implement in the application layers, restore listeners and statements, after Connection::reset.

Or perhaps it is better if it is implemented in ext-pq?

m6w6 commented 8 years ago

I think ext-pq should do this through PGEventConnReset handling. We're providing facilities for listeners and prepared statements, so we should also handle availability of them after a connection reset (which is not always user-induced).

ParkFramework commented 8 years ago

After resetAsync or remote disconnect, event reset not fire

m6w6 commented 8 years ago

After resetAsync or remote event reset not fire

Can you elaborate? I didn't understand that sentence (especially the occurrence of "remote").

ParkFramework commented 8 years ago

Can you elaborate? I didn't understand that sentence (especially the occurrence of "remote").

Sorry I mean - remote disconnect

m6w6 commented 8 years ago

Why should the restore logic not fire when a remote disconnect happens?

ParkFramework commented 8 years ago

Why should the restore logic not fire when a remote disconnect happens?

I mean:

$connection->on(pq\Connection::EVENT_RESET, function() {
   // never executed, after resetAsync() or remote disconnect PostgreSQL server
});

Is bug?

m6w6 commented 8 years ago

Probably, let me check!

Thank you.

m6w6 commented 8 years ago

Seems to work here. Keep in mind you have to use the first method presented here to complete async connect/reset.

$c = new pq\Connection(PQ_DSN);

$c->on(pq\Connection::EVENT_RESET, function($conn) {
    var_dump($conn);
});

$c->resetAsync();

// wait until the stream becomes writable
$w = array($c->socket);
$r = $e = null;

if (stream_select($r, $w, $e, null)) {

    // loop until the connection is established
    while (true) {

        switch ($c->poll()) {

            case pq\Connection::POLLING_READING:
                // we should wait for the stream to be read-ready
                $r = array($c->socket);
                stream_select($r, $w, $e, NULL);
                break;

            case pq\Connection::POLLING_WRITING:
                // we should wait for the stream to be write-ready
                $w = array($c->socket);
                $r = $e = null;
                stream_select($r, $w, $e, null);
                break;

            case pq\Connection::POLLING_FAILED:
                printf("Connection failed: %s\n", $c->errorMessage);
                break 2;

            case pq\Connection::POLLING_OK:
                printf("Connection completed\n");
                break 2;
        }
    }
}
ParkFramework commented 8 years ago

Seems to work here. Keep in mind you have to use the first method presented here to complete async connect/reset.

Yes EVENT_RESET is working. Problem was in my code, not your bug, sorry.