epoll/xepoll: more consistent client implementations
[rainbows.git] / lib / rainbows / epoll / server.rb
blobab5a49f4ca44b8fda4af88738561d952ff6a0341
1 # -*- encoding: binary -*-
2 # :enddoc:
3 module Rainbows::Epoll::Server
4   @@nr = 0
5   IN = SleepyPenguin::Epoll::IN | SleepyPenguin::Epoll::ET
6   MAX = Rainbows.server.worker_connections
7   THRESH = MAX - 1
8   LISTENERS = Rainbows::HttpServer::LISTENERS
9   EP = Rainbows::EP
11   def self.nr
12     @@nr
13   end
15   # rearms all listeners when there's a free slot
16   def self.decr
17     THRESH == (@@nr -= 1) and LISTENERS.each { |sock| EP.set(sock, IN) }
18   end
20   def self.extended(sock)
21     EP.set(sock, IN)
22   end
24   def epoll_run
25     return EP.delete(self) if @@nr >= MAX
26     while io = kgio_tryaccept
27       @@nr += 1
28       # there's a chance the client never even sees epoll for simple apps
29       io.epoll_once
30       return EP.delete(self) if @@nr >= MAX
31     end
32   end
33 end