|
| 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 |
0 commit comments