Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit 8aa7157

Browse files
committed
Optimize code [3]
1 parent 40dadc9 commit 8aa7157

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

swoole_postgresql_coro.cc

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ static PHP_METHOD(swoole_postgresql_coro, fetchRow);
137137
static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_type, int into_object);
138138

139139
static void _free_result(zend_resource *rsrc);
140-
static int swoole_pgsql_coro_onRead(swReactor *reactor, swEvent *event);
141-
static int swoole_pgsql_coro_onWrite(swReactor *reactor, swEvent *event);
142-
static int swoole_pgsql_coro_onError(swReactor *reactor, swEvent *event);
140+
static int swoole_pgsql_coro_onRead(Reactor *reactor, Event *event);
141+
static int swoole_pgsql_coro_onWrite(Reactor *reactor, Event *event);
142+
static int swoole_pgsql_coro_onError(Reactor *reactor, Event *event);
143143
static int swoole_postgresql_coro_close(zval *zobject);
144144
static int query_result_parse(pg_object *object);
145145
static int prepare_result_parse(pg_object *object);
146146
static int meta_data_result_parse(pg_object *object);
147-
static void swoole_pgsql_coro_onTimeout(swTimer *timer, swoole::TimerNode *tnode);
147+
static void swoole_pgsql_coro_onTimeout(Timer *timer, TimerNode *tnode);
148148
static void _php_pgsql_free_params(char **params, int num_params);
149149

150150
// clang-format off
@@ -472,17 +472,22 @@ static void connect_callback(pg_object *object, Reactor *reactor, Event *event)
472472
PHPCoroutine::resume_m(context, &return_value);
473473
}
474474

475-
static int swoole_pgsql_coro_onWrite(swReactor *reactor, swEvent *event) {
475+
static int swoole_pgsql_coro_onWrite(Reactor *reactor, Event *event) {
476476
pg_object *object = (pg_object *) event->socket->object;
477477
if (object->connected) {
478-
return sw_reactor()->default_write_handler(sw_reactor(), event);
478+
if (object->co) {
479+
object->co->resume();
480+
return SW_OK;
481+
} else {
482+
return reactor->default_write_handler(reactor, event);
483+
}
479484
} else {
480485
connect_callback(object, reactor, event);
481486
}
482487
return SW_OK;
483488
}
484489

485-
static int swoole_pgsql_coro_onRead(swReactor *reactor, swEvent *event) {
490+
static int swoole_pgsql_coro_onRead(Reactor *reactor, Event *event) {
486491
pg_object *object = (pg_object *) (event->socket->object);
487492

488493
if (!object->connected) {
@@ -737,6 +742,43 @@ static int prepare_result_parse(pg_object *object) {
737742
return SW_OK;
738743
}
739744

745+
static bool swoole_pgsql_wait_write_ready(PGconn *pgsql, pg_object *object, zval *zthis) {
746+
int retval = 0;
747+
object->co = Coroutine::get_current_safe();;
748+
749+
ON_SCOPE_EXIT {
750+
if (object->socket->isset_writable_event()) {
751+
swoole_event_del(object->socket);
752+
}
753+
object->co = nullptr;
754+
};
755+
756+
while ((retval = PQflush(pgsql)) == 1) {
757+
if (!object->socket->isset_writable_event() && !swoole_event_add(object->socket, SW_EVENT_WRITE)) {
758+
return false;
759+
}
760+
object->co->yield_ex(object->timeout);
761+
if (object->co->is_canceled()) {
762+
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(zthis), ZEND_STRL("error"),
763+
swoole_strerror(SW_ERROR_CO_CANCELED));
764+
return false;
765+
}
766+
if (object->co->is_timedout()) {
767+
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(zthis), ZEND_STRL("error"),
768+
swoole_strerror(SW_ERROR_CO_TIMEDOUT));
769+
return false;
770+
}
771+
}
772+
773+
if (retval == -1) {
774+
char *err_msg = PQerrorMessage(pgsql);
775+
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(zthis), ZEND_STRL("error"), err_msg);
776+
return false;
777+
}
778+
779+
return true;
780+
}
781+
740782
static PHP_METHOD(swoole_postgresql_coro, query) {
741783
zval *query;
742784
PGconn *pgsql;
@@ -761,18 +803,7 @@ static PHP_METHOD(swoole_postgresql_coro, query) {
761803
RETURN_FALSE;
762804
}
763805

764-
int retval = 0;
765-
while ((retval = PQflush(pgsql)) == 1) {
766-
if (System::wait_event(PQsocket(pgsql), SW_EVENT_WRITE, object->timeout) <= 0) {
767-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("error"),
768-
swoole_strerror(swoole_get_last_error()));
769-
RETURN_FALSE;
770-
}
771-
}
772-
773-
if (retval == -1) {
774-
char *err_msg = PQerrorMessage(pgsql);
775-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("error"), err_msg);
806+
if (!swoole_pgsql_wait_write_ready(pgsql, object, ZEND_THIS)) {
776807
RETURN_FALSE;
777808
}
778809

@@ -828,6 +859,10 @@ static PHP_METHOD(swoole_postgresql_coro, prepare) {
828859
}
829860
}
830861

862+
if (!swoole_pgsql_wait_write_ready(pgsql, object, ZEND_THIS)) {
863+
RETURN_FALSE;
864+
}
865+
831866
if (swoole_event_add(object->socket, SW_EVENT_READ) < 0) {
832867
RETURN_FALSE;
833868
}
@@ -915,6 +950,10 @@ static PHP_METHOD(swoole_postgresql_coro, execute) {
915950
}
916951
}
917952

953+
if (!swoole_pgsql_wait_write_ready(pgsql, object, ZEND_THIS)) {
954+
RETURN_FALSE;
955+
}
956+
918957
FutureTask *context = php_swoole_postgresql_coro_get_context(ZEND_THIS);
919958
context->coro_params = *ZEND_THIS;
920959

@@ -1400,7 +1439,7 @@ static void _free_result(zend_resource *rsrc) {
14001439
PQclear(pg_result);
14011440
}
14021441

1403-
static int swoole_pgsql_coro_onError(swReactor *reactor, swEvent *event) {
1442+
static int swoole_pgsql_coro_onError(Reactor *reactor, Event *event) {
14041443
zval _result;
14051444
zval *result = &_result;
14061445
pg_object *object = (pg_object *) (event->socket->object);

swoole_postgresql_coro.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum pg_query_type
4343
struct pg_object {
4444
PGconn *conn;
4545
swoole::network::Socket *socket;
46+
swoole::Coroutine *co;
4647
PGresult *result;
4748
zval *object;
4849
zval _object;

0 commit comments

Comments
 (0)