Skip to content

Commit 4809af7

Browse files
committed
Refine README.md
1 parent 86622fa commit 4809af7

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

README.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,7 @@ module Puma
18631863
def run
18641864
#...
18651865

1866-
# Set the behaviors for signals like `$ kill -s SIGTERM process_id` received.
1866+
# Set the behaviors for signals like `$ kill -s SIGTERM puma_process_id` received.
18671867
setup_signals # We will discuss this line later.
18681868

18691869
set_process_title
@@ -2018,6 +2018,7 @@ module Puma
20182018
begin
20192019
# This line will cause current thread waiting until a request arrives.
20202020
# So it will be the entry of every request!
2021+
# sockets: [#<IO:fd 23>, #<TCPServer:fd 22, AF_INET, 0.0.0.0, 3000>]
20212022
ios = IO.select sockets
20222023

20232024
ios.first.each do |sock|
@@ -2070,7 +2071,7 @@ module Puma
20702071
@reaper = nil
20712072

20722073
@mutex.synchronize do
2073-
@min.times { spawn_thread } # Puma spawned @min count threads.
2074+
@min.times { spawn_thread } # Puma spawns @min count threads.
20742075
end
20752076
end
20762077

@@ -2215,7 +2216,7 @@ In `#perform`, call `Rails::Server#start`. Then call `Rack::Server#start`.
22152216

22162217
Then call `Rack::Handler::Puma.run(YourProject::Application.new)`.
22172218

2218-
In `.run`, Puma will new a always running Thread for `ios = IO.select sockets`.
2219+
In `.run`, Puma will new a always running Thread for `ios = IO.select(#<TCPServer:fd 22, AF_INET, 0.0.0.0, 3000>)`.
22192220

22202221
Request is created from `ios` object.
22212222

@@ -2261,12 +2262,14 @@ After you added `thread.join`, you can see:
22612262
in console.
22622263

22632264
##### Example two
2265+
Try to run `test_thread_join2.rb`.
22642266
```ruby
2267+
# ./test_thread_join2.rb
22652268
arr = [
22662269
Thread.new { sleep 1 },
22672270
Thread.new do
22682271
sleep 5
2269-
puts 'I am arry[1]'
2272+
puts 'I am arr[1]'
22702273
end,
22712274
Thread.new { sleep 8}
22722275
]
@@ -2279,7 +2282,7 @@ arr.each { |thread| puts "~~~~~ #{thread}" }
22792282
22802283
puts Thread.list.size # returns 3 (because arr[0] is dead)
22812284
2282-
# arr[1].join # comment off to see differences
2285+
# arr[1].join # uncomment to see differences
22832286
22842287
arr.each { |thread| puts "~~~~~ #{thread}" }
22852288
@@ -2297,7 +2300,7 @@ module Puma
22972300
def run
22982301
#...
22992302
2300-
# Set the behaviors for signals like `$ kill -s SIGTERM process_id`.
2303+
# Set the behaviors for signals like `$ kill -s SIGTERM puma_process_id`.
23012304
setup_signals # Let's step into this line.
23022305
23032306
set_process_title
@@ -2307,7 +2310,7 @@ module Puma
23072310
# ...
23082311
end
23092312
2310-
# Set the behaviors for signals like `$ kill -s SIGTERM process_id`.
2313+
# Set the behaviors for signals like `$ kill -s SIGTERM puma_process_id`.
23112314
# Signal.list #=> {"EXIT"=>0, "HUP"=>1, "INT"=>2, "QUIT"=>3, "ILL"=>4, "TRAP"=>5, "IOT"=>6, "ABRT"=>6, "FPE"=>8, "KILL"=>9, "BUS"=>7, "SEGV"=>11, "SYS"=>31, "PIPE"=>13, "ALRM"=>14, "TERM"=>15, "URG"=>23, "STOP"=>19, "TSTP"=>20, "CONT"=>18, "CHLD"=>17, "CLD"=>17, "TTIN"=>21, "TTOU"=>22, "IO"=>29, "XCPU"=>24, "XFSZ"=>25, "VTALRM"=>26, "PROF"=>27, "WINCH"=>28, "USR1"=>10, "USR2"=>12, "PWR"=>30, "POLL"=>29}
23122315
# Press `Control + C` to quit means 'SIGINT'.
23132316
def setup_signals
@@ -2429,13 +2432,14 @@ module Puma
24292432
24302433
# The created @thread is the @thread in `stop` method below.
24312434
@thread = Thread.new {
2435+
# FYI, this is in the puma starting process.
24322436
# 'Thread.current.object_id' returns '70144220123860',
24332437
# which is the same as the 'Thread.current.object_id' in 'handle_servers' in Puma::Server#run
24342438
# def handle_servers
24352439
# begin
24362440
# # ...
24372441
# ensure
2438-
# # FYI, the 'ensure' part is executed after `$ kill -s SIGTERM process_id`.
2442+
# # FYI, the 'ensure' part is in the puma stopping process.
24392443
# puts "#{Thread.current.object_id}" # returns '70144220123860' too.
24402444
# end
24412445
# end
@@ -2450,8 +2454,8 @@ module Puma
24502454
# off the request queue before finally exiting.
24512455
def stop(sync=false)
24522456
# This line will set '@status = :stop',
2453-
# and cause `ios = IO.select sockets` in method `handle_servers` to return result.
2454-
# So the code after `ios = IO.select sockets` will be executed.
2457+
# and cause `ios = IO.select sockets` (in method `handle_servers`) to return result.
2458+
# So that the code after `ios = IO.select sockets` will be executed.
24552459
notify_safely(STOP_COMMAND) # Let's step into this line.
24562460
24572461
# 'Thread.current.object_id' returns '70144214949920',
@@ -2479,7 +2483,7 @@ module Puma
24792483
#...
24802484

24812485
while @status == :run
2482-
# After `notify_safely(STOP_COMMAND)` in main thread, this line will be executed and will return result.
2486+
# After `notify_safely(STOP_COMMAND)` in main thread, `ios = IO.select sockets` will return result.
24832487
# FYI, `@check, @notify = IO.pipe`.
24842488
# def notify_safely(message)
24852489
# @notify << message
@@ -2508,9 +2512,11 @@ module Puma
25082512

25092513
# ...
25102514
ensure
2515+
# FYI, the 'ensure' part is in the puma stopping process.
25112516
# 'Thread.current.object_id' returns '70144220123860',
25122517
# which is the same as the 'Thread.current.object_id' in 'Thread.new block' in Puma::Server#run
25132518
# @thread = Thread.new do
2519+
# # FYI, this is in the puma starting process.
25142520
# puts "#{Thread.current.object_id}" # returns '70144220123860'
25152521
# handle_servers
25162522
# end
@@ -2567,13 +2573,13 @@ module Puma
25672573
# @workers is an array.
25682574
# @workers.dup will not create new thread.
25692575
# @workers is an instance variable and will be changed when shutdown (by `@workers.delete th`).
2570-
# So ues dup.
2576+
# So ues @workers.dup here.
25712577
@workers.dup
25722578
end
25732579

25742580
# Wait for threads to finish without force shutdown.
25752581
threads.each do |thread|
2576-
thread.join # I guess `thread.join` means join the executing of thread to the calling (main) process.
2582+
thread.join
25772583
end
25782584

25792585
@spawned = 0
@@ -2586,11 +2592,11 @@ module Puma
25862592
@spawned = 0 # The count of @spawned threads.
25872593
@todo = [] # @todo is requests (in puma, it's Puma::Client instance) which need to be processed.
25882594
@min = Integer(min) # @min threads count
2589-
@block = block # block will be called in method `spawn_thread` to processed a request.
2595+
@block = block # block will be called in method `spawn_thread` to process a request.
25902596
@workers = []
25912597

25922598
@mutex.synchronize do
2593-
@min.times { spawn_thread } # Puma started @min count threads.
2599+
@min.times { spawn_thread } # Puma spawns @min count threads.
25942600
end
25952601
end
25962602

@@ -2619,7 +2625,7 @@ module Puma
26192625
end
26202626

26212627
# ...
2622-
# After `@not_empty.broadcast` in executed in '#shutdown', `not_empty` is waked up.
2628+
# After `@not_empty.broadcast` is executed in '#shutdown', `not_empty` is waked up.
26232629
# Ruby will continue to execute the next line here.
26242630
not_empty.wait mutex
26252631

@@ -2650,7 +2656,7 @@ end
26502656

26512657
So all the threads in the ThreadPool joined and finished.
26522658

2653-
And please look at the caller in block of `Signal.trap "SIGTERM"` below.
2659+
Let's inspect the caller in block of `Signal.trap "SIGTERM"` below.
26542660

26552661
```ruby
26562662
# .gems/puma-3.12.0/lib/puma/launcher.rb
@@ -2661,7 +2667,7 @@ module Puma
26612667
def run
26622668
#...
26632669

2664-
# Set the behaviors for signals like `$ kill -s SIGTERM process_id`.
2670+
# Set the behaviors for signals like `$ kill -s SIGTERM puma_process_id`.
26652671
setup_signals # Let's step into this line.
26662672

26672673
set_process_title
@@ -2679,7 +2685,7 @@ module Puma
26792685
begin
26802686
# After running `$ kill -s SIGTERM puma_process_id`, Ruby will execute the block of `Signal.trap "SIGTERM"`.
26812687
Signal.trap "SIGTERM" do
2682-
# I added `caller` to see the calling stack.
2688+
# I inspect `caller` to see the caller stack.
26832689
# caller: [
26842690
# "../gems/puma-3.12.0/lib/puma/single.rb:118:in `join'",
26852691
# "../gems/puma-3.12.0/lib/puma/single.rb:118:in `run'",

0 commit comments

Comments
 (0)