several response body#close fixes
[rainbows.git] / lib / rainbows / writer_thread_spawn.rb
blob43e4f2c21aee3555f8feb1a283d670d5c10a412c
1 # -*- encoding: binary -*-
2 require 'thread'
3 # This concurrency model implements a single-threaded app dispatch and
4 # spawns a new thread for writing responses.  This concurrency model
5 # should be ideal for apps that serve large responses or stream
6 # responses slowly.
8 # Unlike most \Rainbows! concurrency models, WriterThreadSpawn is
9 # designed to run behind nginx just like Unicorn is.  This concurrency
10 # model may be useful for existing Unicorn users looking for more
11 # output concurrency than socket buffers can provide while still
12 # maintaining a single-threaded application dispatch (though if the
13 # response body is generated on-the-fly, it must be thread safe).
15 # For serving large or streaming responses, setting
16 # "proxy_buffering off" in nginx is recommended.  If your application
17 # does not handle uploads, then using any HTTP-aware proxy like
18 # haproxy is fine.  Using a non-HTTP-aware proxy will leave you
19 # vulnerable to slow client denial-of-service attacks.
21 module Rainbows::WriterThreadSpawn
22   # :stopdoc:
23   include Rainbows::Base
25   def write_body(my_sock, body, range) # :nodoc:
26     if body.respond_to?(:close)
27       Rainbows::SyncClose.new(body) { |body| my_sock.queue_body(body, range) }
28     else
29       my_sock.queue_body(body, range)
30     end
31   end
33   def process_client(client) # :nodoc:
34     super(Client.new(client))
35   end
37   def worker_loop(worker)  # :nodoc:
38     Client.const_set(:MAX, worker_connections)
39     super # accept loop from Unicorn
40     Client.quit
41   end
42   # :startdoc:
43 end
44 # :enddoc:
45 require 'rainbows/writer_thread_spawn/client'