-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.rb
55 lines (51 loc) · 1.14 KB
/
example.rb
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
require 'net/http'
require 'http-cage'
require 'colorize'
# Client making a 10 seconds HTTP call.
class SlowCall
def get
request = "http request: "
begin
sleep 0.5
puts request + "started".yellow
http = Net::HTTP.new('httpbin.org')
get = Net::HTTP::Get.new('/delay/10')
http.request(get).body
puts request + "completed".green
rescue Net::ReadTimeout
puts request + "timed out".red
rescue Net::OpenTimeout
puts request + "timed out".red
rescue Interrupt
puts request + "shutdown".green
end
end
end
# Server running with a timeout of 5 seconds.
class Server
def run
server = "server: "
trap("INT") do
puts server + "shutdown".green
exit
end
puts server + "booting".green
loop do
worker = fork do
SlowCall.new().get
end
begin
Timeout.timeout(10) do
puts server + "ready".yellow
Process.wait worker
next
end
rescue Timeout::Error
puts server + "down".red
Process.kill("INT", worker)
Process.wait(worker)
break
end
end
end
end