base: constant/namespace cleanup
[rainbows.git] / lib / rainbows / base.rb
blobcef2d2e478e29ff4a9a8d5184035da92f685170d
1 # -*- encoding: binary -*-
2 require 'rainbows/tee_input'
4 # base class for Rainbows concurrency models, this is currently used by
5 # ThreadSpawn and ThreadPool models.  Base is also its own
6 # (non-)concurrency model which is basically Unicorn-with-keepalive, and
7 # not intended for production use, as keepalive with a pure prefork
8 # concurrency model is extremely expensive.
9 module Rainbows::Base
11   # :stopdoc:
12   include Rainbows::Const
14   # shortcuts...
15   G = Rainbows::G
16   NULL_IO = Unicorn::HttpRequest::NULL_IO
17   TeeInput = Rainbows::TeeInput
18   HttpResponse = Rainbows::HttpResponse
19   HttpParser = Unicorn::HttpParser
21   # this method is called by all current concurrency models
22   def init_worker_process(worker)
23     super(worker)
24     Rainbows::MaxBody.setup
25     G.tmp = worker.tmp
27     # avoid spurious wakeups and blocking-accept() with 1.8 green threads
28     if ! defined?(RUBY_ENGINE) && RUBY_VERSION.to_f < 1.9
29       require "io/nonblock"
30       Rainbows::HttpServer::LISTENERS.each { |l| l.nonblock = true }
31     end
33     # we're don't use the self-pipe mechanism in the Rainbows! worker
34     # since we don't defer reopening logs
35     Rainbows::HttpServer::SELF_PIPE.each { |x| x.close }.clear
36     trap(:USR1) { reopen_worker_logs(worker.nr) }
37     trap(:QUIT) { G.quit! }
38     [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
39     logger.info "Rainbows! #@use worker_connections=#@worker_connections"
40   end
42   if IO.respond_to?(:copy_stream)
43     def write_body(client, body)
44       if body.respond_to?(:to_path)
45         IO.copy_stream(Rainbows.body_to_io(body), client)
46       else
47         body.each { |chunk| client.write(chunk) }
48       end
49       ensure
50         body.respond_to?(:close) and body.close
51     end
52   else
53     def write_body(client, body)
54       body.each { |chunk| client.write(chunk) }
55       ensure
56         body.respond_to?(:close) and body.close
57     end
58   end
60   module_function :write_body
62   # once a client is accepted, it is processed in its entirety here
63   # in 3 easy steps: read request, call app, write app response
64   # this is used by synchronous concurrency models
65   #   Base, ThreadSpawn, ThreadPool
66   def process_client(client)
67     buf = client.readpartial(CHUNK_SIZE) # accept filters protect us here
68     hp = HttpParser.new
69     env = {}
70     alive = true
71     remote_addr = Rainbows.addr(client)
73     begin # loop
74       while ! hp.headers(env, buf)
75         IO.select([client], nil, nil, G.kato) or return
76         buf << client.readpartial(CHUNK_SIZE)
77       end
79       env[CLIENT_IO] = client
80       env[RACK_INPUT] = 0 == hp.content_length ?
81                         NULL_IO : TeeInput.new(client, env, hp, buf)
82       env[REMOTE_ADDR] = remote_addr
83       status, headers, body = app.call(env.update(RACK_DEFAULTS))
85       if 100 == status.to_i
86         client.write(EXPECT_100_RESPONSE)
87         env.delete(HTTP_EXPECT)
88         status, headers, body = app.call(env)
89       end
91       alive = hp.keepalive? && G.alive
92       if hp.headers?
93         out = [ alive ? CONN_ALIVE : CONN_CLOSE ]
94         client.write(HttpResponse.header_string(status, headers, out))
95       end
96       write_body(client, body)
97     end while alive and hp.reset.nil? and env.clear
98   # if we get any error, try to write something back to the client
99   # assuming we haven't closed the socket, but don't get hung up
100   # if the socket is already closed or broken.  We'll always ensure
101   # the socket is closed at the end of this function
102   rescue => e
103     Rainbows::Error.write(client, e)
104   ensure
105     client.close unless client.closed?
106   end
108   def self.included(klass)
109     klass.const_set :LISTENERS, Rainbows::HttpServer::LISTENERS
110     klass.const_set :G, Rainbows::G
111   end
113   # :startdoc: