From bfb58da95e13f3061feb2c1f284efc80a13ba23c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 30 Apr 2011 06:50:17 +0000 Subject: xepoll_thread_spawn: lower memory usage This one actually has a realistic chance of running hundreds/thousands of clients, and 32K per-client is a huge amount. --- lib/rainbows/xepoll_thread_spawn/client.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/rainbows/xepoll_thread_spawn/client.rb b/lib/rainbows/xepoll_thread_spawn/client.rb index 9735f64..9bee1d1 100644 --- a/lib/rainbows/xepoll_thread_spawn/client.rb +++ b/lib/rainbows/xepoll_thread_spawn/client.rb @@ -9,10 +9,11 @@ module Rainbows::XEpollThreadSpawn::Client ACCEPTORS = Rainbows::HttpServer::LISTENERS.map do |sock| Thread.new do sleep + buf = "" begin if io = sock.kgio_accept(Rainbows::Client) N.incr(0, 1) - io.epoll_once + io.epoll_once(buf) end sleep while N[0] >= max rescue => e @@ -41,8 +42,9 @@ module Rainbows::XEpollThreadSpawn::Client def self.loop ACCEPTORS.each { |thr| thr.run } + buf = "" begin - EP.wait(nil, 1000) { |fl, obj| obj.epoll_run } + EP.wait(nil, 1000) { |fl, obj| obj.epoll_run(buf) } expire rescue Errno::EINTR rescue => e @@ -64,10 +66,9 @@ module Rainbows::XEpollThreadSpawn::Client @@last_expire = now end - def epoll_once + def epoll_once(buf) @hp = Rainbows::HttpParser.new - @buf2 = "" - epoll_run + epoll_run(buf) end def timeout!(defer) @@ -86,13 +87,13 @@ module Rainbows::XEpollThreadSpawn::Client closed? or close end - def epoll_run - case kgio_tryread(0x4000, @buf2) + def epoll_run(buf) + case kgio_tryread(0x1000, buf) when :wait_readable return kato_set when String kato_delete - @hp.buf << @buf2 + @hp.buf << buf env = @hp.parse and return spawn(env, @hp) else return close @@ -107,12 +108,12 @@ module Rainbows::XEpollThreadSpawn::Client def pipeline_ready(hp) env = hp.parse and return env - case kgio_tryread(0x4000, @buf2) + case buf = kgio_tryread(0x1000) when :wait_readable kato_set return false when String - hp.buf << @buf2 + hp.buf << buf env = hp.parse and return env # continue loop else -- cgit v1.2.3-24-ge0c7