forked from alibaba/nginx-tfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_connection_pool.c
320 lines (236 loc) · 7.84 KB
/
ngx_http_connection_pool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright (C) 2010-2012 Alibaba Group Holding Limited
*/
#include <ngx_http_connection_pool.h>
static void ngx_http_connection_pool_close(ngx_connection_t *c);
static void ngx_http_connection_pool_close_handler(ngx_event_t *ev);
static void ngx_http_connection_pool_dummy_handler(ngx_event_t *ev);
static ngx_int_t ngx_http_connection_pool_get(ngx_peer_connection_t *pc,
void *data);
static void ngx_http_connection_pool_free(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state);
ngx_http_connection_pool_t *
ngx_http_connection_pool_init(ngx_pool_t *pool, ngx_uint_t max_cached,
ngx_uint_t bucket_count)
{
ngx_uint_t j, k;
ngx_http_connection_pool_t *conn_pool;
ngx_http_connection_pool_elt_t *cached;
conn_pool = ngx_pcalloc(pool, sizeof(ngx_http_connection_pool_t));
if (conn_pool == NULL) {
return NULL;
}
conn_pool->bucket_count = bucket_count;
conn_pool->max_cached = max_cached;
conn_pool->cache = ngx_pcalloc(pool, sizeof(ngx_queue_t) * bucket_count);
if (conn_pool->cache == NULL) {
return NULL;
}
conn_pool->free = ngx_pcalloc(pool, sizeof(ngx_queue_t) * bucket_count);
if (conn_pool->free == NULL) {
return NULL;
}
for (j = 0; j < bucket_count; j++) {
ngx_queue_init(&conn_pool->cache[j]);
ngx_queue_init(&conn_pool->free[j]);
cached = ngx_pcalloc(pool, sizeof(ngx_http_connection_pool_elt_t) * max_cached);
if (cached == NULL) {
return NULL;
}
for (k = 0; k < max_cached; k++) {
ngx_queue_insert_head(&conn_pool->free[j], &cached[k].queue);
}
}
conn_pool->get_peer = ngx_http_connection_pool_get;
conn_pool->free_peer = ngx_http_connection_pool_free;
return conn_pool;
}
ngx_int_t
ngx_http_connection_pool_get(ngx_peer_connection_t *pc, void *data)
{
u_char pc_addr[32] = {'\0'};
ngx_uint_t bucket_id, hash;
ngx_queue_t *q, *cache, *free;
ngx_connection_t *c;
ngx_http_connection_pool_t *p;
ngx_http_connection_pool_elt_t *item;
p = data;
#if (NGX_DEBUG)
p->count--;
#endif
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"get keepalive peer");
p->failed = 0;
hash = ngx_murmur_hash2((u_char *) pc->sockaddr, pc->socklen);
bucket_id = hash % p->bucket_count;
cache = &p->cache[bucket_id];
free = &p->free[bucket_id];
ngx_sprintf(pc_addr, "%s:%d",
inet_ntoa(((struct sockaddr_in*)(pc->sockaddr))->sin_addr),
ntohs(((struct sockaddr_in*)(pc->sockaddr))->sin_port));
for (q = ngx_queue_head(cache);
q != ngx_queue_sentinel(cache);
q = ngx_queue_next(q))
{
item = ngx_queue_data(q, ngx_http_connection_pool_elt_t, queue);
c = item->connection;
if (ngx_memn2cmp((u_char *) &item->sockaddr, (u_char *) pc->sockaddr,
item->socklen, pc->socklen)
== 0)
{
ngx_queue_remove(q);
ngx_queue_insert_head(free, q);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"get keepalive peer: using connection %p", c);
c->idle = 0;
c->log = pc->log;
c->read->log = pc->log;
c->write->log = pc->log;
c->pool->log = pc->log;
pc->connection = c;
pc->cached = 1;
item->free = free;
return NGX_DONE;
}
}
return NGX_OK;
}
void
ngx_http_connection_pool_free(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state)
{
ngx_http_connection_pool_t *p = data;
ngx_http_connection_pool_elt_t *item;
ngx_uint_t hash, bucket_id;
ngx_queue_t *q, *cache, *free;
ngx_connection_t *c;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"free keepalive peer");
/* remember failed state - peer.free() may be called more than once */
if (state & NGX_PEER_FAILED) {
p->failed = 1;
}
/* cache valid connections */
c = pc->connection;
if (p->failed
|| c == NULL
|| c->read->eof
|| c->read->error
|| c->read->timedout
|| c->write->error
|| c->write->timedout)
{
return;
}
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
return;
}
#if (NGX_DEBUG)
p->count++;
#endif
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"free keepalive peer: saving connection %p", c);
hash = ngx_murmur_hash2((u_char *) pc->sockaddr, pc->socklen);
bucket_id = hash % p->bucket_count;
cache = &p->cache[bucket_id];
free = &p->free[bucket_id];
if (ngx_queue_empty(free)) {
q = ngx_queue_last(cache);
ngx_queue_remove(q);
item = ngx_queue_data(q, ngx_http_connection_pool_elt_t, queue);
ngx_http_connection_pool_close(item->connection);
} else {
q = ngx_queue_head(free);
ngx_queue_remove(q);
item = ngx_queue_data(q, ngx_http_connection_pool_elt_t, queue);
}
item->connection = c;
item->free = free;
ngx_queue_insert_head(cache, q);
pc->connection = NULL;
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
c->write->handler = ngx_http_connection_pool_dummy_handler;
c->read->handler = ngx_http_connection_pool_close_handler;
c->data = item;
c->idle = 1;
c->log = ngx_cycle->log;
c->read->log = ngx_cycle->log;
c->write->log = ngx_cycle->log;
c->pool->log = ngx_cycle->log;
item->socklen = pc->socklen;
ngx_memcpy(&item->sockaddr, pc->sockaddr, pc->socklen);
if (c->read->ready) {
ngx_http_connection_pool_close_handler(c->read);
}
}
static void
ngx_http_connection_pool_close_handler(ngx_event_t *ev)
{
ngx_http_connection_pool_elt_t *item;
int n;
char buf[1];
ngx_connection_t *c;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
"keepalive close handler");
c = ev->data;
if (c->close) {
goto close;
}
n = recv(c->fd, buf, 1, MSG_PEEK);
if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
/* stale event */
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
goto close;
}
return;
}
close:
item = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
"connection pool close connection");
ngx_http_connection_pool_close(c);
ngx_queue_remove(&item->queue);
ngx_queue_insert_head(item->free, &item->queue);
}
static void
ngx_http_connection_pool_close(ngx_connection_t *c)
{
#if (NGX_HTTP_SSL)
if (c->ssl) {
c->ssl->no_wait_shutdown = 1;
c->ssl->no_send_shutdown = 1;
if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
c->ssl->handler = ngx_http_connection_pool_close;
return;
}
}
#endif
ngx_destroy_pool(c->pool);
ngx_close_connection(c);
}
static void
ngx_http_connection_pool_dummy_handler(ngx_event_t *ev)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
"keepalive dummy handler");
}
#if (NGX_DEBUG)
void
ngx_http_connection_pool_check(ngx_http_connection_pool_t *conn_pool,
ngx_log_t *log)
{
if (conn_pool->count != 0) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"<== conn pool check ==> some keepalive peer do not free!, conn_pool count: %i",
conn_pool->count);
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0,
"<== conn pool check ==> all keepalive peers are free");
}
}
#endif