rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 8fc717212d3f8b522d22f381ad623f84c8f70dce 1494 bytes (raw)
$ git show v0.95.1:lib/rainbows/rev/thread.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
46
47
48
49
50
51
52
53
54
55
56
 
# -*- encoding: binary -*-
# :enddoc:
require 'thread'
require 'rainbows/rev/master'

RUBY_VERSION =~ %r{\A1\.8} && Rev::VERSION < "0.3.2" and
  warn "Rev (< 0.3.2) and Threads do not mix well under Ruby 1.8"

module Rainbows
  module Rev

    class ThreadClient < Client

      def app_call
        KATO.delete(self)
        disable
        @env[RACK_INPUT] = @input
        app_dispatch # must be implemented by subclass
      end

      # this is only called in the master thread
      def response_write(response)
        enable
        alive = @hp.keepalive? && G.alive
        out = [ alive ? CONN_ALIVE : CONN_CLOSE ] if @hp.headers?
        rev_write_response(response, out)
        return quit unless alive && G.alive

        @env.clear
        @hp.reset
        @state = :headers
        # keepalive requests are always body-less, so @input is unchanged
        if @hp.headers(@env, @buf)
          @input = HttpRequest::NULL_IO
          app_call
        else
          KATO[self] = Time.now
        end
      end

      # fails-safe application dispatch, we absolutely cannot
      # afford to fail or raise an exception (killing the thread)
      # here because that could cause a deadlock and we'd leak FDs
      def app_response
        begin
          @env[REMOTE_ADDR] = @remote_addr
          APP.call(@env.update(RACK_DEFAULTS))
        rescue => e
          Error.app(e) # we guarantee this does not raise
          [ 500, {}, [] ]
        end
      end

    end
  end
end

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