Skip to content

Commit 6f7a9a3

Browse files
cppcoffeezhuizhuhaomeng
authored andcommitted
feature: add four new apis to manipulate the coctx.
ngx_http_lua_co_ctx_t *ngx_http_lua_get_cur_co_ctx(ngx_http_request_t *r); void ngx_http_lua_set_cur_co_ctx(ngx_http_request_t *r, ngx_http_lua_co_ctx_t *coctx); lua_State *ngx_http_lua_get_co_ctx_vm(ngx_http_lua_co_ctx_t *coctx); void ngx_http_lua_co_ctx_resume_helper(ngx_http_lua_co_ctx_t *coctx, int nrets);
1 parent 14be603 commit 6f7a9a3

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

src/api/ngx_http_lua_api.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#define ngx_http_lua_version 10023
2323

2424

25+
typedef struct ngx_http_lua_co_ctx_s ngx_http_lua_co_ctx_t;
26+
27+
2528
typedef struct {
2629
uint8_t type;
2730

@@ -56,6 +59,15 @@ ngx_shm_zone_t *ngx_http_lua_find_zone(u_char *name_data, size_t name_len);
5659
ngx_shm_zone_t *ngx_http_lua_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name,
5760
size_t size, void *tag);
5861

62+
ngx_http_lua_co_ctx_t *ngx_http_lua_get_cur_co_ctx(ngx_http_request_t *r);
63+
64+
void ngx_http_lua_set_cur_co_ctx(ngx_http_request_t *r,
65+
ngx_http_lua_co_ctx_t *coctx);
66+
67+
lua_State *ngx_http_lua_get_co_ctx_vm(ngx_http_lua_co_ctx_t *coctx);
68+
69+
void ngx_http_lua_co_ctx_resume_helper(ngx_http_lua_co_ctx_t *coctx, int nrets);
70+
5971

6072
#endif /* _NGX_HTTP_LUA_API_H_INCLUDED_ */
6173

src/ngx_http_lua_api.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,120 @@ ngx_http_lua_shared_memory_init(ngx_shm_zone_t *shm_zone, void *data)
213213
return NGX_OK;
214214
}
215215

216+
217+
ngx_http_lua_co_ctx_t *
218+
ngx_http_lua_get_cur_co_ctx(ngx_http_request_t *r)
219+
{
220+
ngx_http_lua_ctx_t *ctx;
221+
222+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
223+
224+
return ctx->cur_co_ctx;
225+
}
226+
227+
228+
void
229+
ngx_http_lua_set_cur_co_ctx(ngx_http_request_t *r, ngx_http_lua_co_ctx_t *coctx)
230+
{
231+
ngx_http_lua_ctx_t *ctx;
232+
233+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
234+
235+
coctx->data = r;
236+
237+
ctx->cur_co_ctx = coctx;
238+
}
239+
240+
241+
lua_State *
242+
ngx_http_lua_get_co_ctx_vm(ngx_http_lua_co_ctx_t *coctx)
243+
{
244+
return coctx->co;
245+
}
246+
247+
248+
static ngx_int_t
249+
ngx_http_lua_co_ctx_resume(ngx_http_request_t *r)
250+
{
251+
lua_State *vm;
252+
ngx_connection_t *c;
253+
ngx_int_t rc;
254+
ngx_uint_t nreqs;
255+
ngx_http_lua_ctx_t *ctx;
256+
257+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
258+
if (ctx == NULL) {
259+
return NGX_ERROR;
260+
}
261+
262+
ctx->resume_handler = ngx_http_lua_wev_handler;
263+
264+
c = r->connection;
265+
vm = ngx_http_lua_get_lua_vm(r, ctx);
266+
nreqs = c->requests;
267+
268+
rc = ngx_http_lua_run_thread(vm, r, ctx, ctx->cur_co_ctx->nrets);
269+
270+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
271+
"lua run thread returned %d", rc);
272+
273+
if (rc == NGX_AGAIN) {
274+
return ngx_http_lua_run_posted_threads(c, vm, r, ctx, nreqs);
275+
}
276+
277+
if (rc == NGX_DONE) {
278+
ngx_http_lua_finalize_request(r, NGX_DONE);
279+
return ngx_http_lua_run_posted_threads(c, vm, r, ctx, nreqs);
280+
}
281+
282+
if (ctx->entered_content_phase) {
283+
ngx_http_lua_finalize_request(r, rc);
284+
return NGX_DONE;
285+
}
286+
287+
return rc;
288+
}
289+
290+
291+
void
292+
ngx_http_lua_co_ctx_resume_helper(ngx_http_lua_co_ctx_t *coctx, int nrets)
293+
{
294+
ngx_connection_t *c;
295+
ngx_http_request_t *r;
296+
ngx_http_lua_ctx_t *ctx;
297+
ngx_http_log_ctx_t *log_ctx;
298+
299+
r = coctx->data;
300+
c = r->connection;
301+
302+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
303+
304+
if (ctx == NULL) {
305+
return;
306+
}
307+
308+
if (c->fd != (ngx_socket_t) -1) { /* not a fake connection */
309+
log_ctx = c->log->data;
310+
log_ctx->current_request = r;
311+
}
312+
313+
coctx->nrets = nrets;
314+
coctx->cleanup = NULL;
315+
316+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
317+
"lua coctx resume handler: \"%V?%V\"", &r->uri, &r->args);
318+
319+
ctx->cur_co_ctx = coctx;
320+
321+
if (ctx->entered_content_phase) {
322+
(void) ngx_http_lua_co_ctx_resume(r);
323+
324+
} else {
325+
ctx->resume_handler = ngx_http_lua_co_ctx_resume;
326+
ngx_http_core_run_phases(r);
327+
}
328+
329+
ngx_http_run_posted_requests(c);
330+
}
331+
216332
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/ngx_http_lua_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ struct ngx_http_lua_co_ctx_s {
497497
unsigned nresults_from_worker_thread; /* number of results
498498
* from worker
499499
* thread callback */
500+
unsigned nrets; /* ngx_http_lua_run_thread nrets arg. */
500501

501502
unsigned nsubreqs; /* number of subrequests of the
502503
* current request */

0 commit comments

Comments
 (0)