From f3bc21cf2b14d893c00c90ea2cc8d16e2b3ff0ba Mon Sep 17 00:00:00 2001 From: Hleran Date: Fri, 31 Jul 2015 01:03:40 +0300 Subject: [PATCH] lua_plugin: no switch in 1st yield; async_id_get() If coroutine and async, do not switch until first yield of coroutine; uwsgi.async_id_get() to get 'wsgi_req->async_id' in my app; --- plugins/lua/lua_plugin.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/lua/lua_plugin.c b/plugins/lua/lua_plugin.c index f2e389a7..d892498d 100644 --- a/plugins/lua/lua_plugin.c +++ b/plugins/lua/lua_plugin.c @@ -669,6 +669,15 @@ static int uwsgi_lua_input(lua_State *L) { return 0; } +static int uwsgi_api_async_id_get(lua_State *L) { + + struct wsgi_request *wsgi_req = current_wsgi_req(); + + lua_pushnumber(L, wsgi_req->async_id); + + return 1; +} + static const luaL_Reg uwsgi_api[] = { {"log", uwsgi_api_log}, {"connection_fd", uwsgi_api_req_fd}, @@ -698,6 +707,7 @@ static const luaL_Reg uwsgi_api[] = { {"async_sleep", uwsgi_api_async_sleep}, {"async_connect", uwsgi_api_async_connect}, + {"async_id_get", uwsgi_api_async_id_get}, {"is_connected", uwsgi_api_is_connected}, {"close", uwsgi_api_close}, {"wait_fd_read", uwsgi_api_wait_fd_read}, @@ -856,6 +866,7 @@ static int uwsgi_lua_request(struct wsgi_request *wsgi_req) { // wrong async_status status! return -1; } +async_coroutine: while (lua_pcall(L, 0, 1, 0) == 0) { if (lua_isstring(L, -1)) { http = lua_tolstring(L, -1, &slen); @@ -967,7 +978,7 @@ static int uwsgi_lua_request(struct wsgi_request *wsgi_req) { if (lua_type(L, -1) == LUA_TFUNCTION) { if (uwsgi.async > 0) { - return UWSGI_AGAIN; + goto async_coroutine; } while ( lua_pcall(L, 0, 1, 0) == 0) {