Queuing a task to thread from http request handler ( or location directive handler)
typedef struct {
ngx_http_request_t *pHttpRequest;
} ngx_thread_ctx;
static ngx_int_t ngx_http_rdm_agent_handler(ngx_http_request_t *r)
{
ngx_thread_task_t *task;
ngx_thread_ctx *thread_ctx; // Thread context is my struct
// that has pointer to nginx_request_t struct
..........
..........
thread_ctx = (ngx_thread_ctx*)(task->ctx);
thread_ctx->pHttpRequest = r;
task->handler = my_thread_callback;
task->event.handler = my_thread_completion;
task->event.data = thread_ctx;
//*** This is the key part. Increment this so nginx
//*** won't finalize request (r)
r->main->blocked++;
// loc_cf -> location config struct where I added thread pool during
// configuration phase
if (ngx_thread_task_post(loc_cf->pThreadPool, task) != NGX_OK) {
r->main->blocked--;
return NGX_ERROR;
}
return NGX_DONE;
}
Completion handler of your thread
static void my_thread_completion(ngx_event_t *ev)
{
ngx_thread_ctx *ctx = (ngx_thread_ctx*)ev->data;
ctx->pHttpRequest->main->blocked--;
ngx_http_finalize_request(ctx->pHttpRequest, NGX_DONE);
}