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;
23 ngx_event_get_peer_pt get_rr_peer;
24 } ngx_http_upstream_ip_hash_peer_data_t;
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,
31 static char *ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd,
35 static ngx_command_t ngx_http_upstream_ip_hash_commands[] = {
37 { ngx_string("ip_hash"),
38 NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS,
39 ngx_http_upstream_ip_hash,
48 static ngx_http_module_t ngx_http_upstream_ip_hash_module_ctx = {
49 NULL, /* preconfiguration */
50 NULL, /* postconfiguration */
52 NULL, /* create main configuration */
53 NULL, /* init main configuration */
55 NULL, /* create server configuration */
56 NULL, /* merge server configuration */
58 NULL, /* create location configuration */
59 NULL /* merge location configuration */
63 ngx_module_t ngx_http_upstream_ip_hash_module = {
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 */
80 ngx_http_upstream_init_ip_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
82 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
86 us->peer.init = ngx_http_upstream_init_ip_hash_peer;
93 ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r,
94 ngx_http_upstream_srv_conf_t *us)
97 struct sockaddr_in *sin;
98 ngx_http_upstream_ip_hash_peer_data_t *iphp;
100 iphp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_ip_hash_peer_data_t));
105 r->upstream->peer.data = &iphp->rrp;
107 if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
111 r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer;
114 sin = (struct sockaddr_in *) r->connection->sockaddr;
115 p = (u_char *) &sin->sin_addr.s_addr;
116 iphp->addr[0] = p[0];
117 iphp->addr[1] = p[1];
118 iphp->addr[2] = p[2];
122 iphp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;
129 ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
131 ngx_http_upstream_ip_hash_peer_data_t *iphp = data;
135 ngx_uint_t i, n, p, hash;
136 ngx_http_upstream_rr_peer_t *peer;
138 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
139 "get ip hash peer, try: %ui", pc->tries);
143 if (iphp->tries > 20 || iphp->rrp.peers->single) {
144 return iphp->get_rr_peer(pc, &iphp->rrp);
150 pc->connection = NULL;
156 for (i = 0; i < 3; i++) {
157 hash = (hash * 113 + iphp->addr[i]) % 6271;
160 p = hash % iphp->rrp.peers->number;
162 n = p / (8 * sizeof(uintptr_t));
163 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
165 if (!(iphp->rrp.tried[n] & m)) {
167 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
168 "get ip hash peer, hash: %ui %04XA", p, m);
170 peer = &iphp->rrp.peers->peer[p];
172 /* ngx_lock_mutex(iphp->rrp.peers->mutex); */
176 if (peer->max_fails == 0 || peer->fails < peer->max_fails) {
180 if (now - peer->accessed > peer->fail_timeout) {
186 iphp->rrp.tried[n] |= m;
188 /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
193 if (++iphp->tries >= 20) {
194 return iphp->get_rr_peer(pc, &iphp->rrp);
198 iphp->rrp.current = p;
200 pc->sockaddr = peer->sockaddr;
201 pc->socklen = peer->socklen;
202 pc->name = &peer->name;
204 /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
206 iphp->rrp.tried[n] |= m;
214 ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
216 ngx_http_upstream_srv_conf_t *uscf;
218 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
220 uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash;
222 uscf->flags = NGX_HTTP_UPSTREAM_CREATE
223 |NGX_HTTP_UPSTREAM_MAX_FAILS
224 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
225 |NGX_HTTP_UPSTREAM_DOWN;