avoid HttpParser#keepalive? and HttpParser#reset
[rainbows.git] / lib / rainbows / rev / thread.rb
blob0f36ce54af07937667cfb1ec56f9add73d047989
1 # -*- encoding: binary -*-
2 # :enddoc:
3 require 'thread'
4 require 'rainbows/rev/master'
6 RUBY_VERSION =~ %r{\A1\.8} && Rev::VERSION < "0.3.2" and
7   warn "Rev (< 0.3.2) and Threads do not mix well under Ruby 1.8"
9 module Rainbows
10   module Rev
12     class ThreadClient < Client
14       def app_call
15         KATO.delete(self)
16         disable if enabled?
17         @env[RACK_INPUT] = @input
18         app_dispatch # must be implemented by subclass
19       end
21       # this is only called in the master thread
22       def response_write(response)
23         alive = @hp.next? && G.alive
24         rev_write_response(response, alive)
25         return quit unless alive && :close != @state
27         @state = :headers
28       end
30       # fails-safe application dispatch, we absolutely cannot
31       # afford to fail or raise an exception (killing the thread)
32       # here because that could cause a deadlock and we'd leak FDs
33       def app_response
34         begin
35           @env[REMOTE_ADDR] = @_io.kgio_addr
36           APP.call(@env.update(RACK_DEFAULTS))
37         rescue => e
38           Error.app(e) # we guarantee this does not raise
39           [ 500, {}, [] ]
40         end
41       end
43     end
44   end
45 end