comparison t/memcached-keepalive.t @ 11:15530a464dba

Keepalive: don't cache invalid connections. 1. Remember failed status, since peer.free() may be called more than once. It's called twice if peer fails - once from ngx_http_upstream_next() with NGX_PEER_FAILED set, and then from ngx_http_upstream_finalize_request() without. 2. We shouldn't cache connection unless we aren't expecting anything from upstream. For memcached this means either 404 response or 200 response with all body read (body may not be read e.g. when serving HEAD request).
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 13 Nov 2008 19:36:23 +0300
parents bef88ba0b378
children 067ddc059ee0
comparison
equal deleted inserted replaced
10:06bd0e50e696 11:15530a464dba
18 select STDOUT; $| = 1; 18 select STDOUT; $| = 1;
19 19
20 eval { require Cache::Memcached; }; 20 eval { require Cache::Memcached; };
21 plain(skip_all => 'Cache::Memcached not installed') if $@; 21 plain(skip_all => 'Cache::Memcached not installed') if $@;
22 22
23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(10) 23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(16)
24 ->write_file_expand('nginx.conf', <<'EOF'); 24 ->write_file_expand('nginx.conf', <<'EOF');
25 25
26 master_process off; 26 master_process off;
27 daemon off; 27 daemon off;
28 28
103 my $memd1 = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]); 103 my $memd1 = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]);
104 my $memd2 = Cache::Memcached->new(servers => [ '127.0.0.1:8082' ]); 104 my $memd2 = Cache::Memcached->new(servers => [ '127.0.0.1:8082' ]);
105 105
106 $memd1->set('/', 'SEE-THIS'); 106 $memd1->set('/', 'SEE-THIS');
107 $memd2->set('/', 'SEE-THIS'); 107 $memd2->set('/', 'SEE-THIS');
108 $memd1->set('/big', 'X' x 1000000);
108 109
109 my $total = $memd1->stats()->{total}->{total_connections}; 110 my $total = $memd1->stats()->{total}->{total_connections};
110 111
111 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request'); 112 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request');
112 like(http_get('/notfound'), qr/404/, 'keepalive memcached not found'); 113 like(http_get('/notfound'), qr/404/, 'keepalive memcached not found');
116 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again'); 117 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
117 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again'); 118 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
118 119
119 is($memd1->stats()->{total}->{total_connections}, $total + 1, 120 is($memd1->stats()->{total}->{total_connections}, $total + 1,
120 'only one connection used'); 121 'only one connection used');
122
123 # Since nginx doesn't read all data from connection in some situations (head
124 # requests, post_action, errors writing to client) we have to close such
125 # connections. Check if we really do close them.
126
127 $total = $memd1->stats()->{total}->{total_connections};
128
129 unlike(http_head('/'), qr/SEE-THIS/, 'head request');
130 like(http_get('/'), qr/SEE-THIS/, 'get after head');
131
132 is($memd1->stats()->{total}->{total_connections}, $total + 1,
133 'head request closes connection');
134
135 $total = $memd1->stats()->{total}->{total_connections};
136
137 unlike(http_head('/big'), qr/XXX/, 'big head');
138 like(http_get('/'), qr/SEE-THIS/, 'get after big head');
139
140 is($memd1->stats()->{total}->{total_connections}, $total + 1,
141 'big head request closes connection');
121 142
122 # two backends with 'single' option - should establish only one connection 143 # two backends with 'single' option - should establish only one connection
123 144
124 $total = $memd1->stats()->{total}->{total_connections} + 145 $total = $memd1->stats()->{total}->{total_connections} +
125 $memd2->stats()->{total}->{total_connections}; 146 $memd2->stats()->{total}->{total_connections};