writer_thread_spawn: factor out Client.quit
[rainbows.git] / lib / rainbows / writer_thread_spawn.rb
blob0e7d1a74ddfaa80c2ddf259eb615600d103afda8
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     my_sock.queue_body(body, range)
27   end
29   def process_client(client) # :nodoc:
30     super(Client.new(client))
31   end
33   def worker_loop(worker)  # :nodoc:
34     Client.const_set(:MAX, worker_connections)
35     super # accept loop from Unicorn
36     Client.quit
37   end
38   # :startdoc:
39 end
40 # :enddoc:
41 require 'rainbows/writer_thread_spawn/client'