tests: disable async_sinatra test for Ruby 1.9.2
[rainbows.git] / lib / rainbows.rb
blobe18654984e91532b23196efce058ea025a0422e4
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   # :stopdoc:
26   G = State.new(true, 0, 0, 5)
27   O = {}
28   # :startdoc:
30   require 'rainbows/const'
31   require 'rainbows/http_server'
32   require 'rainbows/http_response'
33   require 'rainbows/base'
34   require 'rainbows/tee_input'
35   autoload :Sendfile, 'rainbows/sendfile'
36   autoload :AppPool, 'rainbows/app_pool'
37   autoload :DevFdResponse, 'rainbows/dev_fd_response'
38   autoload :MaxBody, 'rainbows/max_body'
39   autoload :QueuePool, 'rainbows/queue_pool'
41   class << self
43     # Sleeps the current application dispatch.  This will pick the
44     # optimal method to sleep depending on the concurrency model chosen
45     # (which may still suck and block the entire process).  Using this
46     # with the basic :Rev or :EventMachine models is not recommended.
47     # This should be used within your Rack application.
48     def sleep(nr)
49       case G.server.use
50       when :FiberPool, :FiberSpawn
51         Rainbows::Fiber.sleep(nr)
52       when :RevFiberSpawn
53         Rainbows::Fiber::Rev::Sleeper.new(nr)
54       when :Revactor
55         Actor.sleep(nr)
56       else
57         Kernel.sleep(nr)
58       end
59     end
61     # runs the Rainbows! HttpServer with +app+ and +options+ and does
62     # not return until the server has exited.
63     def run(app, options = {})
64       HttpServer.new(app, options).start.join
65     end
67     # returns nil if accept fails
68     def sync_accept(sock)
69       rv = sock.accept
70       rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
71       rv
72     rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EINTR
73     end
75     # returns nil if accept fails
76     def accept(sock)
77       rv = sock.accept_nonblock
78       rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
79       rv
80     rescue Errno::EAGAIN, Errno::ECONNABORTED
81     end
83     # returns a string representing the address of the given client +io+
84     # For local UNIX domain sockets, this will return a string referred
85     # to by the (non-frozen) Unicorn::HttpRequest::LOCALHOST constant.
86     def addr(io)
87       io.respond_to?(:peeraddr) ?
88                         io.peeraddr.last : Unicorn::HttpRequest::LOCALHOST
89     end
91     # the default max body size is 1 megabyte (1024 * 1024 bytes)
92     @@max_bytes = 1024 * 1024
94     def max_bytes; @@max_bytes; end
95     def max_bytes=(nr); @@max_bytes = nr; end
96   end
98   # configures \Rainbows! with a given concurrency model to +use+ and
99   # a +worker_connections+ upper-bound.  This method may be called
100   # inside a Unicorn/Rainbows configuration file:
101   #
102   #   Rainbows! do
103   #     use :Revactor # this may also be :ThreadSpawn or :ThreadPool
104   #     worker_connections 400
105   #     keepalive_timeout 0 # zero disables keepalives entirely
106   #     client_max_body_size 5*1024*1024 # 5 megabytes
107   #   end
108   #
109   #   # the rest of the Unicorn configuration
110   #   worker_processes 8
111   #
112   # See the documentation for the respective Revactor, ThreadSpawn,
113   # and ThreadPool classes for descriptions and recommendations for
114   # each of them.  The total number of clients we're able to serve is
115   # +worker_processes+ * +worker_connections+, so in the above example
116   # we can serve 8 * 400 = 3200 clients concurrently.
117   #
118   # The default is +keepalive_timeout+ is 5 seconds, which should be
119   # enough under most conditions for browsers to render the page and
120   # start retrieving extra elements for.  Increasing this beyond 5
121   # seconds is not recommended.  Zero disables keepalive entirely
122   # (but pipelining fully-formed requests is still works).
123   #
124   # The default +client_max_body_size+ is 1 megabyte (1024 * 1024 bytes),
125   # setting this to +nil+ will disable body size checks and allow any
126   # size to be specified.
127   def Rainbows!(&block)
128     block_given? or raise ArgumentError, "Rainbows! requires a block"
129     HttpServer.setup(block)
130   end
132   # :stopdoc:
133   # maps models to default worker counts, default worker count numbers are
134   # pretty arbitrary and tuning them to your application and hardware is
135   # highly recommended
136   MODEL_WORKER_CONNECTIONS = {
137     :Base => 1, # this one can't change
138     :WriterThreadPool => 20,
139     :WriterThreadSpawn => 20,
140     :Revactor => 50,
141     :ThreadSpawn => 30,
142     :ThreadPool => 20,
143     :Rev => 50,
144     :RevThreadSpawn => 50,
145     :RevThreadPool => 50,
146     :EventMachine => 50,
147     :FiberSpawn => 50,
148     :FiberPool => 50,
149     :ActorSpawn => 50,
150     :NeverBlock => 50,
151     :RevFiberSpawn => 50,
152   }.each do |model, _|
153     u = model.to_s.gsub(/([a-z0-9])([A-Z0-9])/) { "#{$1}_#{$2.downcase!}" }
154     autoload model, "rainbows/#{u.downcase!}"
155   end
156   # :startdoc:
157   autoload :Fiber, 'rainbows/fiber' # core class
161 # inject the Rainbows! method into Unicorn::Configurator
162 Unicorn::Configurator.class_eval { include Rainbows }