1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| | # -*- encoding: binary -*-
# :enddoc:
# used to wrap a BasicSocket to use with +q+ for all writes
# this is compatible with IO.select
class Rainbows::WriterThreadSpawn::Client < Struct.new(:to_io, :q, :thr)
include Rainbows::Response
include Rainbows::SocketProxy
include Rainbows::WorkerYield
CUR = {} # :nodoc:
def self.quit
g = Rainbows::G
CUR.delete_if do |t,q|
q << nil
g.tick
t.alive? ? t.join(0.01) : true
end until CUR.empty?
end
def queue_writer
until CUR.size < MAX
CUR.delete_if { |t,_|
t.alive? ? t.join(0) : true
}.size >= MAX and worker_yield
end
q = Queue.new
self.thr = Thread.new(to_io, q) do |io, q|
while response = q.shift
begin
arg1, arg2, arg3 = response
case arg1
when :body then write_body(io, arg2, arg3)
when :close
io.close unless io.closed?
break
else
io.write(arg1)
end
rescue => e
Rainbows::Error.write(io, e)
end
end
CUR.delete(Thread.current)
end
CUR[thr] = q
end
def write(buf)
(self.q ||= queue_writer) << buf
end
def queue_body(body, range)
(self.q ||= queue_writer) << [ :body, body, range ]
end
def close
if q
q << :close
else
to_io.close
end
end
def closed?
false
end
end
|