puma 4.3.8 → 5.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puma might be problematic. Click here for more details.

Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +58 -21
  3. data/LICENSE +23 -20
  4. data/README.md +17 -11
  5. data/docs/deployment.md +3 -1
  6. data/docs/fork_worker.md +31 -0
  7. data/docs/jungle/README.md +13 -0
  8. data/{tools → docs}/jungle/rc.d/README.md +0 -0
  9. data/{tools → docs}/jungle/rc.d/puma +0 -0
  10. data/{tools → docs}/jungle/rc.d/puma.conf +0 -0
  11. data/{tools → docs}/jungle/upstart/README.md +0 -0
  12. data/{tools → docs}/jungle/upstart/puma-manager.conf +0 -0
  13. data/{tools → docs}/jungle/upstart/puma.conf +0 -0
  14. data/docs/signals.md +1 -0
  15. data/docs/systemd.md +1 -63
  16. data/ext/puma_http11/PumaHttp11Service.java +2 -4
  17. data/ext/puma_http11/extconf.rb +3 -2
  18. data/ext/puma_http11/http11_parser.c +1 -3
  19. data/ext/puma_http11/http11_parser.rl +1 -3
  20. data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
  21. data/ext/puma_http11/puma_http11.c +2 -38
  22. data/lib/puma.rb +4 -0
  23. data/lib/puma/app/status.rb +16 -5
  24. data/lib/puma/binder.rb +62 -60
  25. data/lib/puma/cli.rb +7 -15
  26. data/lib/puma/client.rb +35 -32
  27. data/lib/puma/cluster.rb +179 -74
  28. data/lib/puma/configuration.rb +30 -42
  29. data/lib/puma/const.rb +2 -3
  30. data/lib/puma/control_cli.rb +27 -17
  31. data/lib/puma/detect.rb +8 -0
  32. data/lib/puma/dsl.rb +70 -34
  33. data/lib/puma/io_buffer.rb +9 -2
  34. data/lib/puma/jruby_restart.rb +0 -58
  35. data/lib/puma/launcher.rb +41 -29
  36. data/lib/puma/minissl.rb +13 -8
  37. data/lib/puma/null_io.rb +1 -1
  38. data/lib/puma/plugin.rb +1 -10
  39. data/lib/puma/rack/builder.rb +0 -4
  40. data/lib/puma/reactor.rb +6 -1
  41. data/lib/puma/runner.rb +5 -34
  42. data/lib/puma/server.rb +70 -190
  43. data/lib/puma/single.rb +7 -64
  44. data/lib/puma/state_file.rb +5 -2
  45. data/lib/puma/thread_pool.rb +85 -47
  46. data/lib/rack/handler/puma.rb +1 -3
  47. data/tools/{docker/Dockerfile → Dockerfile} +0 -0
  48. metadata +20 -24
  49. data/docs/tcp_mode.md +0 -96
  50. data/ext/puma_http11/io_buffer.c +0 -155
  51. data/ext/puma_http11/org/jruby/puma/IOBuffer.java +0 -72
  52. data/lib/puma/tcp_logger.rb +0 -41
  53. data/tools/jungle/README.md +0 -19
  54. data/tools/jungle/init.d/README.md +0 -61
  55. data/tools/jungle/init.d/puma +0 -421
  56. data/tools/jungle/init.d/run-puma +0 -18
data/lib/puma/single.rb CHANGED
@@ -14,11 +14,9 @@ module Puma
14
14
  # that this inherits from.
15
15
  class Single < Runner
16
16
  def stats
17
- b = @server.backlog || 0
18
- r = @server.running || 0
19
- t = @server.pool_capacity || 0
20
- m = @server.max_threads || 0
21
- %Q!{ "started_at": "#{@started_at.utc.iso8601}", "backlog": #{b}, "running": #{r}, "pool_capacity": #{t}, "max_threads": #{m} }!
17
+ {
18
+ started_at: @started_at.utc.iso8601
19
+ }.merge(@server.stats)
22
20
  end
23
21
 
24
22
  def restart
@@ -39,64 +37,10 @@ module Puma
39
37
  @server.stop(true) if @server
40
38
  end
41
39
 
42
- def jruby_daemon?
43
- daemon? and Puma.jruby?
44
- end
45
-
46
- def jruby_daemon_start
47
- require 'puma/jruby_restart'
48
- JRubyRestart.daemon_start(@restart_dir, @launcher.restart_args)
49
- end
50
-
51
40
  def run
52
- already_daemon = false
53
-
54
- if jruby_daemon?
55
- require 'puma/jruby_restart'
56
-
57
- if JRubyRestart.daemon?
58
- # load and bind before redirecting IO so errors show up on stdout/stderr
59
- load_and_bind
60
- redirect_io
61
- end
62
-
63
- already_daemon = JRubyRestart.daemon_init
64
- end
65
-
66
41
  output_header "single"
67
42
 
68
- if jruby_daemon?
69
- if already_daemon
70
- JRubyRestart.perm_daemonize
71
- else
72
- pid = nil
73
-
74
- Signal.trap "SIGUSR2" do
75
- log "* Started new process #{pid} as daemon..."
76
-
77
- # Must use exit! so we don't unwind and run the ensures
78
- # that will be run by the new child (such as deleting the
79
- # pidfile)
80
- exit!(true)
81
- end
82
-
83
- Signal.trap "SIGCHLD" do
84
- log "! Error starting new process as daemon, exiting"
85
- exit 1
86
- end
87
-
88
- jruby_daemon_start
89
- sleep
90
- end
91
- else
92
- if daemon?
93
- log "* Daemonizing..."
94
- Process.daemon(true)
95
- redirect_io
96
- end
97
-
98
- load_and_bind
99
- end
43
+ load_and_bind
100
44
 
101
45
  Plugins.fire_background
102
46
 
@@ -106,10 +50,9 @@ module Puma
106
50
 
107
51
  @server = server = start_server
108
52
 
109
- unless daemon?
110
- log "Use Ctrl-C to stop"
111
- redirect_io
112
- end
53
+
54
+ log "Use Ctrl-C to stop"
55
+ redirect_io
113
56
 
114
57
  @launcher.events.fire_on_booted!
115
58
 
@@ -8,8 +8,11 @@ module Puma
8
8
  @options = {}
9
9
  end
10
10
 
11
- def save(path)
12
- File.write path, YAML.dump(@options)
11
+ def save(path, permission = nil)
12
+ File.open(path, "w") do |file|
13
+ file.chmod(permission) if permission
14
+ file.write(YAML.dump(@options))
15
+ end
13
16
  end
14
17
 
15
18
  def load(path)
@@ -47,6 +47,7 @@ module Puma
47
47
  @shutdown = false
48
48
 
49
49
  @trim_requested = 0
50
+ @out_of_band_pending = false
50
51
 
51
52
  @workers = []
52
53
 
@@ -54,7 +55,10 @@ module Puma
54
55
  @reaper = nil
55
56
 
56
57
  @mutex.synchronize do
57
- @min.times { spawn_thread }
58
+ @min.times do
59
+ spawn_thread
60
+ @not_full.wait(@mutex)
61
+ end
58
62
  end
59
63
 
60
64
  @clean_thread_locals = false
@@ -62,6 +66,7 @@ module Puma
62
66
 
63
67
  attr_reader :spawned, :trim_requested, :waiting
64
68
  attr_accessor :clean_thread_locals
69
+ attr_accessor :out_of_band_hook
65
70
 
66
71
  def self.clean_thread_locals
67
72
  Thread.current.keys.each do |key| # rubocop: disable Performance/HashEachMethods
@@ -72,13 +77,17 @@ module Puma
72
77
  # How many objects have yet to be processed by the pool?
73
78
  #
74
79
  def backlog
75
- @mutex.synchronize { @todo.size }
80
+ with_mutex { @todo.size }
76
81
  end
77
82
 
78
83
  def pool_capacity
79
84
  waiting + (@max - spawned)
80
85
  end
81
86
 
87
+ def busy_threads
88
+ with_mutex { @spawned - @waiting + @todo.size }
89
+ end
90
+
82
91
  # :nodoc:
83
92
  #
84
93
  # Must be called with @mutex held!
@@ -99,48 +108,37 @@ module Puma
99
108
  while true
100
109
  work = nil
101
110
 
102
- continue = true
103
-
104
111
  mutex.synchronize do
105
112
  while todo.empty?
106
113
  if @trim_requested > 0
107
114
  @trim_requested -= 1
108
- continue = false
109
- not_full.signal
110
- break
111
- end
112
-
113
- if @shutdown
114
- continue = false
115
- break
115
+ @spawned -= 1
116
+ @workers.delete th
117
+ Thread.exit
116
118
  end
117
119
 
118
120
  @waiting += 1
121
+ if @out_of_band_pending && trigger_out_of_band_hook
122
+ @out_of_band_pending = false
123
+ end
119
124
  not_full.signal
120
125
  not_empty.wait mutex
121
126
  @waiting -= 1
122
127
  end
123
128
 
124
- work = todo.shift if continue
129
+ work = todo.shift
125
130
  end
126
131
 
127
- break unless continue
128
-
129
132
  if @clean_thread_locals
130
133
  ThreadPool.clean_thread_locals
131
134
  end
132
135
 
133
136
  begin
134
- block.call(work, *extra)
137
+ @out_of_band_pending = true if block.call(work, *extra)
135
138
  rescue Exception => e
136
139
  STDERR.puts "Error reached top of thread-pool: #{e.message} (#{e.class})"
137
140
  end
138
141
  end
139
-
140
- mutex.synchronize do
141
- @spawned -= 1
142
- @workers.delete th
143
- end
144
142
  end
145
143
 
146
144
  @workers << th
@@ -150,9 +148,30 @@ module Puma
150
148
 
151
149
  private :spawn_thread
152
150
 
151
+ def trigger_out_of_band_hook
152
+ return false unless out_of_band_hook && out_of_band_hook.any?
153
+
154
+ # we execute on idle hook when all threads are free
155
+ return false unless @spawned == @waiting
156
+
157
+ out_of_band_hook.each(&:call)
158
+ true
159
+ rescue Exception => e
160
+ STDERR.puts "Exception calling out_of_band_hook: #{e.message} (#{e.class})"
161
+ true
162
+ end
163
+
164
+ private :trigger_out_of_band_hook
165
+
166
+ def with_mutex(&block)
167
+ @mutex.owned? ?
168
+ yield :
169
+ @mutex.synchronize(&block)
170
+ end
171
+
153
172
  # Add +work+ to the todo list for a Thread to pickup and process.
154
173
  def <<(work)
155
- @mutex.synchronize do
174
+ with_mutex do
156
175
  if @shutdown
157
176
  raise "Unable to add work while shutting down"
158
177
  end
@@ -193,11 +212,8 @@ module Puma
193
212
  # method would not block and another request would be added into the reactor
194
213
  # by the server. This would continue until a fully bufferend request
195
214
  # makes it through the reactor and can then be processed by the thread pool.
196
- #
197
- # Returns the current number of busy threads, or +nil+ if shutting down.
198
- #
199
215
  def wait_until_not_full
200
- @mutex.synchronize do
216
+ with_mutex do
201
217
  while true
202
218
  return if @shutdown
203
219
 
@@ -205,21 +221,40 @@ module Puma
205
221
  # is work queued that cannot be handled by waiting
206
222
  # threads, then accept more work until we would
207
223
  # spin up the max number of threads.
208
- busy_threads = @spawned - @waiting + @todo.size
209
- return busy_threads if @max > busy_threads
224
+ return if busy_threads < @max
210
225
 
211
226
  @not_full.wait @mutex
212
227
  end
213
228
  end
214
229
  end
215
230
 
216
- # If too many threads are in the pool, tell one to finish go ahead
231
+ def wait_for_less_busy_worker(delay_s)
232
+ # Ruby MRI does GVL, this can result
233
+ # in processing contention when multiple threads
234
+ # (requests) are running concurrently
235
+ return unless Puma.mri?
236
+ return unless delay_s > 0
237
+
238
+ with_mutex do
239
+ return if @shutdown
240
+
241
+ # do not delay, if we are not busy
242
+ return unless busy_threads > 0
243
+
244
+ # this will be signaled once a request finishes,
245
+ # which can happen earlier than delay
246
+ @not_full.wait @mutex, delay_s
247
+ end
248
+ end
249
+
250
+ # If there are any free threads in the pool, tell one to go ahead
217
251
  # and exit. If +force+ is true, then a trim request is requested
218
252
  # even if all threads are being utilized.
219
253
  #
220
254
  def trim(force=false)
221
- @mutex.synchronize do
222
- if (force or @waiting > 0) and @spawned - @trim_requested > @min
255
+ with_mutex do
256
+ free = @waiting - @todo.size
257
+ if (force or free > 0) and @spawned - @trim_requested > @min
223
258
  @trim_requested += 1
224
259
  @not_empty.signal
225
260
  end
@@ -229,7 +264,7 @@ module Puma
229
264
  # If there are dead threads in the pool make them go away while decreasing
230
265
  # spawned counter so that new healthy threads could be created again.
231
266
  def reap
232
- @mutex.synchronize do
267
+ with_mutex do
233
268
  dead_workers = @workers.reject(&:alive?)
234
269
 
235
270
  dead_workers.each do |worker|
@@ -281,10 +316,14 @@ module Puma
281
316
  end
282
317
 
283
318
  # Tell all threads in the pool to exit and wait for them to finish.
319
+ # Wait +timeout+ seconds then raise +ForceShutdown+ in remaining threads.
320
+ # Next, wait an extra +grace+ seconds then force-kill remaining threads.
321
+ # Finally, wait +kill_grace+ seconds for remaining threads to exit.
284
322
  #
285
323
  def shutdown(timeout=-1)
286
- threads = @mutex.synchronize do
324
+ threads = with_mutex do
287
325
  @shutdown = true
326
+ @trim_requested = @spawned
288
327
  @not_empty.broadcast
289
328
  @not_full.broadcast
290
329
 
@@ -298,27 +337,26 @@ module Puma
298
337
  # Wait for threads to finish without force shutdown.
299
338
  threads.each(&:join)
300
339
  else
301
- # Wait for threads to finish after n attempts (+timeout+).
302
- # If threads are still running, it will forcefully kill them.
303
- timeout.times do
304
- threads.delete_if do |t|
305
- t.join 1
306
- end
307
-
308
- if threads.empty?
309
- break
310
- else
311
- sleep 1
340
+ join = ->(inner_timeout) do
341
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
342
+ threads.reject! do |t|
343
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
344
+ t.join inner_timeout - elapsed
312
345
  end
313
346
  end
314
347
 
348
+ # Wait +timeout+ seconds for threads to finish.
349
+ join.call(timeout)
350
+
351
+ # If threads are still running, raise ForceShutdown and wait to finish.
315
352
  threads.each do |t|
316
353
  t.raise ForceShutdown
317
354
  end
355
+ join.call(SHUTDOWN_GRACE_TIME)
318
356
 
319
- threads.each do |t|
320
- t.join SHUTDOWN_GRACE_TIME
321
- end
357
+ # If threads are _still_ running, forcefully kill them and wait to finish.
358
+ threads.each(&:kill)
359
+ join.call(1)
322
360
  end
323
361
 
324
362
  @spawned = 0
@@ -30,8 +30,6 @@ module Rack
30
30
  end
31
31
 
32
32
  conf = ::Puma::Configuration.new(options, default_options) do |user_config, file_config, default_config|
33
- user_config.quiet
34
-
35
33
  if options.delete(:Verbose)
36
34
  app = Rack::CommonLogger.new(app, STDOUT)
37
35
  end
@@ -61,7 +59,7 @@ module Rack
61
59
  conf
62
60
  end
63
61
 
64
- def self.run(app, options = {})
62
+ def self.run(app, **options)
65
63
  conf = self.config(app, options)
66
64
 
67
65
  events = options.delete(:Silent) ? ::Puma::Events.strings : ::Puma::Events.stdio
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puma
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.8
4
+ version: 5.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Phoenix
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-11 00:00:00.000000000 Z
11
+ date: 2020-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r
@@ -45,15 +45,22 @@ files:
45
45
  - bin/pumactl
46
46
  - docs/architecture.md
47
47
  - docs/deployment.md
48
+ - docs/fork_worker.md
48
49
  - docs/images/puma-connection-flow-no-reactor.png
49
50
  - docs/images/puma-connection-flow.png
50
51
  - docs/images/puma-general-arch.png
52
+ - docs/jungle/README.md
53
+ - docs/jungle/rc.d/README.md
54
+ - docs/jungle/rc.d/puma
55
+ - docs/jungle/rc.d/puma.conf
56
+ - docs/jungle/upstart/README.md
57
+ - docs/jungle/upstart/puma-manager.conf
58
+ - docs/jungle/upstart/puma.conf
51
59
  - docs/nginx.md
52
60
  - docs/plugins.md
53
61
  - docs/restart.md
54
62
  - docs/signals.md
55
63
  - docs/systemd.md
56
- - docs/tcp_mode.md
57
64
  - ext/puma_http11/PumaHttp11Service.java
58
65
  - ext/puma_http11/ext_help.h
59
66
  - ext/puma_http11/extconf.rb
@@ -62,11 +69,9 @@ files:
62
69
  - ext/puma_http11/http11_parser.java.rl
63
70
  - ext/puma_http11/http11_parser.rl
64
71
  - ext/puma_http11/http11_parser_common.rl
65
- - ext/puma_http11/io_buffer.c
66
72
  - ext/puma_http11/mini_ssl.c
67
73
  - ext/puma_http11/org/jruby/puma/Http11.java
68
74
  - ext/puma_http11/org/jruby/puma/Http11Parser.java
69
- - ext/puma_http11/org/jruby/puma/IOBuffer.java
70
75
  - ext/puma_http11/org/jruby/puma/MiniSSL.java
71
76
  - ext/puma_http11/puma_http11.c
72
77
  - lib/puma.rb
@@ -99,29 +104,20 @@ files:
99
104
  - lib/puma/server.rb
100
105
  - lib/puma/single.rb
101
106
  - lib/puma/state_file.rb
102
- - lib/puma/tcp_logger.rb
103
107
  - lib/puma/thread_pool.rb
104
108
  - lib/puma/util.rb
105
109
  - lib/rack/handler/puma.rb
106
- - tools/docker/Dockerfile
107
- - tools/jungle/README.md
108
- - tools/jungle/init.d/README.md
109
- - tools/jungle/init.d/puma
110
- - tools/jungle/init.d/run-puma
111
- - tools/jungle/rc.d/README.md
112
- - tools/jungle/rc.d/puma
113
- - tools/jungle/rc.d/puma.conf
114
- - tools/jungle/upstart/README.md
115
- - tools/jungle/upstart/puma-manager.conf
116
- - tools/jungle/upstart/puma.conf
110
+ - tools/Dockerfile
117
111
  - tools/trickletest.rb
118
112
  homepage: http://puma.io
119
113
  licenses:
120
114
  - BSD-3-Clause
121
115
  metadata:
122
- msys2_mingw_dependencies: openssl
116
+ bug_tracker_uri: https://github.com/puma/puma/issues
123
117
  changelog_uri: https://github.com/puma/puma/blob/master/History.md
124
- post_install_message:
118
+ homepage_uri: http://puma.io
119
+ source_code_uri: https://github.com/puma/puma
120
+ post_install_message:
125
121
  rdoc_options: []
126
122
  require_paths:
127
123
  - lib
@@ -132,12 +128,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
128
  version: '2.2'
133
129
  required_rubygems_version: !ruby/object:Gem::Requirement
134
130
  requirements:
135
- - - ">="
131
+ - - ">"
136
132
  - !ruby/object:Gem::Version
137
- version: '0'
133
+ version: 1.3.1
138
134
  requirements: []
139
- rubygems_version: 3.2.3
140
- signing_key:
135
+ rubygems_version: 3.1.2
136
+ signing_key:
141
137
  specification_version: 4
142
138
  summary: Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for
143
139
  Ruby/Rack applications