Mercurial > hg > nginx
annotate src/http/modules/ngx_http_upstream_ip_hash_module.c @ 1312:13e649b813d6 release-0.6.4
nginx-0.6.4-RELEASE
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 17 Jul 2007 09:57:37 +0000 |
parents | be2b895d31e0 |
children | ad2311c943a3 |
rev | line source |
---|---|
884 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 typedef struct { | |
13 /* the round robin data must be first */ | |
14 ngx_http_upstream_rr_peer_data_t rrp; | |
15 | |
16 ngx_uint_t hash; | |
17 | |
18 /* AF_INET only */ | |
19 u_char addr[3]; | |
20 | |
21 u_char tries; | |
22 | |
23 ngx_event_get_peer_pt get_rr_peer; | |
24 } ngx_http_upstream_ip_hash_peer_data_t; | |
25 | |
26 | |
27 static ngx_int_t ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r, | |
28 ngx_http_upstream_srv_conf_t *us); | |
29 static ngx_int_t ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, | |
30 void *data); | |
31 static char *ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, | |
32 void *conf); | |
33 | |
34 | |
35 static ngx_command_t ngx_http_upstream_ip_hash_commands[] = { | |
36 | |
37 { ngx_string("ip_hash"), | |
38 NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS, | |
39 ngx_http_upstream_ip_hash, | |
40 0, | |
41 0, | |
42 NULL }, | |
43 | |
44 ngx_null_command | |
45 }; | |
46 | |
47 | |
48 static ngx_http_module_t ngx_http_upstream_ip_hash_module_ctx = { | |
49 NULL, /* preconfiguration */ | |
50 NULL, /* postconfiguration */ | |
51 | |
52 NULL, /* create main configuration */ | |
53 NULL, /* init main configuration */ | |
54 | |
55 NULL, /* create server configuration */ | |
56 NULL, /* merge server configuration */ | |
57 | |
58 NULL, /* create location configuration */ | |
59 NULL /* merge location configuration */ | |
60 }; | |
61 | |
62 | |
63 ngx_module_t ngx_http_upstream_ip_hash_module = { | |
64 NGX_MODULE_V1, | |
65 &ngx_http_upstream_ip_hash_module_ctx, /* module context */ | |
66 ngx_http_upstream_ip_hash_commands, /* module directives */ | |
67 NGX_HTTP_MODULE, /* module type */ | |
68 NULL, /* init master */ | |
69 NULL, /* init module */ | |
70 NULL, /* init process */ | |
71 NULL, /* init thread */ | |
72 NULL, /* exit thread */ | |
73 NULL, /* exit process */ | |
74 NULL, /* exit master */ | |
75 NGX_MODULE_V1_PADDING | |
76 }; | |
77 | |
78 | |
79 ngx_int_t | |
80 ngx_http_upstream_init_ip_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us) | |
81 { | |
82 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) { | |
83 return NGX_ERROR; | |
84 } | |
85 | |
86 us->peer.init = ngx_http_upstream_init_ip_hash_peer; | |
87 | |
88 return NGX_OK; | |
89 } | |
90 | |
91 | |
92 static ngx_int_t | |
93 ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r, | |
94 ngx_http_upstream_srv_conf_t *us) | |
95 { | |
1144
324a5a3fe27f
ip_hash used wrong byte order
Igor Sysoev <igor@sysoev.ru>
parents:
1129
diff
changeset
|
96 u_char *p; |
884 | 97 struct sockaddr_in *sin; |
98 ngx_http_upstream_ip_hash_peer_data_t *iphp; | |
99 | |
100 iphp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_ip_hash_peer_data_t)); | |
101 if (iphp == NULL) { | |
102 return NGX_ERROR; | |
103 } | |
104 | |
105 r->upstream->peer.data = &iphp->rrp; | |
106 | |
107 if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) { | |
108 return NGX_ERROR; | |
109 } | |
110 | |
111 r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer; | |
112 | |
113 /* AF_INET only */ | |
114 sin = (struct sockaddr_in *) r->connection->sockaddr; | |
1144
324a5a3fe27f
ip_hash used wrong byte order
Igor Sysoev <igor@sysoev.ru>
parents:
1129
diff
changeset
|
115 p = (u_char *) &sin->sin_addr.s_addr; |
324a5a3fe27f
ip_hash used wrong byte order
Igor Sysoev <igor@sysoev.ru>
parents:
1129
diff
changeset
|
116 iphp->addr[0] = p[0]; |
324a5a3fe27f
ip_hash used wrong byte order
Igor Sysoev <igor@sysoev.ru>
parents:
1129
diff
changeset
|
117 iphp->addr[1] = p[1]; |
324a5a3fe27f
ip_hash used wrong byte order
Igor Sysoev <igor@sysoev.ru>
parents:
1129
diff
changeset
|
118 iphp->addr[2] = p[2]; |
884 | 119 |
120 iphp->hash = 89; | |
121 iphp->tries = 0; | |
122 iphp->get_rr_peer = ngx_http_upstream_get_round_robin_peer; | |
123 | |
124 return NGX_OK; | |
125 } | |
126 | |
127 | |
128 static ngx_int_t | |
129 ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data) | |
130 { | |
131 ngx_http_upstream_ip_hash_peer_data_t *iphp = data; | |
132 | |
133 time_t now; | |
134 uintptr_t m; | |
135 ngx_uint_t i, n, p, hash; | |
136 ngx_http_upstream_rr_peer_t *peer; | |
137 | |
138 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
139 "get ip hash peer, try: %ui", pc->tries); | |
140 | |
141 /* TODO: cached */ | |
142 | |
143 if (iphp->tries > 20 || iphp->rrp.peers->number == 1) { | |
144 return iphp->get_rr_peer(pc, &iphp->rrp); | |
145 } | |
146 | |
147 now = ngx_time(); | |
148 | |
149 pc->cached = 0; | |
150 pc->connection = NULL; | |
151 | |
152 hash = iphp->hash; | |
153 | |
154 for ( ;; ) { | |
155 | |
156 for (i = 0; i < 3; i++) { | |
157 hash = (hash * 113 + iphp->addr[i]) % 6271; | |
158 } | |
159 | |
160 p = hash % iphp->rrp.peers->number; | |
161 | |
162 n = p / (8 * sizeof(uintptr_t)); | |
163 m = 1 << p % (8 * sizeof(uintptr_t)); | |
164 | |
165 if (!(iphp->rrp.tried[n] & m)) { | |
166 | |
167 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
168 "get ip hash peer, hash: %ui %04XA", p, m); | |
169 | |
170 peer = &iphp->rrp.peers->peer[p]; | |
171 | |
172 /* ngx_lock_mutex(iphp->rrp.peers->mutex); */ | |
173 | |
174 if (!peer->down) { | |
175 | |
1025 | 176 if (peer->max_fails == 0 || peer->fails < peer->max_fails) { |
177 break; | |
178 } | |
884 | 179 |
1025 | 180 if (now - peer->accessed > peer->fail_timeout) { |
181 peer->fails = 0; | |
182 break; | |
183 } | |
1129 | 184 } |
884 | 185 |
1129 | 186 iphp->rrp.tried[n] |= m; |
884 | 187 |
188 /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */ | |
189 | |
190 pc->tries--; | |
191 } | |
192 | |
193 if (++iphp->tries >= 20) { | |
194 return iphp->get_rr_peer(pc, &iphp->rrp); | |
195 } | |
196 } | |
197 | |
198 pc->sockaddr = peer->sockaddr; | |
199 pc->socklen = peer->socklen; | |
200 pc->name = &peer->name; | |
201 | |
202 /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */ | |
203 | |
204 iphp->rrp.tried[n] |= m; | |
205 iphp->hash = hash; | |
206 | |
207 return NGX_OK; | |
208 } | |
209 | |
210 | |
211 static char * | |
212 ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
213 { | |
214 ngx_http_upstream_srv_conf_t *uscf; | |
215 | |
216 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module); | |
217 | |
218 uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash; | |
219 | |
220 uscf->flags = NGX_HTTP_UPSTREAM_CREATE | |
221 |NGX_HTTP_UPSTREAM_MAX_FAILS | |
222 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT | |
223 |NGX_HTTP_UPSTREAM_DOWN; | |
224 | |
225 return NGX_CONF_OK; | |
226 } |