upstream nginx-0.7.31
[nginx.git] / nginx / src / http / modules / ngx_http_upstream_ip_hash_module.c
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 {
96     u_char                                 *p;
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;
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];
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->single) {
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 = (uintptr_t) 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
176                 if (peer->max_fails == 0 || peer->fails < peer->max_fails) {
177                     break;
178                 }
179
180                 if (now - peer->accessed > peer->fail_timeout) {
181                     peer->fails = 0;
182                     break;
183                 }
184             }
185
186             iphp->rrp.tried[n] |= m;
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     iphp->rrp.current = p;
199
200     pc->sockaddr = peer->sockaddr;
201     pc->socklen = peer->socklen;
202     pc->name = &peer->name;
203
204     /* ngx_unlock_mutex(iphp->rrp.peers->mutex); */
205
206     iphp->rrp.tried[n] |= m;
207     iphp->hash = hash;
208
209     return NGX_OK;
210 }
211
212
213 static char *
214 ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
215 {
216     ngx_http_upstream_srv_conf_t  *uscf;
217
218     uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
219
220     uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash;
221
222     uscf->flags = NGX_HTTP_UPSTREAM_CREATE
223                   |NGX_HTTP_UPSTREAM_MAX_FAILS
224                   |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
225                   |NGX_HTTP_UPSTREAM_DOWN;
226
227     return NGX_CONF_OK;
228 }