From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS46457 204.124.83.0/24 X-Spam-Status: No, score=-1.6 required=3.0 tests=AWL,BAYES_00,RCVD_IN_XBL, RDNS_NONE shortcircuit=no autolearn=no version=3.3.2 X-Original-To: spew@80x24.org Received: from 80x24.org (unknown [204.124.83.134]) by dcvr.yhbt.net (Postfix) with ESMTP id F35DE1F5B6 for ; Tue, 10 Nov 2015 03:55:12 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] http_parser: handle keepalive_requests internally Date: Tue, 10 Nov 2015 03:55:10 +0000 Message-Id: <20151110035510.9504-1-e@80x24.org> List-Id: unicorn 5 dropped support for this, essentially allowing unlimited persistent connections if we used the parser as-is. Since most of our concurrency models cannot handle infinite persistent connections without being vulnerable to DoS, we must support keepalive_requests like nginx does. --- lib/rainbows/http_parser.rb | 15 +++++++++++++++ lib/rainbows/http_server.rb | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/rainbows/http_parser.rb b/lib/rainbows/http_parser.rb index ec55fe9..30a67cb 100644 --- a/lib/rainbows/http_parser.rb +++ b/lib/rainbows/http_parser.rb @@ -2,6 +2,21 @@ # :enddoc: # avoid modifying Unicorn::HttpParser class Rainbows::HttpParser < Unicorn::HttpParser + @keepalive_requests = 100 + class << self + attr_accessor :keepalive_requests + end + + def initialize(*args) + @keepalive_requests = self.class.keepalive_requests + super + end + + def next? + return false if (@keepalive_requests -= 1) <= 0 + super + end + def self.quit alias_method :next?, :never! end diff --git a/lib/rainbows/http_server.rb b/lib/rainbows/http_server.rb index 637710d..09f2589 100644 --- a/lib/rainbows/http_server.rb +++ b/lib/rainbows/http_server.rb @@ -92,11 +92,11 @@ class Rainbows::HttpServer < Unicorn::HttpServer end def keepalive_requests=(nr) - Unicorn::HttpRequest.keepalive_requests = nr + Rainbows::HttpParser.keepalive_requests = nr end def keepalive_requests - Unicorn::HttpRequest.keepalive_requests + Rainbows::HttpParser.keepalive_requests end def client_max_header_size=(bytes) -- EW