Skip to content

Commit 6969957

Browse files
committed
Add preforking model for a more solid concurrency
1 parent ec1deb0 commit 6969957

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

config.ru

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class App
22
def call(env)
33
if env['PATH_INFO'] == '/'
4-
[200, { 'Content-Type' => 'text/html' }, ['Hello from the rack application inside config.ru.']]
4+
[200, { 'Content-Type' => 'text/html' }, ["Hello from the rack application inside config.ru with PID: #{Process.pid}\n"]]
55
else
66
[404, { 'Content-Type' => 'text/html' }, ['Boo.']]
77
end

hyperloop.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,30 @@ def initialize port
88
@server = TCPServer.new port
99
end
1010

11+
def prefork
12+
3.times do
13+
fork do
14+
start
15+
end
16+
end
17+
18+
waitall
19+
end
20+
21+
def waitall
22+
Process.waitall
23+
24+
rescue Interrupt
25+
puts "\nKilled all. No zombies."
26+
end
27+
1128
def start
1229
loop do
1330
socket = @server.accept
1431
Thread.new { SocketHandler.new(socket).process }
1532
end
33+
34+
rescue Interrupt
1635
end
1736
end
1837

@@ -95,4 +114,4 @@ def close_socket
95114

96115
server = Hyperloop.new 3000
97116
puts "Starting Hyperloop server on port 3000"
98-
server.start
117+
server.prefork

0 commit comments

Comments
 (0)