public
Description: pkgsrc repository for Joyent.com accelerators
Clone URL: git://github.com/joevandyk/pkgsrc.git
Search Repo:
Added the nginx fair balancer module

See http://github.com/gnosek/nginx-upstream-fair/tree/master for details.
joevandyk (author)
Tue Apr 29 17:42:57 -0700 2008
commit  d37a66cc9e6188da7ae16be995ec2de07ec8ddf2
tree    d8f2bdac59990863ec65cfbe55477304a42aad4c
parent  a1b28f62ef84d1d6ae7bbdf13c1bbc8d5ba14032
...
1
2
 
3
...
1
2
3
4
0
@@ -1,4 +1,5 @@
0
 work
0
 distfiles
0
+packages
0
 *~
...
1
2
3
 
4
5
6
...
18
19
20
 
 
 
21
22
23
...
1
2
3
4
5
6
7
...
19
20
21
22
23
24
25
26
27
0
@@ -1,6 +1,7 @@
0
 # $NetBSD: Makefile,v 1.1.1.1 2007/12/11 09:12:26 ghen Exp $
0
 
0
 DISTNAME=    nginx-0.5.33
0
+PKGREVISION= 2-with-fair-balancer,stub-status
0
 CATEGORIES=    www
0
 MASTER_SITES=    http://sysoev.ru/nginx/
0
 
0
@@ -18,6 +19,9 @@
0
 CONFIGURE_ARGS+=  --error-log-path=${NGINX_LOGDIR}/error.log
0
 CONFIGURE_ARGS+=  --http-log-path=${NGINX_LOGDIR}/access.log
0
 CONFIGURE_ARGS+=  --with-mail
0
+CONFIGURE_ARGS+= --add-module=added_modules/fair_balancer
0
+CONFIGURE_ARGS+= --with-http_stub_status_module
0
+CONFIGURE_ARGS+= --with-http_ssl_module
0
 
0
 .include "../../mk/bsd.prefs.mk"
0
 
...
3
4
5
6
 
 
 
 
...
3
4
5
 
6
7
8
9
0
@@ -3,5 +3,8 @@
0
 SHA1 (nginx-0.5.33.tar.gz) = d8c321126c57d3a5cf06914541465bf44a6f3904
0
 RMD160 (nginx-0.5.33.tar.gz) = 1836b4019f450b562bbffade6ea2046f6b640062
0
 Size (nginx-0.5.33.tar.gz) = 485441 bytes
0
-SHA1 (patch-aa) = ffc4de0134bd3d1808bdd1462e4602a11159d996
0
+SHA1 (patch-aa) = f64f38468e5ba1d2ff46657f8d7ac61d6d29e270
0
+SHA1 (patch-ab) = 5cf7df6f3ed74dc02ff824ed3b29150ba746d27e
0
+SHA1 (patch-ac) = 45111d254fadbd0e310fb16986464b32d0b0100b
0
+SHA1 (patch-ad) = a584ebc2169512f24d8bf6583d9509b7e81f9b92
...
1
2
3
 
4
5
6
7
...
9
10
11
12
13
14
 
 
 
15
16
17
 
18
19
20
...
23
24
25
26
 
27
28
29
...
31
32
33
34
 
35
36
37
...
40
41
42
43
 
44
45
46
...
62
63
64
65
 
66
67
68
...
1
2
 
3
4
5
6
7
...
9
10
11
 
 
 
12
13
14
15
16
 
17
18
19
20
...
23
24
25
 
26
27
28
29
...
31
32
33
 
34
35
36
37
...
40
41
42
 
43
44
45
46
...
62
63
64
 
65
66
67
68
0
@@ -1,6 +1,6 @@
0
 $NetBSD: patch-aa,v 1.1.1.1 2007/12/11 09:12:26 ghen Exp $
0
 
0
---- conf/nginx.conf.orig 2007-01-18 08:08:18.000000000 +0100
0
+--- conf/nginx.conf.orig 2007-01-18 07:08:18.000000000 +0000
0
 +++ conf/nginx.conf
0
 @@ -2,11 +2,11 @@
0
  #user nobody;
0
0
@@ -9,12 +9,12 @@
0
 -#error_log logs/error.log;
0
 -#error_log logs/error.log notice;
0
 -#error_log logs/error.log info;
0
-+#error_log %%NGINX_LOGDIR%%/error.log;
0
-+#error_log %%NGINX_LOGDIR%%/error.log notice;
0
-+#error_log %%NGINX_LOGDIR%%/error.log info;
0
++#error_log /opt/local/var/log/nginx/error.log;
0
++#error_log /opt/local/var/log/nginx/error.log notice;
0
++#error_log /opt/local/var/log/nginx/error.log info;
0
  
0
 -#pid logs/nginx.pid;
0
-+#pid %%NGINX_LOGDIR%%/nginx.pid;
0
++#pid /opt/local/var/log/nginx/nginx.pid;
0
  
0
  
0
  events {
0
@@ -23,7 +23,7 @@
0
  
0
  http {
0
 - include conf/mime.types;
0
-+ include %%PKG_SYSCONFDIR%%/mime.types;
0
++ include /opt/local/etc/nginx/mime.types;
0
      default_type application/octet-stream;
0
  
0
      #log_format main '$remote_addr - $remote_user [$time_local] $request '
0
@@ -31,7 +31,7 @@
0
      # '"$http_user_agent" "$http_x_forwarded_for"';
0
  
0
 - #access_log logs/access.log main;
0
-+ #access_log %%NGINX_LOGDIR%%/access.log main;
0
++ #access_log /opt/local/var/log/nginx/access.log main;
0
  
0
      sendfile on;
0
      #tcp_nopush on;
0
@@ -40,7 +40,7 @@
0
          #charset koi8-r;
0
  
0
 - #access_log logs/host.access.log main;
0
-+ #access_log %%NGINX_LOGDIR%%/host.access.log main;
0
++ #access_log /opt/local/var/log/nginx/host.access.log main;
0
  
0
          location / {
0
 - root html;
0
@@ -62,7 +62,7 @@
0
          # fastcgi_index index.php;
0
          # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
0
 - # include conf/fastcgi_params;
0
-+ # include %%PKG_SYSCONFDIR%%/fastcgi_params;
0
++ # include /opt/local/etc/nginx/fastcgi_params;
0
          #}
0
  
0
          # deny access to .htaccess files, if Apache's document root
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,887 @@
0
+$NetBSD$
0
+
0
+--- src/http/modules/ngx_http_upstream_fair_module.c.orig 2008-04-30 00:08:41.842749471 +0000
0
++++ src/http/modules/ngx_http_upstream_fair_module.c
0
+@@ -0,0 +1,881 @@
0
++/*
0
++ * Copyright (C) 2007 Grzegorz Nosek
0
++ * Work sponsored by Ezra Zygmuntowicz & EngineYard.com
0
++ *
0
++ * Based on nginx source (C) Igor Sysoev
0
++ */
0
++
0
++#include <ngx_config.h>
0
++#include <ngx_core.h>
0
++#include <ngx_http.h>
0
++
0
++typedef struct {
0
++ ngx_atomic_t nreq;
0
++ ngx_atomic_t last_active;
0
++} ngx_http_upstream_fair_shared_t;
0
++
0
++
0
++typedef struct {
0
++ ngx_rbtree_node_t node;
0
++ ngx_cycle_t *cycle;
0
++ void *peers; /* forms a unique cookie together with cycle */
0
++ ngx_int_t refcount; /* accessed only under shmtx_lock */
0
++ ngx_http_upstream_fair_shared_t stats[1];
0
++} ngx_http_upstream_fair_shm_block_t;
0
++
0
++
0
++typedef struct {
0
++ ngx_cycle_t *cycle;
0
++ ngx_http_upstream_fair_shm_block_t *shared;
0
++ ngx_http_upstream_rr_peers_t *rrp;
0
++ ngx_uint_t current;
0
++ ngx_uint_t size_err:1;
0
++} ngx_http_upstream_fair_peers_t;
0
++
0
++
0
++#define NGX_PEER_INVALID (~0UL)
0
++
0
++
0
++typedef struct {
0
++ ngx_http_upstream_fair_shared_t *shared;
0
++ ngx_http_upstream_rr_peers_t *rrp;
0
++ ngx_http_upstream_fair_peers_t *peers;
0
++ ngx_uint_t current;
0
++ uintptr_t *tried;
0
++ uintptr_t data;
0
++} ngx_http_upstream_fair_peer_data_t;
0
++
0
++
0
++static ngx_int_t ngx_http_upstream_fair_init_module(ngx_cycle_t *cycle);
0
++static ngx_int_t ngx_http_upstream_init_fair(ngx_conf_t *cf,
0
++ ngx_http_upstream_srv_conf_t *us);
0
++static ngx_int_t ngx_http_upstream_get_fair_peer(ngx_peer_connection_t *pc,
0
++ void *data);
0
++static void ngx_http_upstream_free_fair_peer(ngx_peer_connection_t *pc,
0
++ void *data, ngx_uint_t state);
0
++static ngx_int_t ngx_http_upstream_init_fair_peer(ngx_http_request_t *r,
0
++ ngx_http_upstream_srv_conf_t *us);
0
++static char *ngx_http_upstream_fair(ngx_conf_t *cf, ngx_command_t *cmd,
0
++ void *conf);
0
++static char *ngx_http_upstream_fair_set_shm_size(ngx_conf_t *cf,
0
++ ngx_command_t *cmd, void *conf);
0
++
0
++#if (NGX_HTTP_SSL)
0
++static ngx_int_t ngx_http_upstream_fair_set_session(ngx_peer_connection_t *pc,
0
++ void *data);
0
++static void ngx_http_upstream_fair_save_session(ngx_peer_connection_t *pc,
0
++ void *data);
0
++#endif
0
++
0
++static ngx_command_t ngx_http_upstream_fair_commands[] = {
0
++
0
++ { ngx_string("fair"),
0
++ NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS,
0
++ ngx_http_upstream_fair,
0
++ 0,
0
++ 0,
0
++ NULL },
0
++
0
++ { ngx_string("upstream_fair_shm_size"),
0
++ NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
0
++ ngx_http_upstream_fair_set_shm_size,
0
++ 0,
0
++ 0,
0
++ NULL },
0
++
0
++ ngx_null_command
0
++};
0
++
0
++
0
++static ngx_http_module_t ngx_http_upstream_fair_module_ctx = {
0
++ NULL, /* preconfiguration */
0
++ NULL, /* postconfiguration */
0
++
0
++ NULL, /* create main configuration */
0
++ NULL, /* init main configuration */
0
++
0
++ NULL, /* create server configuration */
0
++ NULL, /* merge server configuration */
0
++
0
++ NULL, /* create location configuration */
0
++ NULL /* merge location configuration */
0
++};
0
++
0
++
0
++ngx_module_t ngx_http_upstream_fair_module = {
0
++ NGX_MODULE_V1,
0
++ &ngx_http_upstream_fair_module_ctx, /* module context */
0
++ ngx_http_upstream_fair_commands, /* module directives */
0
++ NGX_HTTP_MODULE, /* module type */
0
++ NULL, /* init master */
0
++ NULL, /* init module */
0
++ NULL, /* init process */
0
++ NULL, /* init thread */
0
++ NULL, /* exit thread */
0
++ NULL, /* exit process */
0
++ NULL, /* exit master */
0
++ NGX_MODULE_V1_PADDING
0
++};
0
++
0
++
0
++static ngx_uint_t ngx_http_upstream_fair_shm_size;
0
++static ngx_shm_zone_t * ngx_http_upstream_fair_shm_zone;
0
++static ngx_rbtree_t * ngx_http_upstream_fair_rbtree;
0
++
0
++static int
0
++ngx_http_upstream_fair_compare_rbtree_node(const ngx_rbtree_node_t *v_left,
0
++ const ngx_rbtree_node_t *v_right)
0
++{
0
++ ngx_http_upstream_fair_shm_block_t *left, *right;
0
++
0
++ left = (ngx_http_upstream_fair_shm_block_t *) v_left;
0
++ right = (ngx_http_upstream_fair_shm_block_t *) v_right;
0
++
0
++ if (left->cycle < right->cycle) {
0
++ return -1;
0
++ } else if (left->cycle > right->cycle) {
0
++ return 1;
0
++ } else { /* left->cycle == right->cycle */
0
++ if (left->peers < right->peers) {
0
++ return -1;
0
++ } else if (left->peers > right->peers) {
0
++ return 1;
0
++ } else {
0
++ return 0;
0
++ }
0
++ }
0
++}
0
++
0
++/*
0
++ * generic functions start here
0
++ */
0
++static void
0
++ngx_rbtree_generic_insert(ngx_rbtree_node_t *temp,
0
++ ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel,
0
++ int (*compare)(const ngx_rbtree_node_t *left, const ngx_rbtree_node_t *right))
0
++{
0
++ for ( ;; ) {
0
++ if (node->key < temp->key) {
0
++
0
++ if (temp->left == sentinel) {
0
++ temp->left = node;
0
++ break;
0
++ }
0
++
0
++ temp = temp->left;
0
++
0
++ } else if (node->key > temp->key) {
0
++
0
++ if (temp->right == sentinel) {
0
++ temp->right = node;
0
++ break;
0
++ }
0
++
0
++ temp = temp->right;
0
++
0
++ } else { /* node->key == temp->key */
0
++ if (compare(node, temp) < 0) {
0
++
0
++ if (temp->left == sentinel) {
0
++ temp->left = node;
0
++ break;
0
++ }
0
++
0
++ temp = temp->left;
0
++
0
++ } else {
0
++
0
++ if (temp->right == sentinel) {
0
++ temp->right = node;
0
++ break;
0
++ }
0
++
0
++ temp = temp->right;
0
++ }
0
++ }
0
++ }
0
++
0
++ node->parent = temp;
0
++ node->left = sentinel;
0
++ node->right = sentinel;
0
++ ngx_rbt_red(node);
0
++}
0
++
0
++#define NGX_BITVECTOR_ELT_SIZE (sizeof(uintptr_t) * 8)
0
++
0
++static uintptr_t *
0
++ngx_bitvector_alloc(ngx_pool_t *pool, ngx_uint_t size, uintptr_t *small)
0
++{
0
++ ngx_uint_t nelts = (size + NGX_BITVECTOR_ELT_SIZE - 1) / NGX_BITVECTOR_ELT_SIZE;
0
++
0
++ if (small && nelts == 1) {
0
++ *small = 0;
0
++ return small;
0
++ }
0
++
0
++ return ngx_pcalloc(pool, nelts * NGX_BITVECTOR_ELT_SIZE);
0
++}
0
++
0
++static ngx_int_t
0
++ngx_bitvector_test(uintptr_t *bv, ngx_uint_t bit)
0
++{
0
++ ngx_uint_t n, m;
0
++
0
++ n = bit / NGX_BITVECTOR_ELT_SIZE;
0
++ m = 1 << (bit % NGX_BITVECTOR_ELT_SIZE);
0
++
0
++ return bv[n] & m;
0
++}
0
++
0
++static void
0
++ngx_bitvector_set(uintptr_t *bv, ngx_uint_t bit)
0
++{
0
++ ngx_uint_t n, m;
0
++
0
++ n = bit / NGX_BITVECTOR_ELT_SIZE;
0
++ m = 1 << (bit % NGX_BITVECTOR_ELT_SIZE);
0
++
0
++ bv[n] |= m;
0
++}
0
++
0
++/*
0
++ * generic functions end here
0
++ */
0
++
0
++static void
0
++ngx_http_upstream_fair_rbtree_insert(ngx_rbtree_node_t *temp,
0
++ ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) {
0
++
0
++ ngx_rbtree_generic_insert(temp, node, sentinel,
0
++ ngx_http_upstream_fair_compare_rbtree_node);
0
++}
0
++
0
++
0
++static ngx_int_t
0
++ngx_http_upstream_fair_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data)
0
++{
0
++ ngx_slab_pool_t *shpool;
0
++ ngx_rbtree_t *tree;
0
++ ngx_rbtree_node_t *sentinel;
0
++
0
++ if (data) {
0
++ shm_zone->data = data;
0
++ return NGX_OK;
0
++ }
0
++
0
++ shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
0
++ tree = ngx_slab_alloc(shpool, sizeof *tree);
0
++ if (tree == NULL) {
0
++ return NGX_ERROR;
0
++ }
0
++
0
++ sentinel = ngx_slab_alloc(shpool, sizeof *sentinel);
0
++ if (sentinel == NULL) {
0
++ return NGX_ERROR;
0
++ }
0
++
0
++ ngx_rbtree_sentinel_init(sentinel);
0
++ tree->root = sentinel;
0
++ tree->sentinel = sentinel;
0
++ tree->insert = ngx_http_upstream_fair_rbtree_insert;
0
++ shm_zone->data = tree;
0
++ ngx_http_upstream_fair_rbtree = tree;
0
++
0
++ return NGX_OK;
0
++}
0
++
0
++
0
++static char *
0
++ngx_http_upstream_fair_set_shm_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0
++{
0
++ ssize_t new_shm_size;
0
++ ngx_str_t *value;
0
++
0
++ value = cf->args->elts;
0
++
0
++ new_shm_size = ngx_parse_size(&value[1]);
0
++ if (new_shm_size == NGX_ERROR) {
0
++ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Invalid memory area size `%V'", &value[1]);
0
++ return NGX_CONF_ERROR;
0
++ }
0
++
0
++ new_shm_size = ngx_align(new_shm_size, ngx_pagesize);
0
++
0
++ if (new_shm_size < 8 * (ssize_t) ngx_pagesize) {
0
++ ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "The upstream_fair_shm_size value must be at least %udKiB", (8 * ngx_pagesize) >> 10);
0
++ new_shm_size = 8 * ngx_pagesize;
0
++ }
0
++
0
++ if (ngx_http_upstream_fair_shm_size &&
0
++ ngx_http_upstream_fair_shm_size != (ngx_uint_t) new_shm_size) {
0
++ ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "Cannot change memory area size without restart, ignoring change");
0
++ } else {
0
++ ngx_http_upstream_fair_shm_size = new_shm_size;
0
++ }
0
++ ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "Using %udKiB of shared memory for upstream_fair", new_shm_size >> 10);
0
++
0
++ return NGX_CONF_OK;
0
++}
0
++
0
++
0
++static char *
0
++ngx_http_upstream_fair(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0
++{
0
++ ngx_http_upstream_srv_conf_t *uscf;
0
++
0
++ uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
0
++
0
++ uscf->peer.init_upstream = ngx_http_upstream_init_fair;
0
++
0
++ uscf->flags = NGX_HTTP_UPSTREAM_CREATE
0
++ |NGX_HTTP_UPSTREAM_WEIGHT
0
++ |NGX_HTTP_UPSTREAM_MAX_FAILS
0
++ |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
0
++ |NGX_HTTP_UPSTREAM_DOWN;
0
++
0
++ return NGX_CONF_OK;
0
++}
0
++
0
++
0
++static ngx_int_t
0
++ngx_http_upstream_init_fair(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
0
++{
0
++ ngx_http_upstream_fair_peers_t *peers;
0
++ ngx_uint_t n;
0
++ ngx_str_t *shm_name;
0
++
0
++ /* do the dirty work using rr module */
0
++ if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
0
++ return NGX_ERROR;
0
++ }
0
++
0
++ /* setup our wrapper around rr */
0
++ peers = ngx_palloc(cf->pool, sizeof *peers);
0
++ if (peers == NULL) {
0
++ return NGX_ERROR;
0
++ }
0
++ peers->rrp = us->peer.data;
0
++ us->peer.data = peers;
0
++ n = peers->rrp->number;
0
++
0
++ shm_name = ngx_palloc(cf->pool, sizeof *shm_name);
0
++ shm_name->len = sizeof("upstream_fair");
0
++ shm_name->data = (unsigned char *) "upstream_fair";
0
++
0
++ if (ngx_http_upstream_fair_shm_size == 0) {
0
++ ngx_http_upstream_fair_shm_size = 8 * ngx_pagesize;
0
++ }
0
++
0
++ ngx_http_upstream_fair_shm_zone = ngx_shared_memory_add(
0
++ cf, shm_name, ngx_http_upstream_fair_shm_size, &ngx_http_upstream_fair_module);
0
++ if (ngx_http_upstream_fair_shm_zone == NULL) {
0
++ return NGX_ERROR;
0
++ }
0
++ ngx_http_upstream_fair_shm_zone->init = ngx_http_upstream_fair_init_shm_zone;
0
++
0
++ peers->cycle = cf->cycle;
0
++ peers->shared = NULL;
0
++ peers->current = n - 1;
0
++ peers->size_err = 0;
0
++
0
++ us->peer.init = ngx_http_upstream_init_fair_peer;
0
++
0
++ return NGX_OK;
0
++}
0
++
0
++
0
++static void
0
++ngx_http_upstream_fair_update_nreq(ngx_http_upstream_fair_peer_data_t *fp, int delta, ngx_log_t *log)
0
++{
0
++ ngx_http_upstream_fair_shared_t *fs;
0
++
0
++ fs = &fp->shared[fp->current];
0
++
0
++ ngx_atomic_fetch_add(&fs->nreq, delta);
0
++
0
++ fs->last_active = ngx_current_msec;
0
++
0
++ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0, "[upstream_fair] nreq for peer %ui now %d", fp->current, fs->nreq);
0
++}
0
++
0
++/*
0
++ * SCHED_TIME_BITS is the portion of an ngx_uint_t which represents the
0
++ * last_time_delta part (time since last activity in msec). The rest
0
++ * (top bits) represents the number of currently processed requests.
0
++ *
0
++ * The value is not too critical because overflow is handled via
0
++ * saturation. With the default value of 24, scheduling is exact for
0
++ * requests shorter than 16777 sec and for less than 256 requests per
0
++ * backend (on 32-bit architectures). Beyond these limits, the algorithm
0
++ * essentially falls back to pure weighted round-robin
0
++ *
0
++ * A higher score means less suitable -- this changed from previous
0
++ * releases.
0
++ *
0
++ * The `delta' parameter is bit-negated so that high values yield low
0
++ * scores and get chosen more often.
0
++ */
0
++
0
++#define SCHED_TIME_BITS 24
0
++#define SCHED_NREQ_MAX ((~0UL) >> SCHED_TIME_BITS)
0
++#define SCHED_TIME_MAX ((1 << SCHED_TIME_BITS) - 1)
0
++#define SCHED_SCORE(nreq,delta) (((nreq) << SCHED_TIME_BITS) | (~(delta)))
0
++#define ngx_upstream_fair_min(a,b) (((a) < (b)) ? (a) : (b))
0
++
0
++static ngx_uint_t
0
++ngx_http_upstream_fair_sched_score(ngx_peer_connection_t *pc,
0
++ ngx_http_upstream_fair_shared_t *fs,
0
++ ngx_http_upstream_rr_peer_t *peer, ngx_uint_t n)
0
++{
0
++ ngx_msec_t last_active_delta;
0
++
0
++ last_active_delta = ngx_current_msec - fs->last_active;
0
++ if ((ngx_int_t) last_active_delta < 0) {
0
++ ngx_log_error(NGX_LOG_WARN, pc->log, 0, "[upstream_fair] Clock skew of at least %i msec detected", -(ngx_int_t) last_active_delta);
0
++
0
++ /* a pretty arbitrary value */
0
++ last_active_delta = abs(last_active_delta);
0
++ }
0
++
0
++ /* sanity check */
0
++ if ((ngx_int_t)fs->nreq < 0) {
0
++ ngx_log_error(NGX_LOG_WARN, pc->log, 0, "[upstream_fair] upstream %ui has negative nreq (%i)", n, fs->nreq);
0
++ return SCHED_SCORE(0, last_active_delta);
0
++ }
0
++
0
++ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] nreq = %i, last_active_delta = %ui", fs->nreq, last_active_delta);
0
++
0
++ return SCHED_SCORE(
0
++ ngx_upstream_fair_min(fs->nreq, SCHED_NREQ_MAX),
0
++ ngx_upstream_fair_min(last_active_delta, SCHED_TIME_MAX));
0
++}
0
++
0
++/*
0
++ * the core of load balancing logic
0
++ */
0
++
0
++static ngx_int_t
0
++ngx_http_upstream_fair_try_peer(ngx_peer_connection_t *pc,
0
++ ngx_http_upstream_fair_peer_data_t *fp,
0
++ ngx_uint_t peer_id,
0
++ time_t now)
0
++{
0
++ ngx_http_upstream_rr_peer_t *peer;
0
++
0
++ if (ngx_bitvector_test(fp->tried, peer_id))
0
++ return NGX_BUSY;
0
++
0
++ peer = &fp->rrp->peer[peer_id];
0
++
0
++ if (!peer->down) {
0
++ if (peer->max_fails == 0 || peer->fails < peer->max_fails) {
0
++ return NGX_OK;
0
++ }
0
++
0
++ if (now - peer->accessed > peer->fail_timeout) {
0
++ peer->fails = 0;
0
++ return NGX_OK;
0
++ }
0
++ }
0
++
0
++ ngx_bitvector_set(fp->tried, peer_id);
0
++
0
++ if (pc)
0
++ pc->tries--;