start using kgio library
[rainbows.git] / lib / rainbows / rev / thread.rb
blob7b7d4558cb3c733f825d9547fb1470021f8c94e9
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.keepalive? && G.alive
24         rev_write_response(response, alive)
25         return quit unless alive && :close != @state
27         @env.clear
28         @hp.reset
29         @state = :headers
30       end
32       # fails-safe application dispatch, we absolutely cannot
33       # afford to fail or raise an exception (killing the thread)
34       # here because that could cause a deadlock and we'd leak FDs
35       def app_response
36         begin
37           @env[REMOTE_ADDR] = @_io.kgio_addr
38           APP.call(@env.update(RACK_DEFAULTS))
39         rescue => e
40           Error.app(e) # we guarantee this does not raise
41           [ 500, {}, [] ]
42         end
43       end
45     end
46   end
47 end