From 2489368a624cff50a330238cf3c3f16eb0bd743c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 28 Nov 2009 11:26:39 -0800 Subject: common Rainbows.accept method --- lib/rainbows/actor_spawn.rb | 17 +++++++---------- lib/rainbows/event_machine.rb | 9 +++------ lib/rainbows/fiber_pool.rb | 8 +++----- lib/rainbows/fiber_spawn.rb | 6 +----- lib/rainbows/rev/core.rb | 5 +---- lib/rainbows/thread_pool.rb | 7 ++----- lib/rainbows/thread_spawn.rb | 7 ++----- 7 files changed, 19 insertions(+), 40 deletions(-) (limited to 'lib/rainbows') diff --git a/lib/rainbows/actor_spawn.rb b/lib/rainbows/actor_spawn.rb index 30e62a9..7603734 100644 --- a/lib/rainbows/actor_spawn.rb +++ b/lib/rainbows/actor_spawn.rb @@ -25,17 +25,14 @@ module Rainbows begin ret = IO.select(LISTENERS, nil, nil, 1) and ret.first.each do |l| - next if lock.synchronize { nr >= limit } - begin - Actor.spawn(l.accept_nonblock) do |c| - lock.synchronize { nr += 1 } - begin - process_client(c) - ensure - lock.synchronize { nr -= 1 } - end + lock.synchronize { nr >= limit } and break sleep(0.01) + c = Rainbows.accept(l) and Actor.spawn do + lock.synchronize { nr += 1 } + begin + process_client(c) + ensure + lock.synchronize { nr -= 1 } end - rescue Errno::EAGAIN, Errno::ECONNABORTED end end rescue => e diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb index b099721..e28b232 100644 --- a/lib/rainbows/event_machine.rb +++ b/lib/rainbows/event_machine.rb @@ -174,12 +174,9 @@ module Rainbows def notify_readable return if CUR.size >= MAX - begin - io = @io.accept_nonblock - sig = EM.attach_fd(io.fileno, false) - CUR[sig] = Client.new(sig, io) - rescue Errno::EAGAIN, Errno::ECONNABORTED - end + io = Rainbows.accept(@io) or return + sig = EM.attach_fd(io.fileno, false) + CUR[sig] = Client.new(sig, io) end end diff --git a/lib/rainbows/fiber_pool.rb b/lib/rainbows/fiber_pool.rb index 25f086e..2a1c5f7 100644 --- a/lib/rainbows/fiber_pool.rb +++ b/lib/rainbows/fiber_pool.rb @@ -29,13 +29,11 @@ module Rainbows begin schedule do |l| fib = pool.shift or break # let another worker process take it - io = begin - l.accept_nonblock - rescue Errno::EAGAIN, Errno::ECONNABORTED + if io = Rainbows.accept(l) + fib.resume(Fiber::IO.new(io, fib)) + else pool << fib - next end - fib.resume(Fiber::IO.new(io, fib)) end rescue => e Error.listen_loop(e) diff --git a/lib/rainbows/fiber_spawn.rb b/lib/rainbows/fiber_spawn.rb index da3db01..6104a7b 100644 --- a/lib/rainbows/fiber_spawn.rb +++ b/lib/rainbows/fiber_spawn.rb @@ -22,11 +22,7 @@ module Rainbows begin schedule do |l| break if G.cur >= limit - io = begin - l.accept_nonblock - rescue Errno::EAGAIN, Errno::ECONNABORTED - next - end + io = Rainbows.accept(l) or next ::Fiber.new { process_client(fio.new(io, ::Fiber.current)) }.resume end rescue => e diff --git a/lib/rainbows/rev/core.rb b/lib/rainbows/rev/core.rb index 785990c..b78fe85 100644 --- a/lib/rainbows/rev/core.rb +++ b/lib/rainbows/rev/core.rb @@ -14,10 +14,7 @@ module Rainbows def on_readable return if CONN.size >= MAX - begin - CL.new(@_io.accept_nonblock).attach(LOOP) - rescue Errno::EAGAIN, Errno::ECONNABORTED - end + io = Rainbows.accept(@_io) and CL.new(io).attach(LOOP) end end # class Server diff --git a/lib/rainbows/thread_pool.rb b/lib/rainbows/thread_pool.rb index 949db6f..f398828 100644 --- a/lib/rainbows/thread_pool.rb +++ b/lib/rainbows/thread_pool.rb @@ -49,11 +49,8 @@ module Rainbows # problem. On the other hand, a thundering herd may not # even incur as much overhead as an extra Mutex#synchronize ret = IO.select(LISTENERS, nil, nil, 1) and - ret.first.each do |sock| - begin - process_client(sock.accept_nonblock) - rescue Errno::EAGAIN, Errno::ECONNABORTED - end + ret.first.each do |s| + s = Rainbows.accept(s) and process_client(s) end rescue Errno::EINTR rescue Errno::EBADF, TypeError diff --git a/lib/rainbows/thread_spawn.rb b/lib/rainbows/thread_spawn.rb index 40b37aa..5afb91e 100644 --- a/lib/rainbows/thread_spawn.rb +++ b/lib/rainbows/thread_spawn.rb @@ -37,11 +37,8 @@ module Rainbows sleep(0.1) # hope another process took it break # back to IO.select end - c = begin - l.accept_nonblock - rescue Errno::EAGAIN, Errno::ECONNABORTED - end or next - threads.add(Thread.new { process_client(c) }) + c = Rainbows.accept(l) and + threads.add(Thread.new { process_client(c) }) end rescue Errno::EINTR retry -- cgit v1.2.3-24-ge0c7