Skip to content

Commit 9c5b417

Browse files
committed
Add supervisor memory leak example.
1 parent 7e2c2f9 commit 9c5b417

File tree

6 files changed

+93
-6
lines changed

6 files changed

+93
-6
lines changed

examples/hello/falcon.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,4 @@
2020
# end
2121

2222
# append preload "preload.rb"
23-
24-
include Async::Container::Supervisor::Supervised
25-
end
26-
27-
service "supervisor" do
28-
include Falcon::Environment::Supervisor
2923
end

examples/supervisor/bake.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def leak(size: 1024*1024)
2+
require "async/http/internet/instance"
3+
4+
Sync do
5+
response = Async::HTTP::Internet.get("http://localhost:8080/?leak=#{size}")
6+
ensure
7+
response&.finish
8+
end
9+
end

examples/supervisor/config.ru

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env falcon --verbose serve -c
2+
# frozen_string_literal: true
3+
4+
leaks = []
5+
6+
run do |env|
7+
request = Rack::Request.new(env)
8+
9+
if size = request.params["leak"]
10+
Console.debug(self) {"Leaking #{size} bytes..."}
11+
leaks << " " * size.to_i
12+
end
13+
14+
[200, {}, ["Hello World"]]
15+
end

examples/supervisor/falcon.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env falcon-host
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2019-2024, by Samuel Williams.
6+
7+
require "falcon/environment/self_signed_tls"
8+
require "falcon/environment/rack"
9+
require "falcon/environment/supervisor"
10+
11+
class MemoryMonitor < Async::Container::Supervisor::MemoryMonitor
12+
def memory_leak_detected(process_id, monitor)
13+
connections = @processes[process_id]
14+
15+
# Note that if you use a multi-threaded or hybrid container, there will be multiple connections per process. We break after the first successful dump.
16+
connections.each do |connection|
17+
response = connection.call(do: :memory_dump, path: "memory-#{process_id}.json", timeout: 30)
18+
Console.info(self, "Memory dumped...", response: response)
19+
20+
break
21+
end
22+
23+
super
24+
end
25+
end
26+
27+
service "hello.localhost" do
28+
include Falcon::Environment::SelfSignedTLS
29+
include Falcon::Environment::Rack
30+
31+
scheme "http"
32+
protocol {Async::HTTP::Protocol::HTTP}
33+
34+
endpoint do
35+
Async::HTTP::Endpoint.for(scheme, "localhost", port: 9292, protocol: protocol)
36+
end
37+
38+
count 4
39+
40+
url "http://localhost:8080"
41+
42+
endpoint do
43+
::Async::HTTP::Endpoint.parse(url).with(**endpoint_options)
44+
end
45+
46+
include Async::Container::Supervisor::Supervised
47+
end
48+
49+
service "supervisor" do
50+
include Falcon::Environment::Supervisor
51+
52+
monitors do
53+
[
54+
MemoryMonitor.new(interval: 10,
55+
# Per-supervisor (cluster) limit:
56+
total_size_limit: 80*1024*1024,
57+
# Per-process limit:
58+
maximum_size_limit: 20*1024*1024
59+
)
60+
]
61+
end
62+
end

examples/supervisor/preload.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2020-2024, by Samuel Williams.
5+
6+
# $stderr.puts "Preloading..."

gems.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# gem "protocol-rack", path: "../protocol-rack"
2323
# gem "async-service", path: "../async-service"
2424
# gem "io-stream", path: "../io-stream"
25+
# gem "memory-leak", path: "../memory-leak"
2526

2627
# gem "fiber-profiler"
2728

0 commit comments

Comments
 (0)