about summary refs log tree commit homepage
path: root/ext
diff options
context:
space:
mode:
authorTom Burns <tom.burns@jadedpixel.com>2012-10-30 16:22:21 -0400
committerEric Wong <normalperson@yhbt.net>2012-11-29 20:24:46 +0000
commit5c700fc2cf398848ddcf71a2aa3f0f2a6563e87b (patch)
tree35757c61400f578092de06e813e044cf9b24abd9 /ext
parentf4af812a28b03508c96853739aea53f7a6714abf (diff)
downloadunicorn-5c700fc2cf398848ddcf71a2aa3f0f2a6563e87b.tar.gz
This patch checks incoming connections and avoids calling the application
if the connection has been closed.

It works by sending the beginning of the HTTP response before calling
the application to see if the socket can successfully be written to.

By enabling this feature users can avoid wasting application rendering
time only to find the connection is closed when attempting to write, and
throwing out the result.

When a client disconnects while being queued or processed, Nginx will log
HTTP response 499 but the application will log a 200.

Enabling this feature will minimize the time window during which the problem
can arise.

The feature is disabled by default and can be enabled by adding
'check_client_connection true' to the unicorn config.

[ew: After testing this change, Tom Burns wrote:

  So we just finished the US Black Friday / Cyber Monday weekend running
  unicorn forked with the last version of the patch I had sent you.  It
  worked splendidly and helped us handle huge flash sales without
  increased response time over the weekend.

  Whereas in previous flash traffic scenarios we would see the number of
  HTTP 499 responses grow past the number of real HTTP 200 responses,
  over the weekend we saw no growth in 499s during flash sales.

  Unexpectedly the patch also helped us ward off a DoS attack where the
  attackers were disconnecting immediately after making a request.

ref: <CAK4qKG3rkfVYLyeqEqQyuNEh_nZ8yw0X_cwTxJfJ+TOU+y8F+w@mail.gmail.com>
]

Signed-off-by: Eric Wong <normalperson@yhbt.net>
Diffstat (limited to 'ext')
-rw-r--r--ext/unicorn_http/unicorn_http.rl4
1 files changed, 3 insertions, 1 deletions
diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl
index 96dcf83..1a8003f 100644
--- a/ext/unicorn_http/unicorn_http.rl
+++ b/ext/unicorn_http/unicorn_http.rl
@@ -115,7 +115,7 @@ struct http_parser {
   } len;
 };
 
-static ID id_clear, id_set_backtrace;
+static ID id_clear, id_set_backtrace, id_response_start_sent;
 
 static void finalize_header(struct http_parser *hp);
 
@@ -626,6 +626,7 @@ static VALUE HttpParser_clear(VALUE self)
 
   http_parser_init(hp);
   rb_funcall(hp->env, id_clear, 0);
+  rb_ivar_set(self, id_response_start_sent, Qfalse);
 
   return self;
 }
@@ -1031,6 +1032,7 @@ void Init_unicorn_http(void)
   SET_GLOBAL(g_http_connection, "CONNECTION");
   id_clear = rb_intern("clear");
   id_set_backtrace = rb_intern("set_backtrace");
+  id_response_start_sent = rb_intern("@response_start_sent");
   init_unicorn_httpdate();
 }
 #undef SET_GLOBAL