3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 /* the round robin data must be first */
14 ngx_http_upstream_rr_peer_data_t rrp;
22 ngx_event_get_peer_pt get_rr_peer;
23 } ngx_http_upstream_ip_hash_peer_data_t;
26 static ngx_int_t ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r,
27 ngx_http_upstream_srv_conf_t *us);
28 static ngx_int_t ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc,
30 static char *ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd,
34 static ngx_command_t ngx_http_upstream_ip_hash_commands[] = {
36 { ngx_string("ip_hash"),
37 NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS,
38 ngx_http_upstream_ip_hash,
47 static ngx_http_module_t ngx_http_upstream_ip_hash_module_ctx = {
48 NULL, /* preconfiguration */
49 NULL, /* postconfiguration */
51 NULL, /* create main configuration */
52 NULL, /* init main configuration */
54 NULL, /* create server configuration */
55 NULL, /* merge server configuration */
57 NULL, /* create location configuration */
58 NULL /* merge location configuration */
62 ngx_module_t ngx_http_upstream_ip_hash_module = {
64 &ngx_http_upstream_ip_hash_module_ctx, /* module context */
65 ngx_http_upstream_ip_hash_commands, /* module directives */
66 NGX_HTTP_MODULE, /* module type */
67 NULL, /* init master */
68 NULL, /* init module */
69 NULL, /* init process */
70 NULL, /* init thread */
71 NULL, /* exit thread */
72 NULL, /* exit process */
73 NULL, /* exit master */
79 ngx_http_upstream_init_ip_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
81 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
85 us->peer.init = ngx_http_upstream_init_ip_hash_peer;
92 ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r,
93 ngx_http_upstream_srv_conf_t *us)
96 struct sockaddr_in *sin;
97 ngx_http_upstream_ip_hash_peer_data_t *iphp;
99 iphp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_ip_hash_peer_data_t));
104 r->upstream->peer.data = &iphp->rrp;
106 if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
110 r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer;
114 if (r->connection->sockaddr->sa_family == AF_INET) {
116 sin = (struct sockaddr_in *) r->connection->sockaddr;
117 p = (u_char *) &sin->sin_addr.s_addr;
118 iphp->addr[0] = p[0];
119 iphp->addr[1] = p[1];
120 iphp->addr[2] = p[2];
130 iphp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;
137 ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
139 ngx_http_upstream_ip_hash_peer_data_t *iphp = data;
143 ngx_uint_t i, n, p, hash;
144 ngx_http_upstream_rr_peer_t *peer;
146 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
147 "get ip hash peer, try: %ui", pc->tries);
151 if (iphp->tries > 20 || iphp->rrp.peers->single) {
152 return iphp->get_rr_peer(pc, &iphp->rrp);
158 pc->connection = NULL;
164 for (i = 0; i < 3; i++) {
165 hash = (hash * 113 + iphp->addr[i]) % 6271;
168 p = hash % iphp->rrp.peers->number;
170 n = p / (8 * sizeof(uintptr_t));
171 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
173 if (!(iphp->rrp.tried[n] & m)) {
175 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
176 "get ip hash peer, hash: %ui %04XA", p, m);
178 peer = &iphp->rrp.peers->peer[p];
180 /* ngx_lock_mutex(iphp->rrp.peers->mutex); */
184 if (peer->max_fails == 0 || peer->fails < peer->max_fails) {
188 if (now - peer->accessed > peer->fail_timeout) {
194 iphp->rrp.tried[n] |= m;
196 /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
201 if (++iphp->tries >= 20) {
202 return iphp->get_rr_peer(pc, &iphp->rrp);
206 iphp->rrp.current = p;
208 pc->sockaddr = peer->sockaddr;
209 pc->socklen = peer->socklen;
210 pc->name = &peer->name;
212 /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
214 iphp->rrp.tried[n] |= m;
222 ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
224 ngx_http_upstream_srv_conf_t *uscf;
226 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
228 uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash;
230 uscf->flags = NGX_HTTP_UPSTREAM_CREATE
231 |NGX_HTTP_UPSTREAM_MAX_FAILS
232 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
233 |NGX_HTTP_UPSTREAM_DOWN;