rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob da0a0d17d4b20f3f7786a196188df0f89ef17e76 1072 bytes (raw)
$ git show v3.2.0:lib/rainbows/xepoll/client.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
# -*- encoding: binary -*-
# :enddoc:

module Rainbows::XEpoll::Client
  N = Raindrops.new(1)
  Rainbows::Epoll.nr_clients = lambda { N[0] }
  include Rainbows::Epoll::Client
  MAX = Rainbows.server.worker_connections
  THRESH = MAX - 1
  EP = Rainbows::Epoll::EP
  THREADS = Rainbows::HttpServer::LISTENERS.map do |sock|
    Thread.new(sock) do |sock|
      sleep
      begin
        if io = sock.kgio_accept
          N.incr(0, 1)
          io.epoll_once
        end
        sleep while N[0] >= MAX
      rescue => e
        Rainbows::Error.listen_loop(e)
      end while Rainbows.alive
    end
  end

  def self.run
    THREADS.each { |t| t.run }
    Rainbows::Epoll.loop
    Rainbows::JoinThreads.acceptors(THREADS)
  end

  # only call this once
  def epoll_once
    @wr_queue = [] # may contain String, ResponsePipe, and StreamFile objects
    post_init
    EP.set(self, IN) # wake up the main thread
    rescue => e
      Rainbows::Error.write(self, e)
  end

  def on_close
    KATO.delete(self)
    N.decr(0, 1) == THRESH and THREADS.each { |t| t.run }
  end
end

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