xref: /PHP-8.2/ext/pgsql/tests/nonblocking.inc (revision 1ad08256)
1<?php
2
3function nb_is_readable($stream, $timeout = 1) {
4    $r = [$stream]; $w = []; $e = [];
5    return (bool) stream_select($r, $w, $e, $timeout, 0);
6};
7function nb_is_writable($stream, $timeout = 1) {
8    $r = []; $w = [$stream]; $e = [];
9    return (bool) stream_select($r, $w, $e, $timeout, 0);
10};
11function nb_flush($db, $db_socket) {
12    while (TRUE) {
13        if (! nb_is_writable($db_socket)) {
14            continue;
15        }
16        $flush = pg_flush($db);
17        if ($flush === TRUE) {
18            break; // All data flushed
19        } elseif ($flush === FALSE) {
20            echo "pg_flush() error\n";
21            break;
22        }
23    }
24};
25function nb_consume($db, $db_socket) {
26    while (TRUE) {
27        if (!nb_is_readable($db_socket)) {
28            continue;
29        } elseif (!pg_consume_input($db)) {
30            echo "pg_consume_input() error\n";
31            break;
32        } elseif (!pg_connection_busy($db)) {
33            break; // All data consumed
34        }
35
36    }
37};
38