From ed7669ced3aba5c0ba6f5fbee9411546b32c96df Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 5 Jan 2011 16:29:53 -0800 Subject: simplify keepalive_timeout accounting Easier just to use an instance variable --- lib/rainbows.rb | 9 ++++++--- lib/rainbows/coolio/heartbeat.rb | 2 +- lib/rainbows/event_machine/client.rb | 2 +- lib/rainbows/http_server.rb | 4 ++-- lib/rainbows/response.rb | 3 ++- lib/rainbows/revactor/client.rb | 3 ++- lib/rainbows/timed_read.rb | 4 ++-- 7 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/rainbows.rb b/lib/rainbows.rb index a8bfa78..c99b465 100644 --- a/lib/rainbows.rb +++ b/lib/rainbows.rb @@ -17,7 +17,7 @@ module Rainbows # this struct is only accessed inside workers and thus private to each # G.cur may not be used in the network concurrency model # :stopdoc: - class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp,:expire) + class State < Struct.new(:alive,:m,:cur,:server,:tmp,:expire) def tick tmp.chmod(self.m = m == 0 ? 1 : 0) exit!(2) if expire && Time.now >= expire @@ -32,7 +32,7 @@ module Rainbows false end end - G = State.new(true, 0, 0, 5) + G = State.new(true, 0, 0) O = {} class Response416 < RangeError; end @@ -87,13 +87,16 @@ module Rainbows end # :stopdoc: - attr_accessor :max_bytes + attr_accessor :max_bytes, :keepalive_timeout # :startdoc: end # the default max body size is 1 megabyte (1024 * 1024 bytes) @max_bytes = 1024 * 1024 + # the default keepalive_timeout is 5 seconds + @keepalive_timeout = 5 + # :stopdoc: # maps models to default worker counts, default worker count numbers are # pretty arbitrary and tuning them to your application and hardware is diff --git a/lib/rainbows/coolio/heartbeat.rb b/lib/rainbows/coolio/heartbeat.rb index d1f4747..4506b84 100644 --- a/lib/rainbows/coolio/heartbeat.rb +++ b/lib/rainbows/coolio/heartbeat.rb @@ -11,7 +11,7 @@ class Rainbows::Coolio::Heartbeat < Coolio::TimerWatcher G = Rainbows::G def on_timer - if (ot = G.kato) >= 0 + if (ot = Rainbows.keepalive_timeout) >= 0 ot = Time.now - ot KATO.delete_if { |client, time| time < ot and client.timeout? } end diff --git a/lib/rainbows/event_machine/client.rb b/lib/rainbows/event_machine/client.rb index 3312f5d..d8ed6df 100644 --- a/lib/rainbows/event_machine/client.rb +++ b/lib/rainbows/event_machine/client.rb @@ -49,7 +49,7 @@ class Rainbows::EventMachine::Client < EM::Connection @state = :headers em_write_response(response, true) if @buf.empty? - set_comm_inactivity_timeout(G.kato) + set_comm_inactivity_timeout(Rainbows.keepalive_timeout) elsif @body.nil? EM.next_tick { receive_data(nil) } end diff --git a/lib/rainbows/http_server.rb b/lib/rainbows/http_server.rb index 906fa0a..13b731b 100644 --- a/lib/rainbows/http_server.rb +++ b/lib/rainbows/http_server.rb @@ -36,7 +36,7 @@ class Rainbows::HttpServer < Unicorn::HttpServer def load_config! use :Base - G.kato = 5 + Rainbows.keepalive_timeout = 5 Rainbows.max_bytes = 1024 * 1024 @worker_connections = nil super @@ -91,7 +91,7 @@ class Rainbows::HttpServer < Unicorn::HttpServer def keepalive_timeout(nr) (Integer === nr && nr >= 0) or raise ArgumentError, "keepalive_timeout must be a non-negative Integer" - G.kato = nr + Rainbows.keepalive_timeout = nr end def keepalive_requests(nr) diff --git a/lib/rainbows/response.rb b/lib/rainbows/response.rb index 9a46659..4992696 100644 --- a/lib/rainbows/response.rb +++ b/lib/rainbows/response.rb @@ -12,7 +12,8 @@ module Rainbows::Response # called after forking def self.setup(klass) Kgio.accept_class = Rainbows::Client - 0 == Rainbows::G.kato and Rainbows::HttpParser.keepalive_requests = 0 + 0 == Rainbows.keepalive_timeout and + Rainbows::HttpParser.keepalive_requests = 0 end def write_headers(status, headers, alive) diff --git a/lib/rainbows/revactor/client.rb b/lib/rainbows/revactor/client.rb index 7c4b53d..fc095b5 100644 --- a/lib/rainbows/revactor/client.rb +++ b/lib/rainbows/revactor/client.rb @@ -4,7 +4,8 @@ require 'fcntl' class Rainbows::Revactor::Client autoload :TeeSocket, 'rainbows/revactor/client/tee_socket' RD_ARGS = {} - RD_ARGS[:timeout] = Rainbows::G.kato if Rainbows::G.kato > 0 + Rainbows.keepalive_timeout > 0 and + RD_ARGS[:timeout] = Rainbows.keepalive_timeout attr_reader :kgio_addr def initialize(client) diff --git a/lib/rainbows/timed_read.rb b/lib/rainbows/timed_read.rb index dd7939e..72cabbb 100644 --- a/lib/rainbows/timed_read.rb +++ b/lib/rainbows/timed_read.rb @@ -4,11 +4,11 @@ module Rainbows::TimedRead G = Rainbows::G # :nodoc: def read_expire - Time.now + G.kato + Time.now + Rainbows.keepalive_timeout end def kgio_wait_readable - IO.select([self], nil, nil, G.kato) + IO.select([self], nil, nil, Rainbows.keepalive_timeout) end # used for reading headers (respecting keepalive_timeout) -- cgit v1.2.3-24-ge0c7