1 # -*- encoding: binary -*-
3 module Rainbows::XEpollThreadSpawn::Client
4 HBUFSIZ = Rainbows.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::ET | ep::ONESHOT
31 KATO.compare_by_identity if KATO.respond_to?(:compare_by_identity)
33 @@last_expire = Time.now
36 LOCK.synchronize { KATO[self] = @@last_expire }
41 LOCK.synchronize { KATO.delete self }
47 EP.wait(nil, 1000) { |_, obj| obj.epoll_run(buf) }
51 Rainbows::Error.listen_loop(e)
52 end while Rainbows.tick || N[0] > 0
53 Rainbows::JoinThreads.acceptors(ACCEPTORS)
57 return if ((now = Time.now) - @@last_expire) < 1.0
58 if (ot = Rainbows.keepalive_timeout) >= 0
62 KATO.delete_if { |client, time| time < ot and defer << client }
64 defer.each { |io| io.closed? or io.close }
70 @hp = Rainbows::HttpParser.new
88 case kgio_tryread(HBUFSIZ, buf)
94 env = @hp.parse and return spawn(env, @hp)
103 Thread.new { process_pipeline(env, hp) }
106 def pipeline_ready(hp)
107 hp.parse and return true
108 case buf = kgio_tryread(HBUFSIZ)
114 hp.parse and return true