1 # -*- encoding: binary -*-
4 module Rainbows::Epoll::Client
6 include Rainbows::EvCore
7 APP = Rainbows.server.app
8 Server = Rainbows::Epoll::Server
9 IN = SleepyPenguin::Epoll::IN | SleepyPenguin::Epoll::ET
10 INLT = SleepyPenguin::Epoll::IN
11 OUT = SleepyPenguin::Epoll::OUT | SleepyPenguin::Epoll::ET
13 KATO.compare_by_identity if KATO.respond_to?(:compare_by_identity)
14 KEEPALIVE_TIMEOUT = Rainbows.keepalive_timeout
15 EP = Rainbows::Epoll::EP
16 @@last_expire = Time.now
19 return if ((now = Time.now) - @@last_expire) < 1.0
20 if (ot = KEEPALIVE_TIMEOUT) >= 0
22 KATO.delete_if { |client, time| time < ot and client.timeout! }
29 @wr_queue = [] # may contain String, ResponsePipe, and StreamFile objects
37 case rv = kgio_tryread(16384, RBUF)
40 return if @wr_queue[0] || closed?
42 KATO[self] = @@last_expire if :headers == @state
43 return EP.set(self, IN)
46 end until :close == @state
48 rescue Errno::ECONNRESET
53 def app_call # called by on_read()
54 @env[RACK_INPUT] = @input
55 @env[REMOTE_ADDR] = kgio_addr
56 status, headers, body = APP.call(@env.merge!(RACK_DEFAULTS))
57 ev_write_response(status, headers, body, @hp.next?)
60 def write_response_path(status, headers, body, alive)
65 defer_file(status, headers, body, alive, io, st)
66 elsif st.socket? || st.pipe?
67 chunk = stream_response_headers(status, headers, alive)
68 stream_response_body(body, io, chunk)
70 # char or block device... WTF?
71 write_response(status, headers, body, alive)
75 # used for streaming sockets and pipes
76 def stream_response_body(body, io, chunk)
77 pipe = (chunk ? Rainbows::Epoll::ResponseChunkPipe :
78 Rainbows::Epoll::ResponsePipe).new(io, self, body)
79 return @wr_queue << pipe if @wr_queue[0]
80 stream_pipe(pipe) or return
81 @wr_queue[0] or @wr_queue << Z
84 def ev_write_response(status, headers, body, alive)
85 if body.respond_to?(:to_path)
86 write_response_path(status, headers, body, alive)
88 write_response(status, headers, body, alive)
90 @state = alive ? :headers : :close
91 on_read(Z) if alive && 0 == @wr_queue.size && 0 != @buf.size
104 Server::ReRun << self
107 def on_deferred_write_complete
108 :close == @state and return close
109 0 == @buf.size ? on_readable : on_read(Z)
113 msg = Rainbows::Error.response(e) and kgio_trywrite(msg) rescue nil
118 def write_deferred(obj)
119 Rainbows::StreamFile === obj ? stream_file(obj) : stream_pipe(obj)
122 # writes until our write buffer is empty or we block
123 # returns true if we're done writing everything
125 obj = @wr_queue.shift
127 case rv = String === obj ? kgio_trywrite(obj) : write_deferred(obj)
129 obj = @wr_queue.shift or return on_deferred_write_complete
132 when :wait_writable # Strings and StreamFiles only
133 @wr_queue.unshift(obj)
143 # this returns an +Array+ write buffer if blocked
146 case rv = kgio_trywrite(buf)
156 @wr_queue << buf.dup # >3-word 1.9 strings are copy-on-write
160 @wr_queue.each { |x| x.respond_to?(:close) and x.close rescue nil }
171 def defer_file(status, headers, body, alive, io, st)
172 if r = sendfile_range(status, headers)
173 status, headers, range = r
174 write_headers(status, headers, alive)
175 range and defer_file_stream(range[0], range[1], io, body)
177 write_headers(status, headers, alive)
178 defer_file_stream(0, st.size, io, body)
182 # returns +nil+ on EOF, :wait_writable if the client blocks
183 def stream_file(sf) # +sf+ is a Rainbows::StreamFile object
185 sf.offset += (n = sendfile_nonblock(sf, sf.offset, sf.count))
186 0 == (sf.count -= n) and return sf.close
188 return :wait_writable
195 def defer_file_stream(offset, count, io, body)
196 sf = Rainbows::StreamFile.new(offset, count, io, body)
198 stream_file(sf) or return
204 # this alternates between a push and pull model from the pipe -> client
205 # to avoid having too much data in userspace on either end.
206 def stream_pipe(pipe)
207 case buf = pipe.tryread
209 if Array === write(buf)
210 # client is blocked on write, client will pull from pipe later
216 # continue looping...
218 # pipe blocked on read, let the pipe push to the client in the future
223 return pipe.close # nil