globally refactor Range handling for responses
[rainbows.git] / lib / rainbows / writer_thread_pool.rb
blob558827ffeb06758d91a1fe7cacab5c4a9571c58b
1 # -*- encoding: binary -*-
3 # This concurrency model implements a single-threaded app dispatch
4 # with a separate thread pool for writing responses.
6 # Unlike most \Rainbows! concurrency models, WriterThreadPool is
7 # designed to run behind nginx just like Unicorn is.  This concurrency
8 # model may be useful for existing Unicorn users looking for more
9 # output concurrency than socket buffers can provide while still
10 # maintaining a single-threaded application dispatch (though if the
11 # response body is dynamically generated, it must be thread safe).
13 # For serving large or streaming responses, using more threads (via
14 # the +worker_connections+ setting) and setting "proxy_buffering off"
15 # in nginx is recommended.  If your application does not handle
16 # uploads, then using any HTTP-aware proxy like haproxy is fine.
17 # Using a non-HTTP-aware proxy will leave you vulnerable to
18 # slow client denial-of-service attacks.
19 module Rainbows::WriterThreadPool
20   # :stopdoc:
21   include Rainbows::Base
22   autoload :Client, 'rainbows/writer_thread_pool/client'
24   @@nr = 0
25   @@q = nil
27   def process_client(client) # :nodoc:
28     @@nr += 1
29     Client.new(client, @@q[@@nr %= @@q.size]).process_loop
30   end
32   def worker_loop(worker) # :nodoc:
33     # we have multiple, single-thread queues since we don't want to
34     # interleave writes from the same client
35     qp = (1..worker_connections).map do |n|
36       Rainbows::QueuePool.new(1) do |response|
37         begin
38           io, arg, *rest = response
39           case arg
40           when String
41             io.kgio_write(arg)
42           when :close
43             warn "#{Thread.current} #{io} close"
44             io.close unless io.closed?
45           else
46             warn "#{Thread.current} #{io} #{arg}"
47             io.__send__(arg, *rest)
48           end
49         rescue => err
50           Rainbows::Error.write(io, err)
51         end
52       end
53     end
55     @@q = qp.map { |q| q.queue }
56     super(worker) # accept loop from Unicorn
57     qp.map { |q| q.quit! }
58   end
59   # :startdoc:
60 end