forked from kaltura/nginx-parallel-module
/
ngx_http_parallel_module.c
1787 lines (1511 loc) · 43.6 KB
/
ngx_http_parallel_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
#include <nginx.h>
#include "ngx_fixed_buffer_cache.h"
// macros
#define DIV_CEIL(nom, denom) (((nom) + (denom) - 1) / (denom))
#define RANGE_FORMAT "bytes=%O-%O"
#define CONTENT_RANGE_FORMAT "bytes %O-%O/%O"
// typedefs
typedef struct {
off_t start;
off_t end;
ngx_flag_t is_range_request;
} ngx_http_range_t;
typedef struct {
ngx_str_t uri_prefix;
ngx_uint_t fiber_count;
size_t min_chunk_size;
size_t max_chunk_size;
size_t max_headers_size;
ngx_uint_t max_buffer_count;
ngx_flag_t consistency_check_etag;
ngx_flag_t consistency_check_last_modified;
ngx_shm_zone_t* content_length_cache_zone;
} ngx_http_parallel_loc_conf_t;
typedef struct ngx_http_parallel_fiber_ctx_s {
ngx_http_headers_in_t headers_in;
ngx_list_t upstream_headers;
ngx_http_post_subrequest_t *psr;
uint64_t chunk_index;
ngx_http_request_t *sr;
ngx_chain_t* cl;
struct ngx_http_parallel_fiber_ctx_s* next;
} ngx_http_parallel_fiber_ctx_t;
typedef struct {
ngx_str_t sr_uri;
ngx_http_headers_in_t original_headers_in;
ngx_http_range_t range;
ngx_uint_t fiber_count; // fiber_count == 1 means that the request is proxied as is
off_t cached_response_length;
ngx_flag_t key_inited;
u_char key[NGX_FIXED_BUFFER_CACHE_KEY_SIZE];
ngx_chain_t** chunks;
uint64_t chunk_count;
uint64_t missing_chunks; // a bitmask of chunks that returned 416
uint64_t next_send_chunk; // next chunk to send to the client
uint64_t next_request_chunk; // next chunk to request from upstream
size_t initial_chunk_size;
size_t chunk_size;
size_t last_chunk_size;
size_t initial_requested_size;
ngx_chain_t *free; // list of free buffers
ngx_chain_t *busy; // list of busy buffers (being sent to client)
ngx_uint_t allocated_count; // number of allocated buffers
ngx_int_t error_code;
ngx_http_event_handler_pt original_write_event_handler;
ngx_str_t etag;
ngx_str_t last_modified;
ngx_uint_t active_fibers;
ngx_http_parallel_fiber_ctx_t* completed_fiber;
ngx_http_parallel_fiber_ctx_t* free_fibers; // a linked list of free fibers
ngx_http_parallel_fiber_ctx_t fibers[1]; // must be last
} ngx_http_parallel_ctx_t;
// forward declarations
static ngx_int_t ngx_http_parallel_subrequest_finished_handler(
ngx_http_request_t *r, void *data, ngx_int_t rc);
static char *ngx_http_parallel_command(
ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_parallel_cache_command(
ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_parallel_filter_init(ngx_conf_t *cf);
static void *ngx_http_parallel_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_parallel_merge_loc_conf(
ngx_conf_t *cf, void *parent, void *child);
// constants
static ngx_conf_num_bounds_t ngx_http_parallel_fiber_count_bounds = {
ngx_conf_check_num_bounds, 2, 64
};
static ngx_command_t ngx_http_parallel_commands[] = {
{ ngx_string("parallel"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_http_parallel_command,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL},
{ ngx_string("parallel_fiber_count"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_parallel_loc_conf_t, fiber_count),
&ngx_http_parallel_fiber_count_bounds },
{ ngx_string("parallel_min_chunk_size"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_parallel_loc_conf_t, min_chunk_size),
NULL },
{ ngx_string("parallel_max_chunk_size"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_parallel_loc_conf_t, max_chunk_size),
NULL },
{ ngx_string("parallel_max_headers_size"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_parallel_loc_conf_t, max_headers_size),
NULL },
{ ngx_string("parallel_max_buffer_count"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_parallel_loc_conf_t, max_buffer_count),
NULL },
{ ngx_string("parallel_consistency_check_etag"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_parallel_loc_conf_t, consistency_check_etag),
NULL },
{ ngx_string("parallel_consistency_check_last_modified"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_parallel_loc_conf_t, consistency_check_last_modified),
NULL },
{ ngx_string("parallel_content_length_cache"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE12,
ngx_http_parallel_cache_command,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_parallel_loc_conf_t, content_length_cache_zone),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_parallel_module_ctx = {
NULL, /* preconfiguration */
ngx_http_parallel_filter_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_parallel_create_loc_conf, /* create location configuration */
ngx_http_parallel_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_parallel_module = {
NGX_MODULE_V1,
&ngx_http_parallel_module_ctx, /* module context */
ngx_http_parallel_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_uint_t content_range_hash =
ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash(
ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash(
'c', 'o'), 'n'), 't'), 'e'), 'n'), 't'), '-'), 'r'), 'a'), 'n'), 'g'), 'e');
static ngx_str_t content_range_name = ngx_string("content-range");
// globals
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
// Note: the code below is a slightly modified version of ngx_http_range_parse,
// it has a few limitations compared to the code found there:
// 1. only a single range is supported
// 2. suffix ranges (e.g -100) are not supported
static ngx_int_t
ngx_http_parallel_range_parse(ngx_http_request_t *r, ngx_http_range_t* result)
{
u_char *p;
off_t start, end, cutoff, cutlim;
if (r->headers_in.range->value.len < 7 ||
ngx_strncasecmp(r->headers_in.range->value.data,
(u_char *) "bytes=", 6) != 0) {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
p = r->headers_in.range->value.data + 6;
cutoff = NGX_MAX_OFF_T_VALUE / 10;
cutlim = NGX_MAX_OFF_T_VALUE % 10;
start = 0;
end = 0;
while (*p == ' ') { p++; }
if (*p < '0' || *p > '9') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p >= '0' && *p <= '9') {
if (start >= cutoff && (start > cutoff || *p - '0' > cutlim)) {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
start = start * 10 + *p++ - '0';
}
while (*p == ' ') { p++; }
if (*p++ != '-') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p == ' ') { p++; }
if (*p == '\0') {
end = 0; // 0 signifies no limit
goto found;
}
if (*p < '0' || *p > '9') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p >= '0' && *p <= '9') {
if (end >= cutoff && (end > cutoff || *p - '0' > cutlim)) {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
end = end * 10 + *p++ - '0';
}
while (*p == ' ') { p++; }
if (*p != '\0') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
end++;
found:
if (end != 0 && start >= end) {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
result->start = start;
result->end = end;
return NGX_OK;
}
static void *
ngx_http_parallel_memrchr(const u_char *s, int c, size_t n)
{
const u_char *cp;
for (cp = s + n; cp > s;)
{
if (*(--cp) == (u_char)c)
{
return (void*)cp;
}
}
return NULL;
}
static ngx_uint_t
ngx_http_parallel_list_get_count(ngx_list_t* list)
{
ngx_list_part_t *part;
ngx_uint_t result;
result = 0;
for (part = &list->part; part; part = part->next)
{
result += part->nelts;
}
return result;
}
static ngx_int_t
ngx_http_parallel_list_copy(
ngx_http_request_t* r,
ngx_list_t* dest,
ngx_list_t* src,
ngx_uint_t n,
size_t size)
{
ngx_list_part_t *part;
ngx_int_t rc;
u_char* p;
rc = ngx_list_init(dest, r->pool, n, size);
if (rc != NGX_OK)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_list_copy: ngx_list_init failed");
return NGX_ERROR;
}
p = (u_char*)dest->last->elts;
for (part = &r->headers_in.headers.part; part; part = part->next)
{
p = ngx_copy(p, part->elts, size * part->nelts);
dest->last->nelts += part->nelts;
}
return NGX_OK;
}
// the following function updates the pointers in headers_in (e.g. range)
// to point to the respective elements inside the headers_in->headers array
static void
ngx_http_parallel_update_headers(
ngx_http_request_t* r,
ngx_http_headers_in_t* headers_in)
{
ngx_http_core_main_conf_t *cmcf;
ngx_http_header_t *hh;
ngx_list_part_t *part;
ngx_table_elt_t **ph;
ngx_table_elt_t *h;
ngx_uint_t i;
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
part = &headers_in->headers.part;
h = part->elts;
for (i = 0; /* void */; i++)
{
if (i >= part->nelts)
{
if (part->next == NULL)
{
break;
}
part = part->next;
h = part->elts;
i = 0;
}
hh = ngx_hash_find(&cmcf->headers_in_hash, h[i].hash,
h[i].lowcase_key, h[i].key.len);
if (!hh)
{
continue;
}
ph = (ngx_table_elt_t **)((char *)headers_in + hh->offset);
*ph = &h[i];
}
}
static ngx_str_t*
ngx_http_parallel_header_get_value(
ngx_list_t* headers,
ngx_str_t* name,
ngx_uint_t hash)
{
ngx_list_part_t *part;
ngx_table_elt_t *h;
ngx_uint_t i;
part = &headers->part;
h = part->elts;
for (i = 0; /* void */; i++)
{
if (i >= part->nelts)
{
if (part->next == NULL)
{
break;
}
part = part->next;
h = part->elts;
i = 0;
}
if (h[i].hash == hash &&
h[i].key.len == name->len &&
ngx_memcmp(h[i].lowcase_key, name->data, name->len) == 0)
{
return &h[i].value;
}
}
return NULL;
}
static void
ngx_http_parallel_header_clear_value(
ngx_list_t* headers,
ngx_str_t* name,
ngx_uint_t hash)
{
ngx_list_part_t *part;
ngx_table_elt_t *h;
ngx_uint_t i = 0;
part = &headers->part;
h = part->elts;
for (;;)
{
if (i >= part->nelts)
{
if (part->next == NULL)
{
break;
}
part = part->next;
h = part->elts;
i = 0;
}
if (h[i].hash != hash ||
h[i].key.len != name->len ||
ngx_memcmp(h[i].lowcase_key, name->data, name->len) != 0)
{
i++;
continue;
}
ngx_memmove(h + i, h + i + 1, (part->nelts - i - 1) * sizeof(*h));
part->nelts--;
}
}
static off_t
ngx_http_parallel_get_instance_length(ngx_str_t *content_range, ngx_log_t* log)
{
u_char* last_slash;
off_t content_length;
last_slash = ngx_http_parallel_memrchr(
content_range->data, '/', content_range->len);
if (last_slash == NULL)
{
ngx_log_error(NGX_LOG_ERR, log, 0,
"ngx_http_parallel_get_instance_length: "
"no slash in content-range value \"%V\"", content_range);
return -1;
}
content_length = ngx_atoof(
last_slash + 1,
content_range->data + content_range->len - last_slash - 1);
if (content_length < 0)
{
ngx_log_error(NGX_LOG_ERR, log, 0,
"ngx_http_parallel_get_instance_length: failed to parse "
"instance length from content-range \"%V\"", content_range);
return -1;
}
return content_length;
}
static ngx_int_t
ngx_http_parallel_init_fiber(
ngx_http_request_t *r,
ngx_uint_t header_in_count,
ngx_flag_t proxy_as_is,
ngx_http_parallel_fiber_ctx_t* fiber)
{
ngx_table_elt_t *h;
ngx_int_t rc;
// create the post-subrequest
fiber->psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
if (fiber->psr == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_fiber: ngx_palloc failed");
return NGX_ERROR;
}
fiber->psr->handler = ngx_http_parallel_subrequest_finished_handler;
fiber->psr->data = fiber;
// copy the input headers
fiber->headers_in = r->headers_in;
if (proxy_as_is)
{
return NGX_OK;
}
// copy the headers list in order to update it
rc = ngx_http_parallel_list_copy(
r,
&fiber->headers_in.headers,
&r->headers_in.headers,
header_in_count + 1,
sizeof(ngx_table_elt_t));
if (rc != NGX_OK)
{
return rc;
}
ngx_http_parallel_update_headers(r, &fiber->headers_in);
// add a range header
if (fiber->headers_in.range == NULL)
{
h = ngx_list_push(&fiber->headers_in.headers);
if (h == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_fiber: ngx_list_push failed");
return NGX_ERROR;
}
h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash('r', 'a'), 'n'), 'g'), 'e');
h->key.data = (u_char*)"Range";
h->key.len = sizeof("Range") - 1;
h->lowcase_key = (u_char*)"range";
fiber->headers_in.range = h;
}
else
{
h = fiber->headers_in.range;
}
h->value.data = ngx_pnalloc(r->pool, sizeof(RANGE_FORMAT) + 2 * NGX_OFF_T_LEN);
if (h->value.data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_fiber: ngx_pnalloc failed");
return NGX_ERROR;
}
return NGX_OK;
}
// this function is required in order to support buffer reuse - it hooks the
// initial call to ngx_http_handler. after this function returns the upstream
// is already allocated by the proxy module, so it's possible to set its buffer
static void
ngx_http_parallel_fiber_initial_wev_handler(ngx_http_request_t *r)
{
ngx_http_parallel_fiber_ctx_t* fiber;
ngx_http_upstream_t* u;
ngx_connection_t *c;
c = r->connection;
// call the default request handler
r->write_event_handler = ngx_http_handler;
ngx_http_handler(r);
// if request was destoryed ignore
if (c->destroyed)
{
return;
}
// at this point the upstream should have been allocated by the proxy module
u = r->upstream;
if (u == NULL)
{
return;
}
// initialize the upstream buffer
fiber = ngx_http_get_module_ctx(r, ngx_http_parallel_module);
u->buffer = *fiber->cl->buf;
// initialize the headers list
u->headers_in.headers = fiber->upstream_headers;
u->headers_in.headers.last = &u->headers_in.headers.part;
}
static ngx_int_t
ngx_http_parallel_start_fiber(
ngx_http_request_t *r,
ngx_http_parallel_fiber_ctx_t* fiber,
uint64_t chunk_index)
{
ngx_http_parallel_loc_conf_t *conf;
ngx_http_parallel_ctx_t *ctx;
ngx_http_request_t* sr;
ngx_str_t args = ngx_null_string;
ngx_table_elt_t *h;
ngx_int_t rc;
size_t alloc_size;
off_t start_offset;
off_t end_offset;
ngx_chain_t* cl;
ngx_buf_t* b;
ctx = ngx_http_get_module_ctx(r, ngx_http_parallel_module);
conf = ngx_http_get_module_loc_conf(r, ngx_http_parallel_module);
// get a buffer for the response
cl = ngx_chain_get_free_buf(r->pool, &ctx->free);
if (cl == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_start_fiber: ngx_chain_get_free_buf failed");
return NGX_ERROR;
}
b = cl->buf;
if (cl->buf->start == NULL)
{
alloc_size = conf->max_headers_size + conf->max_chunk_size;
b->start = ngx_palloc(r->pool, alloc_size);
if (b->start == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_start_fiber: ngx_palloc failed");
return NGX_ERROR;
}
b->pos = b->start;
b->last = b->start;
b->end = b->last + alloc_size;
b->temporary = 1;
b->tag = (ngx_buf_tag_t)&ngx_http_parallel_module;
ctx->allocated_count++;
}
// allocate a list for the input headers
if (ngx_list_init(&fiber->upstream_headers, r->pool, 8,
sizeof(ngx_table_elt_t)) != NGX_OK)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_start_fiber: ngx_list_init failed");
return NGX_ERROR;
}
// set the range
if (ctx->fiber_count != 1)
{
start_offset = ctx->range.start;
if (chunk_index < ctx->fiber_count)
{
start_offset += chunk_index * ctx->chunk_size;
}
else
{
start_offset += ctx->initial_requested_size +
(chunk_index - ctx->fiber_count) * ctx->chunk_size;
}
end_offset = start_offset + ctx->chunk_size;
if (ctx->range.end != 0 && ctx->range.end < end_offset)
{
end_offset = ctx->range.end;
}
h = fiber->headers_in.range;
h->value.len = ngx_sprintf(
h->value.data,
RANGE_FORMAT,
start_offset,
end_offset - 1) - h->value.data;
h->value.data[h->value.len] = '\0';
}
// start the request
r->headers_in = fiber->headers_in;
rc = ngx_http_subrequest(r, &ctx->sr_uri, &args, &sr, fiber->psr,
NGX_HTTP_SUBREQUEST_WAITED | NGX_HTTP_SUBREQUEST_IN_MEMORY);
r->headers_in = ctx->original_headers_in;
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_start_fiber: ngx_http_subrequest failed %i", rc);
return rc;
}
ngx_http_set_ctx(sr, fiber, ngx_http_parallel_module);
ctx->active_fibers++;
sr->write_event_handler = ngx_http_parallel_fiber_initial_wev_handler;
sr->method = r->method; // copy the method to the subrequest
sr->method_name = r->method_name; // (ngx_http_subrequest always uses GET)
sr->header_in = r->header_in;
// fix the last pointer in headers_in (from echo-nginx-module)
if (fiber->headers_in.headers.last == &fiber->headers_in.headers.part) {
sr->headers_in.headers.last = &sr->headers_in.headers.part;
}
fiber->chunk_index = chunk_index;
fiber->cl = cl;
fiber->sr = sr;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_start_fiber: started fiber %uL", chunk_index);
return NGX_OK;
}
static ngx_int_t
ngx_http_parallel_start_fibers(
ngx_http_parallel_ctx_t *ctx,
ngx_http_request_t *r,
ngx_http_parallel_loc_conf_t *conf)
{
ngx_http_parallel_fiber_ctx_t* fiber;
ngx_int_t rc;
while (ctx->free_fibers &&
(ctx->free || ctx->allocated_count < conf->max_buffer_count) &&
ctx->next_request_chunk < ctx->chunk_count)
{
// remove from free list
fiber = ctx->free_fibers;
ctx->free_fibers = fiber->next;
// start the fiber
rc = ngx_http_parallel_start_fiber(r, fiber, ctx->next_request_chunk);
if (rc != NGX_OK)
{
return rc;
}
ctx->next_request_chunk++;
}
return NGX_OK;
}
static void
ngx_http_parallel_calculate_key(
u_char* key,
ngx_http_request_t *r)
{
ngx_md5_t md5;
ngx_md5_init(&md5);
if (r->headers_in.host != NULL)
{
ngx_md5_update(&md5, r->headers_in.host->value.data, r->headers_in.host->value.len);
}
ngx_md5_update(&md5, r->uri.data, r->uri.len);
ngx_md5_final(key, &md5);
}
static ngx_int_t
ngx_http_parallel_init_chunks(
ngx_http_parallel_ctx_t *ctx,
ngx_http_request_t *r,
ngx_http_headers_out_t* headers_out)
{
ngx_http_parallel_loc_conf_t *conf;
ngx_table_elt_t *h;
ngx_str_t* content_range;
uint64_t missing_chunks_mask;
off_t instance_length;
off_t remaining_length;
off_t content_length;
ngx_int_t rc;
// find the instance length
content_range = ngx_http_parallel_header_get_value(
&headers_out->headers,
&content_range_name,
content_range_hash);
if (content_range == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_parallel_init_chunks: failed to get content-range header");
return NGX_HTTP_BAD_GATEWAY;
}
instance_length = ngx_http_parallel_get_instance_length(
content_range,
r->connection->log);
if (instance_length < 0)
{
return NGX_HTTP_BAD_GATEWAY;
}
conf = ngx_http_get_module_loc_conf(r, ngx_http_parallel_module);
// update cache
if (conf->content_length_cache_zone != NULL &&
instance_length != ctx->cached_response_length)
{
if (!ctx->key_inited)
{
ngx_http_parallel_calculate_key(ctx->key, r);
}
ngx_fixed_buffer_cache_store(
conf->content_length_cache_zone,
ctx->key,
(u_char*)&instance_length,
1);
}
// find the content length
content_length = instance_length;
if (ctx->range.end != 0 && ctx->range.end < content_length)
{
content_length = ctx->range.end;
}
if (content_length < ctx->range.start)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_parallel_init_chunks: "
"unexpected, content length %O less than range start %O",
content_length, ctx->range.start);
return NGX_HTTP_BAD_GATEWAY;
}
content_length -= ctx->range.start;
// find the chunk size and count
if (content_length <= (off_t)ctx->initial_requested_size)
{
ctx->chunk_count = DIV_CEIL(content_length, ctx->chunk_size);
ctx->last_chunk_size = content_length + ctx->chunk_size -
ctx->chunk_count * ctx->chunk_size;
}
else
{
remaining_length = content_length - ctx->initial_requested_size;
if (remaining_length <= (off_t)(ctx->fiber_count * conf->min_chunk_size))
{
ctx->chunk_size = conf->min_chunk_size;
}
else if (remaining_length >= (off_t)(ctx->fiber_count * conf->max_chunk_size))
{
ctx->chunk_size = conf->max_chunk_size;
}
else
{
ctx->chunk_size = DIV_CEIL(remaining_length, ctx->fiber_count);
}
ctx->chunk_count = DIV_CEIL(remaining_length, ctx->chunk_size);
ctx->last_chunk_size = remaining_length + ctx->chunk_size -
ctx->chunk_count * ctx->chunk_size;
ctx->chunk_count += ctx->fiber_count;
}
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_chunks: "
"chunk count %uL chunk size %uz last chunk size %uz",
ctx->chunk_count, ctx->chunk_size, ctx->last_chunk_size);
// check for missing chunks
missing_chunks_mask = ULLONG_MAX;
if (ctx->chunk_count < 64)
{
missing_chunks_mask = ((1ULL << ctx->chunk_count) - 1);
}
if ((ctx->missing_chunks & missing_chunks_mask) != 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_parallel_init_chunks: "
"missing chunks 0x%uxL chunk count %uL",
ctx->missing_chunks, ctx->chunk_count);
return NGX_HTTP_BAD_GATEWAY;
}
// initialize the chunks array (null terminated)
ctx->chunks = ngx_pcalloc(r->pool, sizeof(ctx->chunks[0]) * (ctx->chunk_count + 1));
if (ctx->chunks == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_chunks: ngx_pcalloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// save headers for consistency check
if (conf->consistency_check_etag)
{
h = headers_out->etag;
if (h != NULL)
{
ctx->etag.data = ngx_pstrdup(r->pool, &h->value);
if (ctx->etag.data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_chunks: ngx_pstrdup failed (1)");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx->etag.len = h->value.len;
}
}
if (conf->consistency_check_last_modified)
{
h = headers_out->last_modified;
if (h != NULL)
{
ctx->last_modified.data = ngx_pstrdup(r->pool, &h->value);
if (ctx->last_modified.data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_chunks: ngx_pstrdup failed (2)");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx->last_modified.len = h->value.len;
}
}
// build the response headers
r->headers_out = *headers_out;
if (ctx->range.is_range_request)
{
// leave the status as 206 and update the content range
content_range->data = ngx_pnalloc(
r->pool, sizeof(CONTENT_RANGE_FORMAT) + 3 * NGX_OFF_T_LEN);
if (content_range->data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_chunks: ngx_pnalloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
content_range->len = ngx_sprintf(content_range->data,
CONTENT_RANGE_FORMAT,
ctx->range.start,
ctx->range.start + content_length - 1,
instance_length) - content_range->data;
}
else
{
// change status to 200 and clear the content range
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.status_line.len = 0;
ngx_http_parallel_header_clear_value(
&r->headers_out.headers, &content_range_name, content_range_hash);
}
ngx_http_clear_content_length(r);
r->headers_out.content_length_n = content_length;
// send the response headers
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_parallel_init_chunks: ngx_http_send_header failed %i", rc);
return rc;
}
return NGX_OK;
}