rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 43e4f2c21aee3555f8feb1a283d670d5c10a412c 1572 bytes (raw)
$ git show v2.1.0:lib/rainbows/writer_thread_spawn.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
# -*- encoding: binary -*-
require 'thread'
# This concurrency model implements a single-threaded app dispatch and
# spawns a new thread for writing responses.  This concurrency model
# should be ideal for apps that serve large responses or stream
# responses slowly.
#
# Unlike most \Rainbows! concurrency models, WriterThreadSpawn is
# designed to run behind nginx just like Unicorn is.  This concurrency
# model may be useful for existing Unicorn users looking for more
# output concurrency than socket buffers can provide while still
# maintaining a single-threaded application dispatch (though if the
# response body is generated on-the-fly, it must be thread safe).
#
# For serving large or streaming responses, setting
# "proxy_buffering off" in nginx is recommended.  If your application
# does not handle uploads, then using any HTTP-aware proxy like
# haproxy is fine.  Using a non-HTTP-aware proxy will leave you
# vulnerable to slow client denial-of-service attacks.

module Rainbows::WriterThreadSpawn
  # :stopdoc:
  include Rainbows::Base

  def write_body(my_sock, body, range) # :nodoc:
    if body.respond_to?(:close)
      Rainbows::SyncClose.new(body) { |body| my_sock.queue_body(body, range) }
    else
      my_sock.queue_body(body, range)
    end
  end

  def process_client(client) # :nodoc:
    super(Client.new(client))
  end

  def worker_loop(worker)  # :nodoc:
    Client.const_set(:MAX, worker_connections)
    super # accept loop from Unicorn
    Client.quit
  end
  # :startdoc:
end
# :enddoc:
require 'rainbows/writer_thread_spawn/client'

git clone https://yhbt.net/rainbows.git