changeset 33:52ca695446d3

Keepalive: more connection validity checks. Make sure to not cache connections with errors detected, as well as EOF already read or pending data present. Call ngx_handle_read_event() for event methods which need it.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 31 Aug 2011 21:21:14 +0400
parents 9aa0d263f2a7
children e10649a96f39
files ngx_http_upstream_keepalive_module.c t/proxy.t
diffstat 2 files changed, 33 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ngx_http_upstream_keepalive_module.c
+++ b/ngx_http_upstream_keepalive_module.c
@@ -337,7 +337,11 @@ ngx_http_upstream_free_keepalive_peer(ng
 
     if (kp->failed
         || c == NULL
+        || c->read->eof
+        || c->read->ready
+        || c->read->error
         || c->read->timedout
+        || c->write->error
         || c->write->timedout)
     {
         goto invalid;
@@ -370,6 +374,10 @@ ngx_http_upstream_free_keepalive_peer(ng
 
 #endif
 
+    if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+        goto invalid;
+    }
+
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
                    "free keepalive peer: saving connection %p", c);
 
--- a/t/proxy.t
+++ b/t/proxy.t
@@ -77,7 +77,7 @@ eval {
 };
 plan(skip_all => 'no keepalive patches') if $@;
 
-$t->plan(42);
+$t->plan(50);
 
 ###############################################################################
 
@@ -199,6 +199,20 @@ like(http_get('/inmemory/complex2'), qr/
 like(http_get('/inmemory/chunk01'), qr/set: $/, 'inmemory 0 chunk');
 like(http_get('/inmemory/chunk02'), qr/set: $/, 'inmemory 0 chunk 2');
 
+# closed connection tests
+
+like(http_get('/buffered/closed1'), qr/200 OK/, 'buffered closed 1');
+like(http_get('/buffered/closed2'), qr/200 OK/, 'buffered closed 2');
+like(http_get('/unbuffered/closed1'), qr/200 OK/, 'unbuffered closed 1');
+like(http_get('/unbuffered/closed2'), qr/200 OK/, 'unbuffered closed 2');
+like(http_get('/inmemory/closed1'), qr/200 OK/, 'inmemory closed 1');
+like(http_get('/inmemory/closed2'), qr/200 OK/, 'inmemory closed 2');
+
+# check for errors, shouldn't be any
+
+like(`grep -F '[alert]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no alerts');
+like(`grep -F '[error]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no errors');
+
 ###############################################################################
 
 sub http_daemon {
@@ -316,6 +330,16 @@ sub http_daemon {
 					"0" . CRLF . CRLF
 					unless $headers =~ /^HEAD/i;
 
+			} elsif ($uri =~ m/closed/) {
+				print $client
+					"HTTP/1.1 200 OK" . CRLF .
+					"X-Request: $rcount" . CRLF .
+					"X-Connection: $ccount" . CRLF .
+					"Connection: close" . CRLF .
+					"Content-Length: 12" . CRLF . CRLF .
+					"0123456789" . CRLF;
+				last;
+
 			} else {
         			print $client
 					"HTTP/1.1 404 Not Found" . CRLF .