comparison src/http/ngx_http_copy_filter_module.c @ 6022:1fdba317ee6d

Added support for offloading read() in thread pools.
author Valentin Bartenev <vbart@nginx.com>
date Sat, 14 Mar 2015 17:37:25 +0300
parents 2dac6ae6d703
children 0256738454dc
comparison
equal deleted inserted replaced
6021:117c77b22db1 6022:1fdba317ee6d
21 static void ngx_http_copy_aio_event_handler(ngx_event_t *ev); 21 static void ngx_http_copy_aio_event_handler(ngx_event_t *ev);
22 #if (NGX_HAVE_AIO_SENDFILE) 22 #if (NGX_HAVE_AIO_SENDFILE)
23 static ssize_t ngx_http_copy_aio_sendfile_preload(ngx_buf_t *file); 23 static ssize_t ngx_http_copy_aio_sendfile_preload(ngx_buf_t *file);
24 static void ngx_http_copy_aio_sendfile_event_handler(ngx_event_t *ev); 24 static void ngx_http_copy_aio_sendfile_event_handler(ngx_event_t *ev);
25 #endif 25 #endif
26 #endif
27 #if (NGX_THREADS)
28 static ngx_int_t ngx_http_copy_thread_handler(ngx_thread_task_t *task,
29 ngx_file_t *file);
30 static void ngx_http_copy_thread_event_handler(ngx_event_t *ev);
26 #endif 31 #endif
27 32
28 static void *ngx_http_copy_filter_create_conf(ngx_conf_t *cf); 33 static void *ngx_http_copy_filter_create_conf(ngx_conf_t *cf);
29 static char *ngx_http_copy_filter_merge_conf(ngx_conf_t *cf, 34 static char *ngx_http_copy_filter_merge_conf(ngx_conf_t *cf,
30 void *parent, void *child); 35 void *parent, void *child);
119 ctx->output_filter = (ngx_output_chain_filter_pt) 124 ctx->output_filter = (ngx_output_chain_filter_pt)
120 ngx_http_next_body_filter; 125 ngx_http_next_body_filter;
121 ctx->filter_ctx = r; 126 ctx->filter_ctx = r;
122 127
123 #if (NGX_HAVE_FILE_AIO) 128 #if (NGX_HAVE_FILE_AIO)
124 if (ngx_file_aio && clcf->aio) { 129 if (ngx_file_aio && clcf->aio == NGX_HTTP_AIO_ON) {
125 ctx->aio_handler = ngx_http_copy_aio_handler; 130 ctx->aio_handler = ngx_http_copy_aio_handler;
126 #if (NGX_HAVE_AIO_SENDFILE) 131 #if (NGX_HAVE_AIO_SENDFILE)
127 ctx->aio_preload = ngx_http_copy_aio_sendfile_preload; 132 ctx->aio_preload = ngx_http_copy_aio_sendfile_preload;
128 #endif 133 #endif
129 } 134 }
130 #endif 135 #endif
131 136
137 #if (NGX_THREADS)
138 if (clcf->aio == NGX_HTTP_AIO_THREADS) {
139 ctx->thread_handler = ngx_http_copy_thread_handler;
140 }
141 #endif
142
132 if (in && in->buf && ngx_buf_size(in->buf)) { 143 if (in && in->buf && ngx_buf_size(in->buf)) {
133 r->request_output = 1; 144 r->request_output = 1;
134 } 145 }
135 } 146 }
136 147
137 #if (NGX_HAVE_FILE_AIO) 148 #if (NGX_HAVE_FILE_AIO || NGX_THREADS)
138 ctx->aio = r->aio; 149 ctx->aio = r->aio;
139 #endif 150 #endif
140 151
141 rc = ngx_output_chain(ctx, in); 152 rc = ngx_output_chain(ctx, in);
142 153
231 242
232 #endif 243 #endif
233 #endif 244 #endif
234 245
235 246
247 #if (NGX_THREADS)
248
249 static ngx_int_t
250 ngx_http_copy_thread_handler(ngx_thread_task_t *task, ngx_file_t *file)
251 {
252 ngx_str_t name;
253 ngx_thread_pool_t *tp;
254 ngx_http_request_t *r;
255 ngx_http_core_loc_conf_t *clcf;
256
257 r = file->thread_ctx;
258
259 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
260 tp = clcf->thread_pool;
261
262 if (tp == NULL) {
263 if (ngx_http_complex_value(r, clcf->thread_pool_value, &name)
264 != NGX_OK)
265 {
266 return NGX_ERROR;
267 }
268
269 tp = ngx_thread_pool_get((ngx_cycle_t *) ngx_cycle, &name);
270
271 if (tp == NULL) {
272 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
273 "thread pool \"%V\" not found", &name);
274 return NGX_ERROR;
275 }
276 }
277
278 task->event.data = r;
279 task->event.handler = ngx_http_copy_thread_event_handler;
280
281 if (ngx_thread_task_post(tp, task) != NGX_OK) {
282 return NGX_ERROR;
283 }
284
285 r->main->blocked++;
286 r->aio = 1;
287
288 return NGX_OK;
289 }
290
291
292 static void
293 ngx_http_copy_thread_event_handler(ngx_event_t *ev)
294 {
295 ngx_http_request_t *r;
296
297 r = ev->data;
298
299 r->main->blocked--;
300 r->aio = 0;
301
302 r->connection->write->handler(r->connection->write);
303 }
304
305 #endif
306
307
236 static void * 308 static void *
237 ngx_http_copy_filter_create_conf(ngx_conf_t *cf) 309 ngx_http_copy_filter_create_conf(ngx_conf_t *cf)
238 { 310 {
239 ngx_http_copy_filter_conf_t *conf; 311 ngx_http_copy_filter_conf_t *conf;
240 312