Skip to content

Commit 983c974

Browse files
committed
Fix bug #5313
1 parent 6e9966c commit 983c974

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

ext-src/swoole_pgsql.cc

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,37 @@
2727
#include "thirdparty/php80/pdo_pgsql/php_pdo_pgsql_int.h"
2828
#endif
2929

30+
using swoole::Reactor;
3031
using swoole::coroutine::Socket;
3132

3233
static bool swoole_pgsql_blocking = true;
3334

3435
static int swoole_pgsql_socket_poll(PGconn *conn, swEventType event, double timeout = -1) {
3536
if (swoole_pgsql_blocking) {
36-
return 1;
37+
struct pollfd fds[1] = {};
38+
fds[0].fd = PQsocket(conn);
39+
fds[0].events = 0;
40+
41+
if (Reactor::isset_read_event(event)) {
42+
fds[0].events |= POLLIN;
43+
}
44+
45+
if (Reactor::isset_write_event(event)) {
46+
fds[0].events |= POLLOUT;
47+
}
48+
49+
if (Reactor::isset_error_event(event)) {
50+
fds[0].events |= POLLHUP;
51+
}
52+
53+
int result = 0;
54+
do {
55+
result = poll(fds, 1, timeout);
56+
} while (result < 0 && errno == EINTR);
57+
58+
return result > 0 ? 1 : errno == ETIMEDOUT ? 0 : -1;
3759
}
60+
3861
Socket sock(PQsocket(conn), SW_SOCK_RAW);
3962
sock.get_socket()->nonblock = 1;
4063
bool retval = sock.poll(event, timeout);
@@ -74,10 +97,6 @@ static PGresult *swoole_pgsql_get_result(PGconn *conn) {
7497
}
7598

7699
PGconn *swoole_pgsql_connectdb(const char *conninfo) {
77-
if (swoole_pgsql_blocking) {
78-
return PQconnectdb(conninfo);
79-
}
80-
81100
PGconn *conn = PQconnectStart(conninfo);
82101
if (conn == nullptr) {
83102
return nullptr;
@@ -120,10 +139,6 @@ PGconn *swoole_pgsql_connectdb(const char *conninfo) {
120139

121140
PGresult *swoole_pgsql_prepare(
122141
PGconn *conn, const char *stmt_name, const char *query, int n_params, const Oid *param_types) {
123-
if (swoole_pgsql_blocking) {
124-
return PQprepare(conn, stmt_name, query, n_params, param_types);
125-
}
126-
127142
swoole_trace_log(SW_TRACE_CO_PGSQL, "PQsendPrepare(conn=%p, stmt_name='%s')", conn, stmt_name);
128143
int ret = PQsendPrepare(conn, stmt_name, query, n_params, param_types);
129144
if (ret == 0) {
@@ -144,9 +159,6 @@ PGresult *swoole_pgsql_exec_prepared(PGconn *conn,
144159
const int *param_lengths,
145160
const int *param_formats,
146161
int result_format) {
147-
if (swoole_pgsql_blocking) {
148-
return PQexecPrepared(conn, stmt_name, n_params, param_values, param_lengths, param_formats, result_format);
149-
}
150162
swoole_trace_log(SW_TRACE_CO_PGSQL, "PQsendQueryPrepared(conn=%p, stmt_name='%s')", conn, stmt_name);
151163
int ret = PQsendQueryPrepared(conn, stmt_name, n_params, param_values, param_lengths, param_formats, result_format);
152164
if (ret == 0) {
@@ -161,10 +173,6 @@ PGresult *swoole_pgsql_exec_prepared(PGconn *conn,
161173
}
162174

163175
PGresult *swoole_pgsql_exec(PGconn *conn, const char *query) {
164-
if (swoole_pgsql_blocking) {
165-
return PQexec(conn, query);
166-
}
167-
168176
swoole_trace_log(SW_TRACE_CO_PGSQL, "PQsendQuery(conn=%p, query='%s')", conn, query);
169177
int ret = PQsendQuery(conn, query);
170178
if (ret == 0) {
@@ -186,10 +194,6 @@ PGresult *swoole_pgsql_exec_params(PGconn *conn,
186194
const int *param_lengths,
187195
const int *param_formats,
188196
int result_format) {
189-
if (swoole_pgsql_blocking) {
190-
return PQexecParams(conn, command, n_params, param_types, param_values, param_lengths, param_formats, result_format);
191-
}
192-
193197
swoole_trace_log(SW_TRACE_CO_PGSQL, "PQsendQueryParams(conn=%p, command='%s')", conn, command);
194198
int ret = PQsendQueryParams(
195199
conn, command, n_params, param_types, param_values, param_lengths, param_formats, result_format);

0 commit comments

Comments
 (0)