Skip to content

Commit 687a394

Browse files
committed
Updated puma example using shared socket.
1 parent 2080bc7 commit 687a394

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

examples/puma/application.rb

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,40 @@
44
require "async/container"
55
require "console"
66

7+
require "io/endpoint/host_endpoint"
8+
require "io/endpoint/bound_endpoint"
9+
710
# Console.logger.debug!
811

912
class Application < Async::Container::Controller
13+
def endpoint
14+
IO::Endpoint.tcp("0.0.0.0", 9292)
15+
end
16+
17+
def bound_socket
18+
bound = endpoint.bound
19+
20+
bound.sockets.each do |socket|
21+
socket.listen(Socket::SOMAXCONN)
22+
end
23+
24+
return bound
25+
end
26+
1027
def setup(container)
28+
@bound = bound_socket
29+
1130
container.spawn(name: "Web", restart: true) do |instance|
12-
# Replace the current process with Puma:
13-
# instance.exec("bundle", "exec", "puma", "-C", "puma.rb", ready: false)
31+
env = ENV.to_h
1432

15-
# Manage a child process of puma / puma workers:
16-
pid = ::Process.spawn("puma", "-C", "puma.rb")
17-
18-
instance.ready!
19-
20-
begin
21-
status = ::Process.wait2(pid)
22-
rescue Async::Container::Hangup
23-
Console.warn(self, "Restarting puma...")
24-
::Process.kill("USR1", pid)
25-
retry
26-
ensure
27-
::Process.kill("TERM", pid)
33+
@bound.sockets.each_with_index do |socket, index|
34+
env["PUMA_INHERIT_#{index}"] = "#{socket.fileno}:tcp://0.0.0.0:9292"
2835
end
36+
37+
instance.exec(env, "bundle", "exec", "puma", "-C", "puma.rb", ready: false)
2938
end
3039
end
3140
end
3241

33-
Application.new.run
42+
application = Application.new
43+
application.run

examples/puma/gems.locked

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ GEM
1919
fiber-local (1.1.0)
2020
fiber-storage
2121
fiber-storage (1.0.0)
22+
io-endpoint (0.14.0)
2223
io-event (1.7.5)
2324
json (2.9.1)
2425
nio4r (2.7.4)
@@ -32,6 +33,7 @@ PLATFORMS
3233

3334
DEPENDENCIES
3435
async-container!
36+
io-endpoint
3537
puma
3638
rack (~> 3)
3739

examples/puma/gems.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
source "https://rubygems.org"
22

33
gem "async-container", path: "../.."
4+
gem "io-endpoint"
45

56
gem "puma"
67
gem "rack", "~> 3"

0 commit comments

Comments
 (0)