rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 58e7653a9b88c02e6d0ba3fa46253e9d844d3606 835 bytes (raw)
$ git show v3.2.0:lib/rainbows/epoll/server.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
# -*- encoding: binary -*-
# :enddoc:
module Rainbows::Epoll::Server
  @@nr = 0
  Rainbows::Epoll.nr_clients = lambda { @@nr }
  IN = SleepyPenguin::Epoll::IN | SleepyPenguin::Epoll::ET
  MAX = Rainbows.server.worker_connections
  THRESH = MAX - 1
  LISTENERS = Rainbows::HttpServer::LISTENERS
  EP = Rainbows::Epoll::EP

  def self.run
    LISTENERS.each { |sock| EP.add(sock.extend(self), IN) }
    Rainbows::Epoll.loop
  end

  # rearms all listeners when there's a free slot
  def self.decr
    THRESH == (@@nr -= 1) and LISTENERS.each { |sock| EP.set(sock, IN) }
  end

  def epoll_run
    return EP.delete(self) if @@nr >= MAX
    while io = kgio_tryaccept
      @@nr += 1
      # there's a chance the client never even sees epoll for simple apps
      io.epoll_once
      return EP.delete(self) if @@nr >= MAX
    end
  end
end

git clone https://yhbt.net/rainbows.git