-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathngx_stream_upstream.c
828 lines (629 loc) · 23.2 KB
/
ngx_stream_upstream.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
// annotated by chrono since 2016
//
// * ngx_stream_upstream
// * ngx_stream_upstream_server
// * ngx_stream_upstream_add
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
static ngx_int_t ngx_stream_upstream_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_stream_upstream_addr_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_stream_upstream_response_time_variable(
ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data);
// 解析upstream{}块,它相当于server{}块,级别相同
// 创建一个upstream{}块的配置信息
// 检查是否有同名的upstream{},如有则报错
// 加入main conf里的upstreams数组,之后就可以在这里找到所有的upstream{}
// 创建数组,存储upstream{}里的服务器信息
// 要求至少有一个server指定上游服务器,否则出错
static char *ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd,
void *dummy);
// 解析upstream{}里的server指令,确定一个上游服务器
// 在servers数组里添加一个元素
// 检查里面的weight等参数,设置ngx_stream_upstream_server_t结构体
// 从2开始,第一个是server的地址
// 要求server必须有端口号
static char *ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
// upstream{}的配置结构体,只有一个数组
// 用来存储每个上游upstream{}的信息
static void *ngx_stream_upstream_create_main_conf(ngx_conf_t *cf);
// 在ngx_stream_block解析完配置后调用,初始化main conf
// 检查每一个upstream{}块
// 查看是否有load balance初始化指针
// 没有指定特别的算法就使用默认的round robin
// 对本upstream{}块做初始化
static char *ngx_stream_upstream_init_main_conf(ngx_conf_t *cf, void *conf);
// 在stream{}块里的upstream相关指令,只有两个
static ngx_command_t ngx_stream_upstream_commands[] = {
// 定义upstream{}块
{ ngx_string("upstream"),
NGX_STREAM_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
ngx_stream_upstream,
0,
0,
NULL },
// 定义上游的服务器地址
{ ngx_string("server"),
NGX_STREAM_UPS_CONF|NGX_CONF_1MORE,
ngx_stream_upstream_server,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_stream_module_t ngx_stream_upstream_module_ctx = {
ngx_stream_upstream_add_variables, /* preconfiguration */
NULL, /* postconfiguration */
// upstream{}的配置结构体,只有一个数组
// 用来存储每个上游upstream{}的信息
ngx_stream_upstream_create_main_conf, /* create main configuration */
// 在ngx_stream_block解析完配置后调用,初始化main conf
// 检查每一个upstream{}块
// 查看是否有load balance初始化指针
// 没有指定特别的算法就使用默认的round robin
// 对本upstream{}块做初始化
ngx_stream_upstream_init_main_conf, /* init main configuration */
NULL, /* create server configuration */
NULL /* merge server configuration */
};
// upstream模块定义
ngx_module_t ngx_stream_upstream_module = {
NGX_MODULE_V1,
&ngx_stream_upstream_module_ctx, /* module context */
ngx_stream_upstream_commands, /* module directives */
NGX_STREAM_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
};
// 1.11.x新增的stream变量
static ngx_stream_variable_t ngx_stream_upstream_vars[] = {
{ ngx_string("upstream_addr"), NULL,
ngx_stream_upstream_addr_variable, 0,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_bytes_sent"), NULL,
ngx_stream_upstream_bytes_variable, 0,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_connect_time"), NULL,
ngx_stream_upstream_response_time_variable, 2,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_first_byte_time"), NULL,
ngx_stream_upstream_response_time_variable, 1,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_session_time"), NULL,
ngx_stream_upstream_response_time_variable, 0,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
{ ngx_string("upstream_bytes_received"), NULL,
ngx_stream_upstream_bytes_variable, 1,
NGX_STREAM_VAR_NOCACHEABLE, 0 },
ngx_stream_null_variable
};
static ngx_int_t
ngx_stream_upstream_add_variables(ngx_conf_t *cf)
{
ngx_stream_variable_t *var, *v;
for (v = ngx_stream_upstream_vars; v->name.len; v++) {
var = ngx_stream_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static ngx_int_t
ngx_stream_upstream_addr_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data)
{
u_char *p;
size_t len;
ngx_uint_t i;
ngx_stream_upstream_state_t *state;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
if (s->upstream_states == NULL || s->upstream_states->nelts == 0) {
v->not_found = 1;
return NGX_OK;
}
len = 0;
state = s->upstream_states->elts;
for (i = 0; i < s->upstream_states->nelts; i++) {
if (state[i].peer) {
len += state[i].peer->len;
}
len += 2;
}
p = ngx_pnalloc(s->connection->pool, len);
if (p == NULL) {
return NGX_ERROR;
}
v->data = p;
i = 0;
for ( ;; ) {
if (state[i].peer) {
p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len);
}
if (++i == s->upstream_states->nelts) {
break;
}
*p++ = ',';
*p++ = ' ';
}
v->len = p - v->data;
return NGX_OK;
}
static ngx_int_t
ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data)
{
u_char *p;
size_t len;
ngx_uint_t i;
ngx_stream_upstream_state_t *state;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
if (s->upstream_states == NULL || s->upstream_states->nelts == 0) {
v->not_found = 1;
return NGX_OK;
}
len = s->upstream_states->nelts * (NGX_OFF_T_LEN + 2);
p = ngx_pnalloc(s->connection->pool, len);
if (p == NULL) {
return NGX_ERROR;
}
v->data = p;
i = 0;
state = s->upstream_states->elts;
for ( ;; ) {
if (data == 1) {
p = ngx_sprintf(p, "%O", state[i].bytes_received);
} else {
p = ngx_sprintf(p, "%O", state[i].bytes_sent);
}
if (++i == s->upstream_states->nelts) {
break;
}
*p++ = ',';
*p++ = ' ';
}
v->len = p - v->data;
return NGX_OK;
}
static ngx_int_t
ngx_stream_upstream_response_time_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data)
{
u_char *p;
size_t len;
ngx_uint_t i;
ngx_msec_int_t ms;
ngx_stream_upstream_state_t *state;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
if (s->upstream_states == NULL || s->upstream_states->nelts == 0) {
v->not_found = 1;
return NGX_OK;
}
len = s->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2);
p = ngx_pnalloc(s->connection->pool, len);
if (p == NULL) {
return NGX_ERROR;
}
v->data = p;
i = 0;
state = s->upstream_states->elts;
for ( ;; ) {
if (data == 1) {
ms = state[i].first_byte_time;
} else if (data == 2) {
ms = state[i].connect_time;
} else {
ms = state[i].response_time;
}
if (ms != -1) {
ms = ngx_max(ms, 0);
p = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000);
} else {
*p++ = '-';
}
if (++i == s->upstream_states->nelts) {
break;
}
*p++ = ',';
*p++ = ' ';
}
v->len = p - v->data;
return NGX_OK;
}
// 解析upstream{}块,它相当于server{}块,级别相同
// 创建一个upstream{}块的配置信息
// 检查是否有同名的upstream{},如有则报错
// 加入main conf里的upstreams数组,之后就可以在这里找到所有的upstream{}
// 创建数组,存储upstream{}里的服务器信息
// 要求至少有一个server指定上游服务器,否则出错
static char *
ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
{
char *rv;
void *mconf;
ngx_str_t *value;
ngx_url_t u;
ngx_uint_t m;
ngx_conf_t pcf;
ngx_stream_module_t *module;
ngx_stream_conf_ctx_t *ctx, *stream_ctx;
ngx_stream_upstream_srv_conf_t *uscf;
ngx_memzero(&u, sizeof(ngx_url_t));
value = cf->args->elts;
// 指令的第一个参数是upstream块的名字
u.host = value[1];
u.no_resolve = 1;
u.no_port = 1;
// 创建或者获取一个upstream{}块的配置信息
// 获取时flags==0
// 检查是否有同名的upstream{},如果是创建时有则报错
// 加入main conf里的upstreams数组,之后就可以在这里找到所有的upstream{}
uscf = ngx_stream_upstream_add(cf, &u, NGX_STREAM_UPSTREAM_CREATE
|NGX_STREAM_UPSTREAM_WEIGHT
|NGX_STREAM_UPSTREAM_MAX_CONNS
|NGX_STREAM_UPSTREAM_MAX_FAILS
|NGX_STREAM_UPSTREAM_FAIL_TIMEOUT
|NGX_STREAM_UPSTREAM_DOWN
|NGX_STREAM_UPSTREAM_BACKUP);
if (uscf == NULL) {
return NGX_CONF_ERROR;
}
// tcp流处理的配置结构体
// 与http不同的是没有location,只有两级
// 在cycle->conf_ctx里存储的是stream{}级别的配置
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_conf_ctx_t));
if (ctx == NULL) {
return NGX_CONF_ERROR;
}
// 保存stream{}的配置上下文
// 也就是stream{}里的ngx_stream_conf_ctx_t
stream_ctx = cf->ctx;
// main_conf直接指向stream{}的main_conf
ctx->main_conf = stream_ctx->main_conf;
/* the upstream{}'s srv_conf */
// 分配存储srv_conf的数组,数量是ngx_stream_max_module
ctx->srv_conf = ngx_pcalloc(cf->pool,
sizeof(void *) * ngx_stream_max_module);
if (ctx->srv_conf == NULL) {
return NGX_CONF_ERROR;
}
// 存储本配置块的配置
// ngx_stream_upstream_module相当于ngx_stream_core_module
// 同时在main conf的servers数组里也有
ctx->srv_conf[ngx_stream_upstream_module.ctx_index] = uscf;
// 保存本upstream{}的配置数组
uscf->srv_conf = ctx->srv_conf;
// 遍历模块数组,调用每个stream模块create_srv_conf,创建配置结构体
// 但实际上除了负载均衡模块,其他模块不会在这里出现,没有意义
for (m = 0; cf->cycle->modules[m]; m++) {
if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) {
continue;
}
module = cf->cycle->modules[m]->ctx;
if (module->create_srv_conf) {
mconf = module->create_srv_conf(cf);
if (mconf == NULL) {
return NGX_CONF_ERROR;
}
ctx->srv_conf[cf->cycle->modules[m]->ctx_index] = mconf;
}
}
// 创建数组,准备存储upstream{}里的服务器信息
uscf->servers = ngx_array_create(cf->pool, 4,
sizeof(ngx_stream_upstream_server_t));
if (uscf->servers == NULL) {
return NGX_CONF_ERROR;
}
/* parse inside upstream{} */
// 暂存当前的解析上下文
pcf = *cf;
// 设置upstream模块的新解析上下文
// 使用刚才刚创建的配置结构体存储模块的配置信息
cf->ctx = ctx;
cf->cmd_type = NGX_STREAM_UPS_CONF;
// 递归解析事件相关模块
// 里面主要是server指令和负载均衡算法指令
rv = ngx_conf_parse(cf, NULL);
// 恢复之前保存的解析上下文
*cf = pcf;
if (rv != NGX_CONF_OK) {
return rv;
}
// 要求至少有一个server指定上游服务器,否则出错
if (uscf->servers->nelts == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"no servers are inside upstream");
return NGX_CONF_ERROR;
}
return rv;
}
// 解析upstream{}里的server指令,确定一个上游服务器
// 在servers数组里添加一个元素
// 检查里面的weight等参数,设置ngx_stream_upstream_server_t结构体
// 从2开始,第一个是server的地址
// 要求server必须有端口号
static char *
ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_stream_upstream_srv_conf_t *uscf = conf;
time_t fail_timeout;
ngx_str_t *value, s;
ngx_url_t u;
ngx_int_t weight, max_conns, max_fails;
ngx_uint_t i;
ngx_stream_upstream_server_t *us;
// 在servers数组里添加一个元素
us = ngx_array_push(uscf->servers);
if (us == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(us, sizeof(ngx_stream_upstream_server_t));
value = cf->args->elts;
weight = 1;
max_conns = 0;
max_fails = 1;
fail_timeout = 10;
// 检查里面的weight等参数,设置ngx_stream_upstream_server_t结构体
// 从2开始,第一个是server的地址
for (i = 2; i < cf->args->nelts; i++) {
if (ngx_strncmp(value[i].data, "weight=", 7) == 0) {
if (!(uscf->flags & NGX_STREAM_UPSTREAM_WEIGHT)) {
goto not_supported;
}
weight = ngx_atoi(&value[i].data[7], value[i].len - 7);
if (weight == NGX_ERROR || weight == 0) {
goto invalid;
}
continue;
}
if (ngx_strncmp(value[i].data, "max_conns=", 10) == 0) {
if (!(uscf->flags & NGX_STREAM_UPSTREAM_MAX_CONNS)) {
goto not_supported;
}
max_conns = ngx_atoi(&value[i].data[10], value[i].len - 10);
if (max_conns == NGX_ERROR) {
goto invalid;
}
continue;
}
if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) {
if (!(uscf->flags & NGX_STREAM_UPSTREAM_MAX_FAILS)) {
goto not_supported;
}
max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10);
if (max_fails == NGX_ERROR) {
goto invalid;
}
continue;
}
if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) {
if (!(uscf->flags & NGX_STREAM_UPSTREAM_FAIL_TIMEOUT)) {
goto not_supported;
}
s.len = value[i].len - 13;
s.data = &value[i].data[13];
fail_timeout = ngx_parse_time(&s, 1);
if (fail_timeout == (time_t) NGX_ERROR) {
goto invalid;
}
continue;
}
if (ngx_strcmp(value[i].data, "backup") == 0) {
if (!(uscf->flags & NGX_STREAM_UPSTREAM_BACKUP)) {
goto not_supported;
}
us->backup = 1;
continue;
}
if (ngx_strcmp(value[i].data, "down") == 0) {
if (!(uscf->flags & NGX_STREAM_UPSTREAM_DOWN)) {
goto not_supported;
}
us->down = 1;
continue;
}
goto invalid;
}
// 第一个参数是server地址,解析之
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
if (u.err) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"%s in upstream \"%V\"", u.err, &u.url);
}
return NGX_CONF_ERROR;
}
// 要求server必须有端口号
if (u.no_port) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"no port in upstream \"%V\"", &u.url);
return NGX_CONF_ERROR;
}
// 解析完毕,填充server信息
us->name = u.url;
us->addrs = u.addrs;
us->naddrs = u.naddrs;
us->weight = weight;
us->max_conns = max_conns;
us->max_fails = max_fails;
us->fail_timeout = fail_timeout;
return NGX_CONF_OK;
invalid:
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[i]);
return NGX_CONF_ERROR;
not_supported:
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"balancing method does not support parameter \"%V\"",
&value[i]);
return NGX_CONF_ERROR;
}
// 创建或者获取一个upstream{}块的配置信息
// 获取时flags==0
// 检查是否有同名的upstream{},如果是创建时有则报错
// 加入main conf里的upstreams数组,之后就可以在这里找到所有的upstream{}
ngx_stream_upstream_srv_conf_t *
ngx_stream_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
{
ngx_uint_t i;
ngx_stream_upstream_server_t *us;
ngx_stream_upstream_srv_conf_t *uscf, **uscfp;
ngx_stream_upstream_main_conf_t *umcf;
if (!(flags & NGX_STREAM_UPSTREAM_CREATE)) {
if (ngx_parse_url(cf->pool, u) != NGX_OK) {
if (u->err) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"%s in upstream \"%V\"", u->err, &u->url);
}
return NULL;
}
}
// 获取main 配置,里面有数组
umcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_upstream_module);
// 数组存储所有的upstream{}配置
uscfp = umcf->upstreams.elts;
// 检查是否有同名的upstream{},如有则报错
for (i = 0; i < umcf->upstreams.nelts; i++) {
// 比较名字
if (uscfp[i]->host.len != u->host.len
|| ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
!= 0)
{
continue;
}
if ((flags & NGX_STREAM_UPSTREAM_CREATE)
&& (uscfp[i]->flags & NGX_STREAM_UPSTREAM_CREATE))
{
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"duplicate upstream \"%V\"", &u->host);
return NULL;
}
if ((uscfp[i]->flags & NGX_STREAM_UPSTREAM_CREATE) && !u->no_port) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"upstream \"%V\" may not have port %d",
&u->host, u->port);
return NULL;
}
if ((flags & NGX_STREAM_UPSTREAM_CREATE) && !uscfp[i]->no_port) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"upstream \"%V\" may not have port %d in %s:%ui",
&u->host, uscfp[i]->port,
uscfp[i]->file_name, uscfp[i]->line);
return NULL;
}
if (uscfp[i]->port != u->port) {
continue;
}
if (flags & NGX_STREAM_UPSTREAM_CREATE) {
uscfp[i]->flags = flags;
}
// 返回找到的匹配upstream配置
return uscfp[i];
}
// 创建此upstream{}的srv配置结构体
uscf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_srv_conf_t));
if (uscf == NULL) {
return NULL;
}
// 填充基本信息
uscf->flags = flags;
uscf->host = u->host;
uscf->file_name = cf->conf_file->file.name.data;
uscf->line = cf->conf_file->line;
uscf->port = u->port;
uscf->no_port = u->no_port;
// upstream指定了一个具体的上游server
// 那么就把它加入servers数组
if (u->naddrs == 1 && (u->port || u->family == AF_UNIX)) {
uscf->servers = ngx_array_create(cf->pool, 1,
sizeof(ngx_stream_upstream_server_t));
if (uscf->servers == NULL) {
return NULL;
}
us = ngx_array_push(uscf->servers);
if (us == NULL) {
return NULL;
}
ngx_memzero(us, sizeof(ngx_stream_upstream_server_t));
us->addrs = u->addrs;
us->naddrs = 1;
}
// 加入main conf里的upstreams数组,之后就可以在这里找到所有的upstream{}
uscfp = ngx_array_push(&umcf->upstreams);
if (uscfp == NULL) {
return NULL;
}
*uscfp = uscf;
return uscf;
}
// upstream{}的配置结构体,只有一个数组
// 用来存储每个上游upstream{}的信息
static void *
ngx_stream_upstream_create_main_conf(ngx_conf_t *cf)
{
ngx_stream_upstream_main_conf_t *umcf;
umcf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_main_conf_t));
if (umcf == NULL) {
return NULL;
}
if (ngx_array_init(&umcf->upstreams, cf->pool, 4,
sizeof(ngx_stream_upstream_srv_conf_t *))
!= NGX_OK)
{
return NULL;
}
return umcf;
}
// 在ngx_stream_block解析完配置后调用,初始化main conf
// 检查每一个upstream{}块
// 查看是否有load balance初始化指针
// 没有指定特别的算法就使用默认的round robin
// 对本upstream{}块做初始化
static char *
ngx_stream_upstream_init_main_conf(ngx_conf_t *cf, void *conf)
{
ngx_stream_upstream_main_conf_t *umcf = conf;
ngx_uint_t i;
ngx_stream_upstream_init_pt init;
ngx_stream_upstream_srv_conf_t **uscfp;
// umcf里存储的是ngx_stream_upstream_srv_conf_t数组
uscfp = umcf->upstreams.elts;
// 检查每一个upstream{}块
// 此时在uscfp里已经存储了多个上游server的信息
// 包括地址、权重、失败次数等
for (i = 0; i < umcf->upstreams.nelts; i++) {
// 查看是否有load balance初始化指针
// 没有指定特别的算法就使用默认的round robin
init = uscfp[i]->peer.init_upstream
? uscfp[i]->peer.init_upstream
: ngx_stream_upstream_init_round_robin;
// 对本upstream{}块做初始化
if (init(cf, uscfp[i]) != NGX_OK) {
return NGX_CONF_ERROR;
}
}
return NGX_CONF_OK;
}