From 6769ffabf929aa0ef227895219ca9b82050a0f76 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 11 Feb 2025 16:04:49 +1300 Subject: [PATCH] WIP --- examples/hello/config.ru | 4 ++++ examples/hello/falcon.rb | 22 +++++++++++++++------- examples/hello/limited.rb | 17 +++++++++++++++++ lib/falcon/environment/server.rb | 18 +++++++++++++----- 4 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 examples/hello/limited.rb diff --git a/examples/hello/config.ru b/examples/hello/config.ru index 497bd751..36bf2f04 100755 --- a/examples/hello/config.ru +++ b/examples/hello/config.ru @@ -2,5 +2,9 @@ # frozen_string_literal: true run do |env| + Console.info(self, "Sleeping 10 seconds") + sleep(10) + Console.info(self, "Waking up") + [200, {}, ["Hello World"]] end diff --git a/examples/hello/falcon.rb b/examples/hello/falcon.rb index 0168b72f..0995f959 100755 --- a/examples/hello/falcon.rb +++ b/examples/hello/falcon.rb @@ -8,21 +8,29 @@ require "falcon/environment/rack" require "falcon/environment/supervisor" +require_relative "limited" + 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 + connection_semaphore {Async::Semaphore.new(1)} + + endpoint_options do + super().merge({ + wrapper: LimitedAcceptor.new(connection_semaphore) + }) + end + + count 1 - # append preload "preload.rb" + url "http://localhost:8080" - # Process will connect to supervisor to report statistics periodically, otherwise it would be killed. - # report :supervisor + endpoint do + ::Async::HTTP::Endpoint.parse(url).with(**endpoint_options) + end end service "supervisor" do diff --git a/examples/hello/limited.rb b/examples/hello/limited.rb new file mode 100644 index 00000000..a57c0ea8 --- /dev/null +++ b/examples/hello/limited.rb @@ -0,0 +1,17 @@ +class LimitedAcceptor < IO::Endpoint::Wrapper + def initialize(semaphore) + @semaphore = semaphore + end + + def async(&block) + ::Fiber.schedule(&block) + end + + def before_accept(server) + server.wait_readable + + Console.info(self, semaphore: @semaphore) {"Acquiring semaphore..."} + @semaphore.acquire + Console.info(self, semaphore: @semaphore) {"Acquired semaphore."} + end +end diff --git a/lib/falcon/environment/server.rb b/lib/falcon/environment/server.rb index 6b3d733e..8c5a1824 100644 --- a/lib/falcon/environment/server.rb +++ b/lib/falcon/environment/server.rb @@ -33,7 +33,11 @@ def count # Options to use when creating the container. def container_options - {restart: true, count: self.count, health_check_timeout: 30}.compact + { + restart: true, + count: self.count, + health_check_timeout: 30 + }.compact end # The host that this server will receive connections for. @@ -45,13 +49,17 @@ def timeout nil end + def endpoint_options + { + reuse_address: true, + timeout: timeout, + } + end + # The upstream endpoint that will handle incoming requests. # @returns [Async::HTTP::Endpoint] def endpoint - ::Async::HTTP::Endpoint.parse(url).with( - reuse_address: true, - timeout: timeout, - ) + ::Async::HTTP::Endpoint.parse(url).with(**endpoint_options) end def verbose