Changeset ca37e8
- Timestamp:
- 01/16/09 19:58:58 (3 years ago)
- Branches:
- master-v0.7, upstream_count_limit
- Children:
- cd26d0
- Parents:
- 3f2f24
- git-author:
- George Potapov <nephrite@…> (01/16/09 19:58:58)
- git-committer:
- George Potapov <nephrite@…> (01/16/09 19:58:58)
- Location:
- upstream_keepalive
- Files:
-
- 2 edited
-
ngx_http_upstream_keepalive_module.c (modified) (4 diffs)
-
t/memcached-keepalive.t (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
upstream_keepalive/ngx_http_upstream_keepalive_module.c
r2904d4 rca37e8 26 26 ngx_http_upstream_keepalive_srv_conf_t *conf; 27 27 28 ngx_http_upstream_t *upstream; 29 28 30 void *data; 29 31 30 32 ngx_event_get_peer_pt original_get_peer; 31 33 ngx_event_free_peer_pt original_free_peer; 34 35 ngx_uint_t failed; /* unsigned:1 */ 32 36 33 37 } ngx_http_upstream_keepalive_peer_data_t; … … 170 174 171 175 kp->conf = kcf; 176 kp->upstream = r->upstream; 172 177 kp->data = r->upstream->peer.data; 173 178 kp->original_get_peer = r->upstream->peer.get; … … 194 199 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, 195 200 "get keepalive peer"); 201 202 kp->failed = 0; 196 203 197 204 /* single pool of cached connections */ … … 265 272 ngx_http_upstream_keepalive_cache_t *item; 266 273 267 ngx_queue_t *q; 268 ngx_connection_t *c; 274 ngx_uint_t status; 275 ngx_queue_t *q; 276 ngx_connection_t *c; 277 ngx_http_upstream_t *u; 269 278 270 279 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, 271 280 "free keepalive peer"); 272 281 273 if (!(state & NGX_PEER_FAILED) 274 && pc->connection != NULL) 282 /* remember failed state - peer.free() may be called more than once */ 283 284 if (state & NGX_PEER_FAILED) { 285 kp->failed = 1; 286 } 287 288 /* 289 * cache valid connections 290 * 291 * For memcached this means status either 404 or 200. For status 200 we 292 * should also check if all response body was read (u->length == 0) and 293 * make sure that u->length is valid (we use u->header_sent flag to test 294 * this). Memcached is the only supported protocol for now. 295 * 296 * Some notes on other possibilities (incomplete): 297 * 298 * fastcgi: u->pipe->upstream_done should be sufficient 299 * 300 * proxy buffered: u->pipe->upstream_done, 304 replies, replies to head 301 * requests (see RFC 2616, 4.4 Message Length) 302 * 303 * proxy unbuffered: 200 as for memcached (with u->length == 0 and 304 * header_sent), 304, replies to head requests 305 * 306 * subrequest_in_memory: won't work as of now 307 * 308 * TODO: move this logic to protocol modules (NGX_PEER_KEEPALIVE?) 309 */ 310 311 u = kp->upstream; 312 status = u->headers_in.status_n; 313 314 if (!kp->failed 315 && pc->connection != NULL 316 && (status == NGX_HTTP_NOT_FOUND 317 || (status == NGX_HTTP_OK && u->header_sent && u->length == 0))) 275 318 { 276 319 c = pc->connection; 320 321 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, 322 "free keepalive peer: saving connection %p", c); 277 323 278 324 if (ngx_queue_empty(&kp->conf->free)) { -
upstream_keepalive/t/memcached-keepalive.t
r2904d4 rca37e8 21 21 plain(skip_all => 'Cache::Memcached not installed') if $@; 22 22 23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(1 0)23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(16) 24 24 ->write_file_expand('nginx.conf', <<'EOF'); 25 25 … … 95 95 EOF 96 96 97 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081'); 98 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8082'); 97 if (`memcached -h` =~ /repcached/) { 98 # repcached patches adds additional listen socket memcached 99 # that should be different too 100 101 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081', 102 '-X', '8091'); 103 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8082', 104 '-X', '8092'); 105 106 } else { 107 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081'); 108 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8082'); 109 } 110 99 111 $t->run(); 112 113 $t->waitforsocket('127.0.0.1:8081') 114 or die "Unable to start memcached"; 115 $t->waitforsocket('127.0.0.1:8082') 116 or die "Unable to start second memcached"; 100 117 101 118 ############################################################################### … … 106 123 $memd1->set('/', 'SEE-THIS'); 107 124 $memd2->set('/', 'SEE-THIS'); 125 $memd1->set('/big', 'X' x 1000000); 108 126 109 127 my $total = $memd1->stats()->{total}->{total_connections}; … … 120 138 'only one connection used'); 121 139 140 # Since nginx doesn't read all data from connection in some situations (head 141 # requests, post_action, errors writing to client) we have to close such 142 # connections. Check if we really do close them. 143 144 $total = $memd1->stats()->{total}->{total_connections}; 145 146 unlike(http_head('/'), qr/SEE-THIS/, 'head request'); 147 like(http_get('/'), qr/SEE-THIS/, 'get after head'); 148 149 is($memd1->stats()->{total}->{total_connections}, $total + 1, 150 'head request closes connection'); 151 152 $total = $memd1->stats()->{total}->{total_connections}; 153 154 unlike(http_head('/big'), qr/XXX/, 'big head'); 155 like(http_get('/'), qr/SEE-THIS/, 'get after big head'); 156 157 is($memd1->stats()->{total}->{total_connections}, $total + 1, 158 'big head request closes connection'); 159 122 160 # two backends with 'single' option - should establish only one connection 123 161
Note: See TracChangeset
for help on using the changeset viewer.
