Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_upstream_random_module.c @ 8182:3bff3f397c05
SSL: ssl_conf_command directive.
With the ssl_conf_command directive it is now possible to set
arbitrary OpenSSL configuration parameters as long as nginx is compiled
with OpenSSL 1.0.2 or later. Full list of available configuration
commands can be found in the SSL_CONF_cmd manual page
(https://www.openssl.org/docs/man1.1.1/man3/SSL_CONF_cmd.html).
In particular, this allows configuring PrioritizeChaCha option
(ticket #1445):
ssl_conf_command Options PrioritizeChaCha;
It can be also used to configure TLSv1.3 ciphers in OpenSSL,
which fails to configure them via the SSL_CTX_set_cipher_list()
interface (ticket #1529):
ssl_conf_command Ciphersuites TLS_CHACHA20_POLY1305_SHA256;
Configuration commands are applied after nginx own configuration
for SSL, so they can be used to override anything set by nginx.
Note though that configuring OpenSSL directly with ssl_conf_command
might result in a behaviour nginx does not expect, and should be
done with care.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 22 Oct 2020 18:00:22 +0300 |
parents | f2396ecf608b |
children |
rev | line source |
---|---|
7301
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
1 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
2 /* |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
3 * Copyright (C) Nginx, Inc. |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
4 */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
5 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
6 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
9 #include <ngx_http.h> |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
10 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
11 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
12 typedef struct { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
13 ngx_http_upstream_rr_peer_t *peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
14 ngx_uint_t range; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
15 } ngx_http_upstream_random_range_t; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
16 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
17 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
18 typedef struct { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
19 ngx_uint_t two; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
20 ngx_http_upstream_random_range_t *ranges; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
21 } ngx_http_upstream_random_srv_conf_t; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
22 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
23 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
24 typedef struct { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
25 /* the round robin data must be first */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
26 ngx_http_upstream_rr_peer_data_t rrp; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
27 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
28 ngx_http_upstream_random_srv_conf_t *conf; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
29 u_char tries; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
30 } ngx_http_upstream_random_peer_data_t; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
31 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
32 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
33 static ngx_int_t ngx_http_upstream_init_random(ngx_conf_t *cf, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
34 ngx_http_upstream_srv_conf_t *us); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
35 static ngx_int_t ngx_http_upstream_update_random(ngx_pool_t *pool, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
36 ngx_http_upstream_srv_conf_t *us); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
37 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
38 static ngx_int_t ngx_http_upstream_init_random_peer(ngx_http_request_t *r, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
39 ngx_http_upstream_srv_conf_t *us); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
40 static ngx_int_t ngx_http_upstream_get_random_peer(ngx_peer_connection_t *pc, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
41 void *data); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
42 static ngx_int_t ngx_http_upstream_get_random2_peer(ngx_peer_connection_t *pc, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
43 void *data); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
44 static ngx_uint_t ngx_http_upstream_peek_random_peer( |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
45 ngx_http_upstream_rr_peers_t *peers, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
46 ngx_http_upstream_random_peer_data_t *rp); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
47 static void *ngx_http_upstream_random_create_conf(ngx_conf_t *cf); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
48 static char *ngx_http_upstream_random(ngx_conf_t *cf, ngx_command_t *cmd, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
49 void *conf); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
50 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
51 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
52 static ngx_command_t ngx_http_upstream_random_commands[] = { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
53 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
54 { ngx_string("random"), |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
55 NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE12, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
56 ngx_http_upstream_random, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
57 NGX_HTTP_SRV_CONF_OFFSET, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
58 0, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
59 NULL }, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
60 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
61 ngx_null_command |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
62 }; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
63 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
64 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
65 static ngx_http_module_t ngx_http_upstream_random_module_ctx = { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
66 NULL, /* preconfiguration */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
67 NULL, /* postconfiguration */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
68 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
69 NULL, /* create main configuration */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
70 NULL, /* init main configuration */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
71 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
72 ngx_http_upstream_random_create_conf, /* create server configuration */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
73 NULL, /* merge server configuration */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
74 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
75 NULL, /* create location configuration */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
76 NULL /* merge location configuration */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
77 }; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
78 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
79 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
80 ngx_module_t ngx_http_upstream_random_module = { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
81 NGX_MODULE_V1, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
82 &ngx_http_upstream_random_module_ctx, /* module context */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
83 ngx_http_upstream_random_commands, /* module directives */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
84 NGX_HTTP_MODULE, /* module type */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
85 NULL, /* init master */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
86 NULL, /* init module */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
87 NULL, /* init process */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
88 NULL, /* init thread */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
89 NULL, /* exit thread */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
90 NULL, /* exit process */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
91 NULL, /* exit master */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
92 NGX_MODULE_V1_PADDING |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
93 }; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
94 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
95 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
96 static ngx_int_t |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
97 ngx_http_upstream_init_random(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
98 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
99 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "init random"); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
100 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
101 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
102 return NGX_ERROR; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
103 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
104 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
105 us->peer.init = ngx_http_upstream_init_random_peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
106 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
107 #if (NGX_HTTP_UPSTREAM_ZONE) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
108 if (us->shm_zone) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
109 return NGX_OK; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
110 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
111 #endif |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
112 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
113 return ngx_http_upstream_update_random(cf->pool, us); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
114 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
115 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
116 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
117 static ngx_int_t |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
118 ngx_http_upstream_update_random(ngx_pool_t *pool, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
119 ngx_http_upstream_srv_conf_t *us) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
120 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
121 size_t size; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
122 ngx_uint_t i, total_weight; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
123 ngx_http_upstream_rr_peer_t *peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
124 ngx_http_upstream_rr_peers_t *peers; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
125 ngx_http_upstream_random_range_t *ranges; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
126 ngx_http_upstream_random_srv_conf_t *rcf; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
127 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
128 rcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_random_module); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
129 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
130 peers = us->peer.data; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
131 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
132 size = peers->number * sizeof(ngx_http_upstream_random_range_t); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
133 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
134 ranges = pool ? ngx_palloc(pool, size) : ngx_alloc(size, ngx_cycle->log); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
135 if (ranges == NULL) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
136 return NGX_ERROR; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
137 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
138 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
139 total_weight = 0; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
140 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
141 for (peer = peers->peer, i = 0; peer; peer = peer->next, i++) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
142 ranges[i].peer = peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
143 ranges[i].range = total_weight; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
144 total_weight += peer->weight; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
145 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
146 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
147 rcf->ranges = ranges; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
148 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
149 return NGX_OK; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
150 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
151 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
152 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
153 static ngx_int_t |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
154 ngx_http_upstream_init_random_peer(ngx_http_request_t *r, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
155 ngx_http_upstream_srv_conf_t *us) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
156 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
157 ngx_http_upstream_random_srv_conf_t *rcf; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
158 ngx_http_upstream_random_peer_data_t *rp; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
159 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
160 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
161 "init random peer"); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
162 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
163 rcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_random_module); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
164 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
165 rp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_random_peer_data_t)); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
166 if (rp == NULL) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
167 return NGX_ERROR; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
168 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
169 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
170 r->upstream->peer.data = &rp->rrp; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
171 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
172 if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
173 return NGX_ERROR; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
174 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
175 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
176 if (rcf->two) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
177 r->upstream->peer.get = ngx_http_upstream_get_random2_peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
178 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
179 } else { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
180 r->upstream->peer.get = ngx_http_upstream_get_random_peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
181 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
182 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
183 rp->conf = rcf; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
184 rp->tries = 0; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
185 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
186 ngx_http_upstream_rr_peers_rlock(rp->rrp.peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
187 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
188 #if (NGX_HTTP_UPSTREAM_ZONE) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
189 if (rp->rrp.peers->shpool && rcf->ranges == NULL) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
190 if (ngx_http_upstream_update_random(NULL, us) != NGX_OK) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
191 ngx_http_upstream_rr_peers_unlock(rp->rrp.peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
192 return NGX_ERROR; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
193 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
194 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
195 #endif |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
196 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
197 ngx_http_upstream_rr_peers_unlock(rp->rrp.peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
198 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
199 return NGX_OK; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
200 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
201 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
202 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
203 static ngx_int_t |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
204 ngx_http_upstream_get_random_peer(ngx_peer_connection_t *pc, void *data) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
205 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
206 ngx_http_upstream_random_peer_data_t *rp = data; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
207 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
208 time_t now; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
209 uintptr_t m; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
210 ngx_uint_t i, n; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
211 ngx_http_upstream_rr_peer_t *peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
212 ngx_http_upstream_rr_peers_t *peers; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
213 ngx_http_upstream_rr_peer_data_t *rrp; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
214 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
215 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
216 "get random peer, try: %ui", pc->tries); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
217 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
218 rrp = &rp->rrp; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
219 peers = rrp->peers; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
220 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
221 ngx_http_upstream_rr_peers_rlock(peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
222 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
223 if (rp->tries > 20 || peers->single) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
224 ngx_http_upstream_rr_peers_unlock(peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
225 return ngx_http_upstream_get_round_robin_peer(pc, rrp); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
226 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
227 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
228 pc->cached = 0; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
229 pc->connection = NULL; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
230 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
231 now = ngx_time(); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
232 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
233 for ( ;; ) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
234 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
235 i = ngx_http_upstream_peek_random_peer(peers, rp); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
236 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
237 peer = rp->conf->ranges[i].peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
238 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
239 n = i / (8 * sizeof(uintptr_t)); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
240 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
241 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
242 if (rrp->tried[n] & m) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
243 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
244 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
245 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
246 ngx_http_upstream_rr_peer_lock(peers, peer); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
247 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
248 if (peer->down) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
249 ngx_http_upstream_rr_peer_unlock(peers, peer); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
250 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
251 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
252 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
253 if (peer->max_fails |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
254 && peer->fails >= peer->max_fails |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
255 && now - peer->checked <= peer->fail_timeout) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
256 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
257 ngx_http_upstream_rr_peer_unlock(peers, peer); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
258 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
259 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
260 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
261 if (peer->max_conns && peer->conns >= peer->max_conns) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
262 ngx_http_upstream_rr_peer_unlock(peers, peer); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
263 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
264 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
265 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
266 break; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
267 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
268 next: |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
269 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
270 if (++rp->tries > 20) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
271 ngx_http_upstream_rr_peers_unlock(peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
272 return ngx_http_upstream_get_round_robin_peer(pc, rrp); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
273 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
274 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
275 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
276 rrp->current = peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
277 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
278 if (now - peer->checked > peer->fail_timeout) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
279 peer->checked = now; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
280 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
281 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
282 pc->sockaddr = peer->sockaddr; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
283 pc->socklen = peer->socklen; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
284 pc->name = &peer->name; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
285 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
286 peer->conns++; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
287 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
288 ngx_http_upstream_rr_peer_unlock(peers, peer); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
289 ngx_http_upstream_rr_peers_unlock(peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
290 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
291 rrp->tried[n] |= m; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
292 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
293 return NGX_OK; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
294 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
295 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
296 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
297 static ngx_int_t |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
298 ngx_http_upstream_get_random2_peer(ngx_peer_connection_t *pc, void *data) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
299 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
300 ngx_http_upstream_random_peer_data_t *rp = data; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
301 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
302 time_t now; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
303 uintptr_t m; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
304 ngx_uint_t i, n, p; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
305 ngx_http_upstream_rr_peer_t *peer, *prev; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
306 ngx_http_upstream_rr_peers_t *peers; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
307 ngx_http_upstream_rr_peer_data_t *rrp; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
308 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
309 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
310 "get random2 peer, try: %ui", pc->tries); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
311 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
312 rrp = &rp->rrp; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
313 peers = rrp->peers; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
314 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
315 ngx_http_upstream_rr_peers_wlock(peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
316 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
317 if (rp->tries > 20 || peers->single) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
318 ngx_http_upstream_rr_peers_unlock(peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
319 return ngx_http_upstream_get_round_robin_peer(pc, rrp); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
320 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
321 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
322 pc->cached = 0; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
323 pc->connection = NULL; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
324 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
325 now = ngx_time(); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
326 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
327 prev = NULL; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
328 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
329 #if (NGX_SUPPRESS_WARN) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
330 p = 0; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
331 #endif |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
332 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
333 for ( ;; ) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
334 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
335 i = ngx_http_upstream_peek_random_peer(peers, rp); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
336 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
337 peer = rp->conf->ranges[i].peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
338 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
339 if (peer == prev) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
340 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
341 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
342 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
343 n = i / (8 * sizeof(uintptr_t)); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
344 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
345 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
346 if (rrp->tried[n] & m) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
347 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
348 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
349 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
350 if (peer->down) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
351 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
352 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
353 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
354 if (peer->max_fails |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
355 && peer->fails >= peer->max_fails |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
356 && now - peer->checked <= peer->fail_timeout) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
357 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
358 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
359 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
360 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
361 if (peer->max_conns && peer->conns >= peer->max_conns) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
362 goto next; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
363 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
364 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
365 if (prev) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
366 if (peer->conns * prev->weight > prev->conns * peer->weight) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
367 peer = prev; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
368 n = p / (8 * sizeof(uintptr_t)); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
369 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
370 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
371 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
372 break; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
373 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
374 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
375 prev = peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
376 p = i; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
377 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
378 next: |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
379 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
380 if (++rp->tries > 20) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
381 ngx_http_upstream_rr_peers_unlock(peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
382 return ngx_http_upstream_get_round_robin_peer(pc, rrp); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
383 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
384 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
385 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
386 rrp->current = peer; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
387 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
388 if (now - peer->checked > peer->fail_timeout) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
389 peer->checked = now; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
390 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
391 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
392 pc->sockaddr = peer->sockaddr; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
393 pc->socklen = peer->socklen; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
394 pc->name = &peer->name; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
395 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
396 peer->conns++; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
397 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
398 ngx_http_upstream_rr_peers_unlock(peers); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
399 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
400 rrp->tried[n] |= m; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
401 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
402 return NGX_OK; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
403 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
404 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
405 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
406 static ngx_uint_t |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
407 ngx_http_upstream_peek_random_peer(ngx_http_upstream_rr_peers_t *peers, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
408 ngx_http_upstream_random_peer_data_t *rp) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
409 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
410 ngx_uint_t i, j, k, x; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
411 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
412 x = ngx_random() % peers->total_weight; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
413 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
414 i = 0; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
415 j = peers->number; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
416 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
417 while (j - i > 1) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
418 k = (i + j) / 2; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
419 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
420 if (x < rp->conf->ranges[k].range) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
421 j = k; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
422 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
423 } else { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
424 i = k; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
425 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
426 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
427 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
428 return i; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
429 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
430 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
431 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
432 static void * |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
433 ngx_http_upstream_random_create_conf(ngx_conf_t *cf) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
434 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
435 ngx_http_upstream_random_srv_conf_t *conf; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
436 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
437 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_random_srv_conf_t)); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
438 if (conf == NULL) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
439 return NULL; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
440 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
441 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
442 /* |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
443 * set by ngx_pcalloc(): |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
444 * |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
445 * conf->two = 0; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
446 */ |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
447 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
448 return conf; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
449 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
450 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
451 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
452 static char * |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
453 ngx_http_upstream_random(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
454 { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
455 ngx_http_upstream_random_srv_conf_t *rcf = conf; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
456 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
457 ngx_str_t *value; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
458 ngx_http_upstream_srv_conf_t *uscf; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
459 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
460 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
461 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
462 if (uscf->peer.init_upstream) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
463 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
464 "load balancing method redefined"); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
465 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
466 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
467 uscf->peer.init_upstream = ngx_http_upstream_init_random; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
468 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
469 uscf->flags = NGX_HTTP_UPSTREAM_CREATE |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
470 |NGX_HTTP_UPSTREAM_WEIGHT |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
471 |NGX_HTTP_UPSTREAM_MAX_CONNS |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
472 |NGX_HTTP_UPSTREAM_MAX_FAILS |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
473 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
474 |NGX_HTTP_UPSTREAM_DOWN; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
475 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
476 if (cf->args->nelts == 1) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
477 return NGX_CONF_OK; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
478 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
479 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
480 value = cf->args->elts; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
481 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
482 if (ngx_strcmp(value[1].data, "two") == 0) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
483 rcf->two = 1; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
484 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
485 } else { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
486 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
487 "invalid parameter \"%V\"", &value[1]); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
488 return NGX_CONF_ERROR; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
489 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
490 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
491 if (cf->args->nelts == 2) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
492 return NGX_CONF_OK; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
493 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
494 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
495 if (ngx_strcmp(value[2].data, "least_conn") != 0) { |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
496 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
497 "invalid parameter \"%V\"", &value[2]); |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
498 return NGX_CONF_ERROR; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
499 } |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
500 |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
501 return NGX_CONF_OK; |
f2396ecf608b
Upstream: ngx_http_upstream_random module.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
502 } |