Skip to content

Commit bcac237

Browse files
Add support for async-utilization. (#338)
1 parent 9e66dc8 commit bcac237

6 files changed

Lines changed: 85 additions & 24 deletions

File tree

examples/utilization/falcon.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env async-service
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2025, by Samuel Williams.
6+
7+
require "falcon/environment/server"
8+
require "async/service/supervisor/supervised"
9+
require "async/service/supervisor/environment"
10+
require "async/service/supervisor/utilization_monitor"
11+
12+
# A simple Rack application that demonstrates utilization monitoring.
13+
class SimpleApp
14+
def call(env)
15+
# Simulate some work
16+
sleep(rand * 0.1)
17+
18+
return [200, {"content-type" => "text/plain"}, ["Hello, World!"]]
19+
end
20+
end
21+
22+
service "web" do
23+
include Falcon::Environment::Server
24+
include Async::Service::Supervisor::Supervised
25+
26+
# Define the middleware stack for this server
27+
middleware do
28+
Falcon::Server.middleware(SimpleApp.new, verbose: false, cache: false)
29+
end
30+
31+
# Define the utilization schema for this service
32+
utilization_schema do
33+
{
34+
connections_total: :u64,
35+
connections_active: :u32,
36+
requests_total: :u64,
37+
requests_active: :u32,
38+
}
39+
end
40+
end
41+
42+
service "supervisor" do
43+
include Async::Service::Supervisor::Environment
44+
45+
# Configure the utilization monitor
46+
monitors do
47+
[
48+
Async::Service::Supervisor::UtilizationMonitor.new(
49+
path: File.expand_path("utilization.shm", __dir__),
50+
interval: 5.0
51+
)
52+
]
53+
end
54+
end

examples/utilization/gems.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "falcon", path: "../../"
6+
gem "async-service-supervisor", "~> 0.12"
7+
gem "async-utilization", "~> 0.3"

falcon.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
3131
spec.add_dependency "async-http", "~> 0.75"
3232
spec.add_dependency "async-http-cache", "~> 0.4"
3333
spec.add_dependency "async-service", "~> 0.19"
34+
spec.add_dependency "async-utilization", "~> 0.3"
3435
spec.add_dependency "bundler"
3536
spec.add_dependency "localhost", "~> 1.1"
3637
spec.add_dependency "openssl", ">= 3.0"

lib/falcon/environment/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def client_endpoint
7272
# @parameter endpoint [IO::Endpoint] The endpoint to bind to.
7373
# @returns [Falcon::Server] The server instance.
7474
def make_server(endpoint)
75-
Falcon::Server.new(self.middleware, endpoint, protocol: self.endpoint.protocol, scheme: self.endpoint.scheme)
75+
Falcon::Server.new(self.middleware, endpoint, protocol: self.endpoint.protocol, scheme: self.endpoint.scheme, utilization_registry: self[:utilization_registry])
7676
end
7777
end
7878
end

lib/falcon/server.rb

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require "protocol/http/content_encoding"
1010

1111
require "async/http/cache"
12+
require "async/utilization"
1213
require_relative "middleware/verbose"
1314
require "protocol/rack"
1415

@@ -37,38 +38,35 @@ def self.middleware(rack_app, verbose: false, cache: true)
3738
end
3839

3940
# Initialize the server and set up statistics tracking.
40-
def initialize(...)
41-
super
41+
#
42+
# @parameter utilization_registry [Registry, nil] The utilization registry to use for metrics tracking.
43+
# If nil, a new registry instance is created.
44+
def initialize(*arguments, utilization_registry: nil, **options)
45+
super(*arguments, **options)
4246

43-
@accept_count = 0
44-
@connection_count = 0
47+
utilization_registry ||= Async::Utilization::Registry.new
4548

46-
@request_count = 0
47-
@active_count = 0
49+
# Get metric references for utilization tracking:
50+
@connections_total_metric = utilization_registry.metric(:connections_total)
51+
@connections_active_metric = utilization_registry.metric(:connections_active)
52+
@requests_total_metric = utilization_registry.metric(:requests_total)
53+
@requests_active_metric = utilization_registry.metric(:requests_active)
4854
end
4955

50-
attr :request_count
51-
attr :accept_count
52-
attr :connect_count
53-
5456
# Accept a new connection and track connection statistics.
5557
def accept(...)
56-
@accept_count += 1
57-
@connection_count += 1
58-
59-
super
60-
ensure
61-
@connection_count -= 1
58+
@connections_total_metric.increment
59+
@connections_active_metric.track do
60+
super
61+
end
6262
end
6363

6464
# Handle a request and track request statistics.
6565
def call(...)
66-
@request_count += 1
67-
@active_count += 1
68-
69-
super
70-
ensure
71-
@active_count -= 1
66+
@requests_total_metric.increment
67+
@requests_active_metric.track do
68+
super
69+
end
7270
end
7371

7472
# Generates a human-readable string representing the current statistics.
@@ -83,7 +81,7 @@ def call(...)
8381
#
8482
# @returns [String] A string representing the current statistics.
8583
def statistics_string
86-
"C=#{format_count @connection_count}/#{format_count @accept_count} R=#{format_count @active_count}/#{format_count @request_count}"
84+
"C=#{format_count @connections_active_metric.value}/#{format_count @connections_total_metric.value} R=#{format_count @requests_active_metric.value}/#{format_count @requests_total_metric.value}"
8785
end
8886

8987
private

releases.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- **Breaking**: Drop dependency on `async-container-supervisor`, you should migrate to `async-service-supervisor` instead.
66
- **Breaking**: Remove support for legacy environments, including `Falcon::Configuration`, now using `Async::Service::Configuration` directly.
77
- **Breaking**: `bake falcon:supervisor:restart` removed – superceeded by `async:service:supervisor:restart`.
8+
- Add support for `async-utilization` metrics.
89

910
## v0.54.1
1011

0 commit comments

Comments
 (0)