Class: Falcon::Server

Inherits:
Async::HTTP::Server
  • Object
show all
Defined in:
lib/falcon/server.rb

Overview

A server listening on a specific endpoint, hosting a specific middleware.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments, utilization_registry: nil, **options) ⇒ Server

Initialize the server and set up statistics tracking.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/falcon/server.rb', line 45

def initialize(*arguments, utilization_registry: nil, **options)
	super(*arguments, **options)
	
	utilization_registry ||= Async::Utilization::Registry.new
	
	# Get metric references for utilization tracking:
	@connections_total_metric = utilization_registry.metric(:connections_total)
	@connections_active_metric = utilization_registry.metric(:connections_active)
	@requests_total_metric = utilization_registry.metric(:requests_total)
	@requests_active_metric = utilization_registry.metric(:requests_active)
end

Class Method Details

.middleware(rack_app, verbose: false, cache: true) ⇒ Object

Wrap a rack application into a middleware suitable the server.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/falcon/server.rb', line 24

def self.middleware(rack_app, verbose: false, cache: true)
	::Protocol::HTTP::Middleware.build do
		if verbose
			use Middleware::Verbose
		end
		
		if cache
			use Async::HTTP::Cache::General
		end
		
		use ::Protocol::HTTP::ContentEncoding
		
		use ::Protocol::Rack::Adapter
		run rack_app
	end
end

Instance Method Details

#acceptObject

Accept a new connection and track connection statistics.



58
59
60
61
62
63
# File 'lib/falcon/server.rb', line 58

def accept(...)
	@connections_total_metric.increment
	@connections_active_metric.track do
		super
	end
end

#callObject

Handle a request and track request statistics.

Uses manual increment/decrement so requests_active stays elevated until the response body is closed (including rack.response_finished). The Body::RequestFinished wrapper runs the decrement after the body closes, so response_finished callbacks are counted as active.



71
72
73
74
75
76
# File 'lib/falcon/server.rb', line 71

def call(...)
	@requests_total_metric.increment
	@requests_active_metric.increment
	
	return Body::RequestFinished.wrap(super, @requests_active_metric)
end

#statistics_stringObject

Generates a human-readable string representing the current statistics.

e.g. ‘C=23/3.42K R=2/3.42K L=0.273`

This can be interpreted as:

  • ‘C=23/3.42K` - The number of connections currently open and the total number of connections accepted.

  • ‘R=2/3.42K` - The number of requests currently being processed and the total number of requests received.

  • ‘L=0.273` - The average scheduler load of the server, where 0.0 is idle and 1.0 is fully loaded.



89
90
91
# File 'lib/falcon/server.rb', line 89

def statistics_string
	"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}"
end