add RevThreadPool concurrency model
[rainbows.git] / lib / rainbows.rb
blobe42f6cf83849a1430cf803f67e12c47de25a0414
1 # -*- encoding: binary -*-
2 require 'unicorn'
3 require 'rainbows/error'
4 require 'fcntl'
6 module Rainbows
8   # global vars because class/instance variables are confusing me :<
9   # this struct is only accessed inside workers and thus private to each
10   # G.cur may not be used in the network concurrency model
11   class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp,:expire)
12     def tick
13       tmp.chmod(self.m = m == 0 ? 1 : 0)
14       exit!(2) if expire && Time.now >= expire
15       alive && server.master_pid == Process.ppid or quit!
16     end
18     def quit!
19       self.alive = false
20       self.expire ||= Time.now + (server.timeout * 2.0)
21       server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil }
22       false
23     end
24   end
25   G = State.new(true, 0, 0, 2)
26   O = {}
28   require 'rainbows/const'
29   require 'rainbows/http_server'
30   require 'rainbows/http_response'
31   require 'rainbows/base'
32   autoload :AppPool, 'rainbows/app_pool'
33   autoload :DevFdResponse, 'rainbows/dev_fd_response'
35   class << self
37     # runs the Rainbows! HttpServer with +app+ and +options+ and does
38     # not return until the server has exited.
39     def run(app, options = {})
40       HttpServer.new(app, options).start.join
41     end
42   end
44   # configures \Rainbows! with a given concurrency model to +use+ and
45   # a +worker_connections+ upper-bound.  This method may be called
46   # inside a Unicorn/Rainbows configuration file:
47   #
48   #   Rainbows! do
49   #     use :Revactor # this may also be :ThreadSpawn or :ThreadPool
50   #     worker_connections 400
51   #     keepalive_timeout 0 # zero disables keepalives entirely
52   #   end
53   #
54   #   # the rest of the Unicorn configuration
55   #   worker_processes 8
56   #
57   # See the documentation for the respective Revactor, ThreadSpawn,
58   # and ThreadPool classes for descriptions and recommendations for
59   # each of them.  The total number of clients we're able to serve is
60   # +worker_processes+ * +worker_connections+, so in the above example
61   # we can serve 8 * 400 = 3200 clients concurrently.
62   #
63   # The default is +keepalive_timeout+ is 2 seconds, which should be
64   # enough under most conditions for browsers to render the page and
65   # start retrieving extra elements for.  Increasing this beyond 5
66   # seconds is not recommended.  Zero disables keepalive entirely
67   # (but pipelining fully-formed requests is still works).
68   def Rainbows!(&block)
69     block_given? or raise ArgumentError, "Rainbows! requires a block"
70     HttpServer.setup(block)
71   end
73   # maps models to default worker counts, default worker count numbers are
74   # pretty arbitrary and tuning them to your application and hardware is
75   # highly recommended
76   MODEL_WORKER_CONNECTIONS = {
77     :Base => 1, # this one can't change
78     :Revactor => 50,
79     :ThreadSpawn => 30,
80     :ThreadPool => 10,
81     :Rev => 50,
82     :RevThreadSpawn => 50,
83     :RevThreadPool => 50,
84     :EventMachine => 50,
85     :FiberSpawn => 50,
86     :FiberPool => 50,
87     :ActorSpawn => 50,
88     :NeverBlock => 50,
89   }.each do |model, _|
90     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
91     autoload model, "rainbows/#{u.downcase!}"
92   end
93   autoload :Fiber, 'rainbows/fiber' # core class
95   # returns nil if accept fails
96   if defined?(Fcntl::FD_CLOEXEC)
97     def self.accept(sock)
98       rv = sock.accept_nonblock
99       rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
100       rv
101     rescue Errno::EAGAIN, Errno::ECONNABORTED
102     end
103   else
104     def self.accept(sock)
105       sock.accept_nonblock
106     rescue Errno::EAGAIN, Errno::ECONNABORTED
107     end
108   end
112 # inject the Rainbows! method into Unicorn::Configurator
113 Unicorn::Configurator.class_eval { include Rainbows }