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::ONESHOT
10 OUT = SleepyPenguin::Epoll::OUT | SleepyPenguin::Epoll::ONESHOT
12 KATO = {}.compare_by_identity
13 Rainbows.at_quit { KATO.each_key(&:timeout!).clear }
14 Rainbows.config!(self, :keepalive_timeout)
16 @@last_expire = Rainbows.now
19 return if ((now = Rainbows.now) - @@last_expire) < 1.0
20 if (ot = KEEPALIVE_TIMEOUT) >= 0
22 KATO.delete_if { |client, time| time < ot and client.timeout! }
29 EP.wait(nil, 1000) { |_, obj| obj.epoll_run }
33 Rainbows::Error.listen_loop(e)
34 end while Rainbows.tick || Server.nr > 0
39 @wr_queue = [] # may contain String, ResponsePipe, and StreamFile objects
47 case rv = kgio_tryread(CLIENT_HEADER_BUFFER_SIZE, RBUF)
50 return if @wr_queue[0] || closed?
51 return hijacked if @hp.hijacked?
53 KATO[self] = @@last_expire if :headers == @state
54 return EP.set(self, IN)
57 end until :close == @state
59 rescue Errno::ECONNRESET
64 def app_call input # called by on_read()
65 @env['rack.input'] = input
66 @env['REMOTE_ADDR'] = kgio_addr
67 @hp.hijack_setup(self)
68 status, headers, body = APP.call(@env.merge!(RACK_DEFAULTS))
69 return hijacked if @hp.hijacked?
70 ev_write_response(status, headers, body, @hp.next?)
73 def write_response_path(status, headers, body, alive)
78 defer_file(status, headers, body, alive, io, st)
79 elsif st.socket? || st.pipe?
80 chunk = stream_response_headers(status, headers, alive, body)
81 return hijacked if nil == chunk
82 stream_response_body(body, io, chunk)
84 # char or block device... WTF?
85 write_response(status, headers, body, alive)
89 # used for streaming sockets and pipes
90 def stream_response_body(body, io, chunk)
91 pipe = (chunk ? Rainbows::Epoll::ResponseChunkPipe :
92 Rainbows::Epoll::ResponsePipe).new(io, self, body)
93 return @wr_queue << pipe if @wr_queue[0]
94 stream_pipe(pipe) or return
95 @wr_queue[0] or @wr_queue << ''.freeze
98 def ev_write_response(status, headers, body, alive)
99 @state = alive ? :headers : :close
100 if body.respond_to?(:to_path)
101 write_response_path(status, headers, body, alive)
103 write_response(status, headers, body, alive)
105 return hijacked if @hp.hijacked?
106 # try to read more if we didn't have to buffer writes
107 next_request if alive && 0 == @wr_queue.size
112 Server.decr # no other place to do this
121 # pipelined request (already in buffer)
123 return if @wr_queue[0] || closed?
124 return hijacked if @hp.hijacked?
125 close if :close == @state
139 EP.set(self, EPINOUT)
142 def on_deferred_write_complete
143 :close == @state and return close
148 msg = Rainbows::Error.response(e) and kgio_trywrite(msg) rescue nil
153 def write_deferred(obj)
154 Rainbows::StreamFile === obj ? stream_file(obj) : stream_pipe(obj)
157 # writes until our write buffer is empty or we block
158 # returns true if we're done writing everything
160 obj = @wr_queue.shift
162 case rv = String === obj ? kgio_trywrite(obj) : write_deferred(obj)
164 obj = @wr_queue.shift or return on_deferred_write_complete
167 when :wait_writable # Strings and StreamFiles only
168 @wr_queue.unshift(obj)
180 case rv = kgio_trywrite(buf)
186 @wr_queue << buf.dup # >3-word 1.9 strings are copy-on-write
187 return EP.set(self, OUT)
190 @wr_queue << buf.dup # >3-word 1.9 strings are copy-on-write
194 @wr_queue.each { |x| x.respond_to?(:close) and x.close rescue nil }
209 # Rack apps should not hijack here, but they may...
210 def defer_file(status, headers, body, alive, io, st)
211 if r = sendfile_range(status, headers)
212 status, headers, range = r
213 write_headers(status, headers, alive, body) or return hijacked
214 range and defer_file_stream(range[0], range[1], io, body)
216 write_headers(status, headers, alive, body) or return hijacked
217 defer_file_stream(0, st.size, io, body)
221 # returns +nil+ on EOF, :wait_writable if the client blocks
222 def stream_file(sf) # +sf+ is a Rainbows::StreamFile object
223 case n = trysendfile(sf, sf.offset, sf.count)
226 0 == (sf.count -= n) and return sf.close
228 return n # :wait_writable or nil
235 def defer_file_stream(offset, count, io, body)
236 sf = Rainbows::StreamFile.new(offset, count, io, body)
238 stream_file(sf) or return
244 # this alternates between a push and pull model from the pipe -> client
245 # to avoid having too much data in userspace on either end.
246 def stream_pipe(pipe)
247 case buf = pipe.tryread
251 # client is blocked on write, client will pull from pipe later
257 # continue looping...
259 # pipe blocked on read, let the pipe push to the client in the future
264 return pipe.close # nil