comparison src/event/ngx_event_pipe.c @ 154:eac26585476e

nginx-0.0.1-2003-10-22-11:05:29 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 22 Oct 2003 07:05:29 +0000
parents c71aeb75c071
children 46eb23d9471d
comparison
equal deleted inserted replaced
153:c71aeb75c071 154:eac26585476e
25 return NGX_ABORT; 25 return NGX_ABORT;
26 } 26 }
27 } 27 }
28 28
29 p->read = 0; 29 p->read = 0;
30 p->upstream_blocked = 0;
30 31
31 if (ngx_event_pipe_read_upstream(p) == NGX_ABORT) { 32 if (ngx_event_pipe_read_upstream(p) == NGX_ABORT) {
32 return NGX_ABORT; 33 return NGX_ABORT;
33 } 34 }
34 35
35 if (!p->read) { 36 if (!p->read && !p->upstream_blocked) {
36 break; 37 break;
37 } 38 }
38 39
39 do_write = 1; 40 do_write = 1;
40 } 41 }
137 138
138 /* 139 /*
139 * if the hunks are not needed to be saved in a cache and 140 * if the hunks are not needed to be saved in a cache and
140 * a downstream is ready then write the hunks to a downstream 141 * a downstream is ready then write the hunks to a downstream
141 */ 142 */
143
144 p->upstream_blocked = 1;
142 145
143 ngx_log_debug(p->log, "downstream ready"); 146 ngx_log_debug(p->log, "downstream ready");
144 147
145 break; 148 break;
146 149
182 ngx_log_debug(p->log, "no hunks to read in"); 185 ngx_log_debug(p->log, "no hunks to read in");
183 186
184 break; 187 break;
185 } 188 }
186 189
190 ngx_log_debug(p->log, "HUNK_FREE: %d" _ chain->hunk->num);
191
187 n = ngx_recv_chain(p->upstream, chain); 192 n = ngx_recv_chain(p->upstream, chain);
188 193
189 ngx_log_debug(p->log, "recv_chain: %d" _ n); 194 ngx_log_debug(p->log, "recv_chain: %d" _ n);
190 195
191 p->free_raw_hunks = chain; 196 p->free_raw_hunks = chain;
216 size = ce->hunk->end - ce->hunk->last; 221 size = ce->hunk->end - ce->hunk->last;
217 222
218 if (n >= size) { 223 if (n >= size) {
219 ce->hunk->last = ce->hunk->end; 224 ce->hunk->last = ce->hunk->end;
220 225
226 /* STUB */ ce->hunk->num = p->num++;
227
221 if (p->input_filter(p, ce->hunk) == NGX_ERROR) { 228 if (p->input_filter(p, ce->hunk) == NGX_ERROR) {
222 return NGX_ABORT; 229 return NGX_ABORT;
223 } 230 }
224 231
225 n -= size; 232 n -= size;
233 240
234 p->free_raw_hunks = ce; 241 p->free_raw_hunks = ce;
235 } 242 }
236 243
237 if ((p->upstream_eof || p->upstream_error) && p->free_raw_hunks) { 244 if ((p->upstream_eof || p->upstream_error) && p->free_raw_hunks) {
245 /* STUB */ p->free_raw_hunks->hunk->num = p->num++;
238 if (p->input_filter(p, p->free_raw_hunks->hunk) == NGX_ERROR) { 246 if (p->input_filter(p, p->free_raw_hunks->hunk) == NGX_ERROR) {
239 return NGX_ABORT; 247 return NGX_ABORT;
240 } 248 }
241 249
242 /* TODO: p->free_raw_hunk->next can be free()ed */ 250 /* TODO: p->free_raw_hunk->next can be free()ed */
300 } 308 }
301 309
302 p->out = p->out->next; 310 p->out = p->out->next;
303 ngx_remove_shadow_free_raw_hunk(&p->free_raw_hunks, ce->hunk); 311 ngx_remove_shadow_free_raw_hunk(&p->free_raw_hunks, ce->hunk);
304 312
313 ngx_log_debug(p->log, "HUNK OUT: %d %x" _ ce->hunk->num _ ce->hunk->type);
314
305 } else if (!p->cachable && p->in) { 315 } else if (!p->cachable && p->in) {
306 ce = p->in; 316 ce = p->in;
307 317
308 if (!(p->upstream_eof || p->upstream_error || p->upstream_done) 318 if (!(p->upstream_eof || p->upstream_error || p->upstream_done)
309 && (busy_len + ngx_hunk_size(ce->hunk) > p->max_busy_len)) 319 && (busy_len + ngx_hunk_size(ce->hunk) > p->max_busy_len))
311 break; 321 break;
312 } 322 }
313 323
314 p->in = p->in->next; 324 p->in = p->in->next;
315 325
326 ngx_log_debug(p->log, "HUNK IN: %d" _ ce->hunk->num);
327
316 } else { 328 } else {
317 break; 329 break;
318 } 330 }
319 331
320 busy_len += ngx_hunk_size(ce->hunk); 332 busy_len += ngx_hunk_size(ce->hunk);
321 ce->next = NULL; 333 ce->next = NULL;
322 ngx_chain_add_ce(out, le, ce); 334 ngx_chain_add_ce(out, le, ce);
323 } 335 }
324 336
325 if (out == NULL) { 337 if (out == NULL) {
326 break; 338 ngx_log_debug(p->log, "no hunks to write BUSY: %d" _ busy_len);
339
340 if (!p->upstream_blocked || busy_len == 0) {
341 break;
342 }
343
344 /* if the upstream is blocked then write the busy hunks */
327 } 345 }
328 346
329 if (p->output_filter(p->output_ctx, out) == NGX_ERROR) { 347 if (p->output_filter(p->output_ctx, out) == NGX_ERROR) {
330 p->downstream_error = 1; 348 p->downstream_error = 1;
349
350 /* handle the downstream error at the begin of the cycle. */
351
331 continue; 352 continue;
332 } 353 }
333 354
334 ngx_chain_update_chains(&p->free, &p->busy, &out); 355 ngx_chain_update_chains(&p->free, &p->busy, &out, p->tag);
335
336 /* add the free shadow raw hunks to p->free_raw_hunks */
337 356
338 for (ce = p->free; ce; ce = ce->next) { 357 for (ce = p->free; ce; ce = ce->next) {
358
359 /* add the free shadow raw hunk to p->free_raw_hunks */
360
339 if (ce->hunk->type & NGX_HUNK_LAST_SHADOW) { 361 if (ce->hunk->type & NGX_HUNK_LAST_SHADOW) {
340 h = ce->hunk->shadow; 362 h = ce->hunk->shadow;
341 /* THINK NEEDED ??? */ h->pos = h->last = h->start; 363 h->pos = h->last = h->start;
342 h->shadow = NULL; 364 h->shadow = NULL;
343 ngx_alloc_ce_and_set_hunk(te, h, p->pool, NGX_ABORT); 365 ngx_alloc_ce_and_set_hunk(te, h, p->pool, NGX_ABORT);
344 ngx_add_after_partially_filled_hunk(&p->free_raw_hunks, te); 366 ngx_add_after_partially_filled_hunk(&p->free_raw_hunks, te);
345 367
346 ce->hunk->type &= ~NGX_HUNK_LAST_SHADOW; 368 ce->hunk->type &= ~NGX_HUNK_LAST_SHADOW;
347 } 369 }
348 ce->hunk->shadow = NULL; 370 ce->hunk->shadow = NULL;
371
372 if (p->cyclic_temp_file && (ce->hunk->type & NGX_HUNK_TEMP_FILE)) {
373
374 /* reset p->temp_offset if all hunks had been sent */
375
376 if (ce->hunk->file_last == p->temp_offset) {
377 p->temp_offset = 0;
378 }
379 }
349 } 380 }
350 } 381 }
351 382
352 return NGX_OK; 383 return NGX_OK;
353 } 384 }
354 385
355 386
356 static int ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p) 387 static int ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p)
357 { 388 {
358 int rc, size, hunk_size; 389 int rc, size, hsize;
359 ngx_hunk_t *h; 390 ngx_hunk_t *h;
360 ngx_chain_t *ce, *te, *next, *out, **le, **last_free; 391 ngx_chain_t *ce, *te, *next, *out, **le, **last_free;
361 392
362 ngx_log_debug(p->log, "write to file"); 393 ngx_log_debug(p->log, "write to file");
363 394
387 le = NULL; 418 le = NULL;
388 419
389 ngx_log_debug(p->log, "offset: %d" _ p->temp_offset); 420 ngx_log_debug(p->log, "offset: %d" _ p->temp_offset);
390 421
391 do { 422 do {
392 hunk_size = ce->hunk->last - ce->hunk->pos; 423 hsize = ce->hunk->last - ce->hunk->pos;
393 424
394 ngx_log_debug(p->log, "hunk size: %d" _ hunk_size); 425 ngx_log_debug(p->log, "hunk size: %d" _ hsize);
395 426
396 if ((size + hunk_size > p->temp_file_write_size) 427 if ((size + hsize > p->temp_file_write_size)
397 || (p->temp_offset + hunk_size > p->max_temp_file_size)) 428 || (p->temp_offset + size + hsize > p->max_temp_file_size))
398 { 429 {
399 break; 430 break;
400 } 431 }
401 432
402 size += hunk_size; 433 size += hsize;
403 le = &ce->next; 434 le = &ce->next;
404 ce = ce->next; 435 ce = ce->next;
405 436
406 } while (ce); 437 } while (ce);
407 438
436 for (ce = out; ce; ce = next) { 467 for (ce = out; ce; ce = next) {
437 next = ce->next; 468 next = ce->next;
438 ce->next = NULL; 469 ce->next = NULL;
439 470
440 h = ce->hunk; 471 h = ce->hunk;
441 h->type |= NGX_HUNK_FILE;
442 h->file = p->temp_file; 472 h->file = p->temp_file;
443 h->file_pos = p->temp_offset; 473 h->file_pos = p->temp_offset;
444 p->temp_offset += h->last - h->pos; 474 p->temp_offset += h->last - h->pos;
445 h->file_last = p->temp_offset; 475 h->file_last = p->temp_offset;
476
477 if (p->cachable) {
478 h->type |= NGX_HUNK_FILE;
479 } else {
480 h->type |= NGX_HUNK_FILE|NGX_HUNK_TEMP_FILE;
481 }
446 482
447 ngx_chain_add_ce(p->out, p->last_out, ce); 483 ngx_chain_add_ce(p->out, p->last_out, ce);
448 484
449 if (h->type & NGX_HUNK_LAST_SHADOW) { 485 if (h->type & NGX_HUNK_LAST_SHADOW) {
450 h->shadow->last = h->shadow->pos = h->shadow->start; 486 h->shadow->last = h->shadow->pos = h->shadow->start;
477 ngx_test_null(h, ngx_alloc_hunk(p->pool), NGX_ERROR); 513 ngx_test_null(h, ngx_alloc_hunk(p->pool), NGX_ERROR);
478 } 514 }
479 515
480 ngx_memcpy(h, hunk, sizeof(ngx_hunk_t)); 516 ngx_memcpy(h, hunk, sizeof(ngx_hunk_t));
481 h->shadow = hunk; 517 h->shadow = hunk;
518 h->tag = p->tag;
482 h->type |= NGX_HUNK_LAST_SHADOW|NGX_HUNK_RECYCLED; 519 h->type |= NGX_HUNK_LAST_SHADOW|NGX_HUNK_RECYCLED;
483 hunk->shadow = h; 520 hunk->shadow = h;
484 521
485 ngx_alloc_ce_and_set_hunk(ce, h, p->pool, NGX_ERROR); 522 ngx_alloc_ce_and_set_hunk(ce, h, p->pool, NGX_ERROR);
523 ngx_log_debug(p->log, "HUNK %d" _ h->num);
486 ngx_chain_add_ce(p->in, p->last_in, ce); 524 ngx_chain_add_ce(p->in, p->last_in, ce);
487 525
488 return NGX_OK; 526 return NGX_OK;
489 } 527 }
490 528