forked from godfat/ruby-server-exp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ru
97 lines (88 loc) · 2.32 KB
/
config.ru
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'timeout'
require 'socket'
use Rack::ContentType
use Rack::ContentLength
OK = [200, {}, ["OK\n"]]
HOST = '127.0.0.1'
PORT = 12345
LATENCY = "GET /latency HTTP/1.0\r\n\r\n"
THROUGH = "GET /through HTTP/1.0\r\n\r\n"
map '/cpu' do
run lambda{ |env|
work = lambda{
begin
timeout(0.5){ true while true }
rescue Timeout::Error
OK
end
}
if Fiber.respond_to?(:current)
if defined?(EM)
f = Fiber.current
r = nil
EM.defer(lambda{ r = work.call }, f.method(:resume))
Fiber.yield
r
else
f = Fiber.current
r = nil
Thread.new{
r = work.call
# tell rainbows to resume us in main thread
Rainbows::Fiber::ZZ[f] = Time.now - 1
}
Fiber.yield
r
end
else
work.call
end
}
end
class SlowApp
def initialize get
@get = get
@connection = Class.new(EM::Connection){
singleton_class.module_eval{ attr_accessor :get }
self.get = get
def initialize ; @fiber, @data = Fiber.current, []; end
def post_init ; send_data(self.class.get) ; end
def unbind ; @fiber.resume(@data) ; end
def receive_data data; @data << data ; end
# we rely on the remote server closes the connection here
} if defined?(EM)
end
def call env
# only eventmachine + fiber spawn falls here
if defined?(EM) && Fiber.respond_to?(:current)
EM.connect(HOST, PORT, @connection)
[200, {}, ["<pre>#{Fiber.yield.map(&:size).inject(&:+)}</pre>"]]
else # eventmachine + thread should use normal socket
sock = if defined?(FiberTCPSocket)
FiberTCPSocket
else
TCPSocket
end.new(HOST, PORT)
begin
sock.write(@get)
buf = []
sock.each{ |data| # for FiberTCPSocket, it would call Fiber.yield
if defined?(FiberTCPSocket)
buf << data.dup # rainbows is reusing buf, so we dup here
else
buf << data
end
}
[200, {}, ["<pre>#{buf.map(&:size).inject(&:+)}</pre>"]]
ensure
sock.close
end
end
end
end
map '/latency' do
run SlowApp.new(LATENCY)
end
map '/through' do
run SlowApp.new(THROUGH)
end