Skip to content

Commit

Permalink
Avoid having unused bound socket.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 2, 2024
1 parent 2ae47fc commit 8057adb
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/rackula/command/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def serve(endpoint, root)
config_path = root + @options[:config]

container.run do |instance|
Async do
Sync do
rack_app, _ = Rack::Builder.parse_file(config_path.to_s)
app = ::Falcon::Server.middleware(rack_app, verbose: @options[:verbose])
server = ::Falcon::Server.new(app, endpoint)
Expand All @@ -79,8 +79,12 @@ def serve(endpoint, root)
return container
end

def run(address, root)
endpoint = Async::HTTP::Endpoint.parse("http://localhost", port: address.ip_port, reuse_port: true)
def run(bound_endpoint, root)
# We need to determine the actual port we are bound to:
local_addresses = bound_endpoint.sockets.map(&:local_address)
address = local_addresses.first

endpoint = Async::HTTP::Endpoint.parse("http://localhost", bound_endpoint)

Console.logger.info(self) {"Setting up container to serve site on port #{address.ip_port}..."}
container = serve(endpoint, root)
Expand All @@ -96,10 +100,13 @@ def call

endpoint = ::IO::Endpoint.tcp("localhost", 0, reuse_port: true)

# We bind to a socket to generate a temporary port:
socket = Sync{endpoint.bind.first}

run(socket.local_address, Pathname.new(parent.root))
begin
bound_endpoint = endpoint.bound

run(bound_endpoint, Pathname.new(parent.root))
ensure
bound_endpoint&.close
end
end
end
end
Expand Down

0 comments on commit 8057adb

Please sign in to comment.