Skip to content

Commit 58c94a9

Browse files
committed
add mysql::prepare defer support
1 parent a3891c3 commit 58c94a9

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

swoole_mysql.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ typedef struct _mysql_client
275275
{
276276
#ifdef SW_COROUTINE
277277
zend_bool defer;
278-
zend_bool defer_yield;
278+
zend_bool suspending;
279279
mysql_io_status iowait;
280280
zval *result;
281281
int cid;

swoole_mysql_coro.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ static PHP_METHOD(swoole_mysql_coro, recv)
883883
RETURN_FALSE;
884884
}
885885

886-
client->defer_yield = 1;
886+
client->suspending = 1;
887887
client->cid = get_current_cid();
888888
php_context *context = swoole_get_property(getThis(), 0);
889889
coro_save(context);
@@ -937,19 +937,24 @@ static PHP_METHOD(swoole_mysql_coro, prepare)
937937
}
938938
RETURN_FALSE;
939939
}
940-
else
940+
941+
if (client->defer)
941942
{
942-
client->state = SW_MYSQL_STATE_READ_START;
943-
php_context *context = swoole_get_property(getThis(), 0);
944-
double timeout = client->connector.timeout;
945-
if (timeout > 0)
946-
{
947-
client->timer = SwooleG.timer.add(&SwooleG.timer, (int) (timeout * 1000), 0, context, swoole_mysql_coro_onTimeout);
948-
}
949-
client->cid = get_current_cid();
950-
coro_save(context);
951-
coro_yield();
943+
client->iowait = SW_MYSQL_CORO_STATUS_WAIT;
944+
RETURN_TRUE;
945+
}
946+
947+
client->state = SW_MYSQL_STATE_READ_START;
948+
php_context *context = swoole_get_property(getThis(), 0);
949+
double timeout = client->connector.timeout;
950+
if (timeout > 0)
951+
{
952+
client->timer = SwooleG.timer.add(&SwooleG.timer, (int) (timeout * 1000), 0, context, swoole_mysql_coro_onTimeout);
952953
}
954+
client->suspending = 1;
955+
client->cid = get_current_cid();
956+
coro_save(context);
957+
coro_yield();
953958
}
954959

955960
static PHP_METHOD(swoole_mysql_coro_statement, execute)
@@ -994,7 +999,7 @@ static PHP_METHOD(swoole_mysql_coro_statement, execute)
994999
client->iowait = SW_MYSQL_CORO_STATUS_WAIT;
9951000
RETURN_TRUE;
9961001
}
997-
client->defer_yield = 1;
1002+
client->suspending = 1;
9981003
client->cid = get_current_cid();
9991004
coro_save(context);
10001005
coro_yield();
@@ -1128,12 +1133,12 @@ static int swoole_mysql_coro_onError(swReactor *reactor, swEvent *event)
11281133
zend_update_property_string(swoole_mysql_coro_class_entry_ptr, zobject, ZEND_STRL("connect_error"), "EPOLLERR/EPOLLHUP/EPOLLRDHUP happen!" TSRMLS_CC);
11291134
zend_update_property_long(swoole_mysql_coro_class_entry_ptr, zobject, ZEND_STRL("connect_errno"), 104 TSRMLS_CC);
11301135
ZVAL_BOOL(result, 0);
1131-
if (client->defer && !client->defer_yield)
1136+
if (client->defer && !client->suspending)
11321137
{
11331138
client->result = result;
11341139
return SW_OK;
11351140
}
1136-
client->defer_yield = 0;
1141+
client->suspending = 0;
11371142
client->cid = 0;
11381143
php_context *sw_current_context = swoole_get_property(zobject, 0);
11391144
int ret = coro_resume(sw_current_context, result, &retval);
@@ -1229,12 +1234,12 @@ static void swoole_mysql_coro_onTimeout(swTimer *timer, swTimer_node *tnode)
12291234
client->state = SW_MYSQL_STATE_QUERY;
12301235
swoole_mysql_coro_close(zobject);
12311236

1232-
if (client->defer && !client->defer_yield)
1237+
if (client->defer && !client->suspending)
12331238
{
12341239
client->result = result;
12351240
return;
12361241
}
1237-
client->defer_yield = 0;
1242+
client->suspending = 0;
12381243
client->cid = 0;
12391244

12401245
int ret = coro_resume(ctx, result, &retval);
@@ -1423,13 +1428,13 @@ static int swoole_mysql_coro_onRead(swReactor *reactor, swEvent *event)
14231428

14241429
SW_ALLOC_INIT_ZVAL(result);
14251430
ZVAL_BOOL(result, 0);
1426-
if (client->defer && !client->defer_yield)
1431+
if (client->defer && !client->suspending)
14271432
{
14281433
client->iowait = SW_MYSQL_CORO_STATUS_DONE;
14291434
client->result = result;
14301435
return SW_OK;
14311436
}
1432-
client->defer_yield = 0;
1437+
client->suspending = 0;
14331438
if (!client->cid)
14341439
{
14351440
return SW_OK;
@@ -1529,13 +1534,13 @@ static int swoole_mysql_coro_onRead(swReactor *reactor, swEvent *event)
15291534

15301535
swString_clear(client->buffer);
15311536
bzero(&client->response, sizeof(client->response));
1532-
if (client->defer && !client->defer_yield)
1537+
if (client->defer && !client->suspending)
15331538
{
15341539
client->iowait = SW_MYSQL_CORO_STATUS_DONE;
15351540
client->result = result;
15361541
return SW_OK;
15371542
}
1538-
client->defer_yield = 0;
1543+
client->suspending = 0;
15391544
client->iowait = SW_MYSQL_CORO_STATUS_READY;
15401545
client->cid = 0;
15411546

0 commit comments

Comments
 (0)