1 # -*- encoding: binary -*-
3 module Rainbows::XEpollThreadSpawn::Client
4 Rainbows.config!(self, :keepalive_timeout, :client_header_buffer_size)
6 ACCEPTORS = Rainbows::HttpServer::LISTENERS.dup
7 extend Rainbows::WorkerYield
9 def self.included(klass) # included in Rainbows::Client
10 max = Rainbows.server.worker_connections
11 ACCEPTORS.map! do |sock|
15 if io = sock.kgio_accept(klass)
19 worker_yield while N[0] >= max
21 Rainbows::Error.listen_loop(e)
22 end while Rainbows.alive
27 ep = SleepyPenguin::Epoll
29 IN = ep::IN | ep::ONESHOT
30 KATO = {}.compare_by_identity
34 LOCK.synchronize { clients = KATO.keys; KATO.clear }
35 clients.each { |io| io.closed? or io.shutdown }
37 @@last_expire = Rainbows.now
40 LOCK.synchronize { KATO[self] = @@last_expire }
45 LOCK.synchronize { KATO.delete self }
51 EP.wait(nil, 1000) { |_, obj| obj.epoll_run(buf) }
55 Rainbows::Error.listen_loop(e)
56 end while Rainbows.tick || N[0] > 0
57 Rainbows::JoinThreads.acceptors(ACCEPTORS)
61 return if ((now = Rainbows.now) - @@last_expire) < 1.0
62 if (ot = KEEPALIVE_TIMEOUT) >= 0
66 KATO.delete_if { |client, time| time < ot and defer << client }
68 defer.each { |io| io.closed? or io.close }
74 @hp = Rainbows::HttpParser.new
92 case kgio_tryread(CLIENT_HEADER_BUFFER_SIZE, buf)
97 env = @hp.add_parse(buf) and return spawn(env, @hp)
106 Thread.new { process_pipeline(env, hp) }
109 def pipeline_ready(hp)
110 hp.parse and return true
111 case buf = kgio_tryread(CLIENT_HEADER_BUFFER_SIZE)
116 hp.add_parse(buf) and return true