Skip to content

Commit

Permalink
Add supervisor memory leak example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 28, 2025
1 parent 7e2c2f9 commit 9c5b417
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 6 deletions.
6 changes: 0 additions & 6 deletions examples/hello/falcon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,4 @@
# end

# append preload "preload.rb"

include Async::Container::Supervisor::Supervised
end

service "supervisor" do
include Falcon::Environment::Supervisor
end
9 changes: 9 additions & 0 deletions examples/supervisor/bake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def leak(size: 1024*1024)
require "async/http/internet/instance"

Sync do
response = Async::HTTP::Internet.get("http://localhost:8080/?leak=#{size}")
ensure
response&.finish
end
end
15 changes: 15 additions & 0 deletions examples/supervisor/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env falcon --verbose serve -c
# frozen_string_literal: true

leaks = []

run do |env|
request = Rack::Request.new(env)

if size = request.params["leak"]
Console.debug(self) {"Leaking #{size} bytes..."}
leaks << " " * size.to_i
end

[200, {}, ["Hello World"]]
end
62 changes: 62 additions & 0 deletions examples/supervisor/falcon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env falcon-host
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2024, by Samuel Williams.

require "falcon/environment/self_signed_tls"
require "falcon/environment/rack"
require "falcon/environment/supervisor"

class MemoryMonitor < Async::Container::Supervisor::MemoryMonitor
def memory_leak_detected(process_id, monitor)
connections = @processes[process_id]

# 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.
connections.each do |connection|
response = connection.call(do: :memory_dump, path: "memory-#{process_id}.json", timeout: 30)
Console.info(self, "Memory dumped...", response: response)

break
end

super
end
end

service "hello.localhost" do
include Falcon::Environment::SelfSignedTLS
include Falcon::Environment::Rack

scheme "http"
protocol {Async::HTTP::Protocol::HTTP}

endpoint do
Async::HTTP::Endpoint.for(scheme, "localhost", port: 9292, protocol: protocol)
end

count 4

url "http://localhost:8080"

endpoint do
::Async::HTTP::Endpoint.parse(url).with(**endpoint_options)
end

include Async::Container::Supervisor::Supervised
end

service "supervisor" do
include Falcon::Environment::Supervisor

monitors do
[
MemoryMonitor.new(interval: 10,
# Per-supervisor (cluster) limit:
total_size_limit: 80*1024*1024,
# Per-process limit:
maximum_size_limit: 20*1024*1024
)
]
end
end
6 changes: 6 additions & 0 deletions examples/supervisor/preload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

# $stderr.puts "Preloading..."
1 change: 1 addition & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# gem "protocol-rack", path: "../protocol-rack"
# gem "async-service", path: "../async-service"
# gem "io-stream", path: "../io-stream"
# gem "memory-leak", path: "../memory-leak"

# gem "fiber-profiler"

Expand Down

0 comments on commit 9c5b417

Please sign in to comment.