puma 3.9.1 → 4.3.1

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 (82) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +232 -0
  3. data/README.md +162 -224
  4. data/docs/architecture.md +37 -0
  5. data/{DEPLOYMENT.md → docs/deployment.md} +24 -4
  6. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  7. data/docs/images/puma-connection-flow.png +0 -0
  8. data/docs/images/puma-general-arch.png +0 -0
  9. data/docs/plugins.md +38 -0
  10. data/docs/restart.md +41 -0
  11. data/docs/signals.md +56 -3
  12. data/docs/systemd.md +130 -37
  13. data/docs/tcp_mode.md +96 -0
  14. data/ext/puma_http11/PumaHttp11Service.java +2 -0
  15. data/ext/puma_http11/extconf.rb +13 -0
  16. data/ext/puma_http11/http11_parser.c +115 -140
  17. data/ext/puma_http11/http11_parser.java.rl +21 -37
  18. data/ext/puma_http11/http11_parser.rl +9 -9
  19. data/ext/puma_http11/http11_parser_common.rl +3 -3
  20. data/ext/puma_http11/mini_ssl.c +104 -8
  21. data/ext/puma_http11/org/jruby/puma/Http11.java +106 -114
  22. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +90 -108
  23. data/ext/puma_http11/org/jruby/puma/IOBuffer.java +72 -0
  24. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +21 -4
  25. data/ext/puma_http11/puma_http11.c +2 -0
  26. data/lib/puma.rb +16 -0
  27. data/lib/puma/accept_nonblock.rb +7 -1
  28. data/lib/puma/app/status.rb +40 -26
  29. data/lib/puma/binder.rb +57 -74
  30. data/lib/puma/cli.rb +26 -7
  31. data/lib/puma/client.rb +243 -190
  32. data/lib/puma/cluster.rb +78 -34
  33. data/lib/puma/commonlogger.rb +2 -0
  34. data/lib/puma/configuration.rb +24 -16
  35. data/lib/puma/const.rb +36 -18
  36. data/lib/puma/control_cli.rb +46 -19
  37. data/lib/puma/detect.rb +2 -0
  38. data/lib/puma/dsl.rb +329 -68
  39. data/lib/puma/events.rb +6 -1
  40. data/lib/puma/io_buffer.rb +3 -6
  41. data/lib/puma/jruby_restart.rb +2 -1
  42. data/lib/puma/launcher.rb +120 -58
  43. data/lib/puma/minissl.rb +69 -27
  44. data/lib/puma/minissl/context_builder.rb +76 -0
  45. data/lib/puma/null_io.rb +2 -0
  46. data/lib/puma/plugin.rb +7 -2
  47. data/lib/puma/plugin/tmp_restart.rb +2 -1
  48. data/lib/puma/rack/builder.rb +4 -1
  49. data/lib/puma/rack/urlmap.rb +2 -0
  50. data/lib/puma/rack_default.rb +2 -0
  51. data/lib/puma/reactor.rb +224 -34
  52. data/lib/puma/runner.rb +25 -4
  53. data/lib/puma/server.rb +148 -62
  54. data/lib/puma/single.rb +16 -5
  55. data/lib/puma/state_file.rb +2 -0
  56. data/lib/puma/tcp_logger.rb +2 -0
  57. data/lib/puma/thread_pool.rb +61 -38
  58. data/lib/puma/util.rb +2 -6
  59. data/lib/rack/handler/puma.rb +10 -4
  60. data/tools/docker/Dockerfile +16 -0
  61. data/tools/jungle/README.md +12 -2
  62. data/tools/jungle/init.d/README.md +2 -0
  63. data/tools/jungle/init.d/puma +8 -8
  64. data/tools/jungle/init.d/run-puma +1 -1
  65. data/tools/jungle/rc.d/README.md +74 -0
  66. data/tools/jungle/rc.d/puma +61 -0
  67. data/tools/jungle/rc.d/puma.conf +10 -0
  68. data/tools/trickletest.rb +1 -2
  69. metadata +29 -56
  70. data/.github/issue_template.md +0 -20
  71. data/Gemfile +0 -14
  72. data/Manifest.txt +0 -78
  73. data/Rakefile +0 -165
  74. data/Release.md +0 -9
  75. data/gemfiles/2.1-Gemfile +0 -12
  76. data/lib/puma/compat.rb +0 -14
  77. data/lib/puma/convenient.rb +0 -23
  78. data/lib/puma/daemon_ext.rb +0 -31
  79. data/lib/puma/delegation.rb +0 -11
  80. data/lib/puma/java_io_buffer.rb +0 -45
  81. data/lib/puma/rack/backports/uri/common_193.rb +0 -33
  82. data/puma.gemspec +0 -20
@@ -1,13 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puma/runner'
2
4
  require 'puma/detect'
3
5
  require 'puma/plugin'
4
6
 
5
7
  module Puma
8
+ # This class is instantiated by the `Puma::Launcher` and used
9
+ # to boot and serve a Ruby application when no puma "workers" are needed
10
+ # i.e. only using "threaded" mode. For example `$ puma -t 1:5`
11
+ #
12
+ # At the core of this class is running an instance of `Puma::Server` which
13
+ # gets created via the `start_server` method from the `Puma::Runner` class
14
+ # that this inherits from.
6
15
  class Single < Runner
7
16
  def stats
8
- b = @server.backlog
9
- r = @server.running
10
- %Q!{ "backlog": #{b}, "running": #{r} }!
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} }!
11
22
  end
12
23
 
13
24
  def restart
@@ -15,7 +26,7 @@ module Puma
15
26
  end
16
27
 
17
28
  def stop
18
- @server.stop false
29
+ @server.stop(false) if @server
19
30
  end
20
31
 
21
32
  def halt
@@ -25,7 +36,7 @@ module Puma
25
36
  def stop_blocked
26
37
  log "- Gracefully stopping, waiting for requests to finish"
27
38
  @control.stop(true) if @control
28
- @server.stop(true)
39
+ @server.stop(true) if @server
29
40
  end
30
41
 
31
42
  def jruby_daemon?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
 
3
5
  module Puma
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Puma
2
4
  class TCPLogger
3
5
  def initialize(logger, app, quiet=false)
@@ -1,8 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'thread'
2
4
 
3
5
  module Puma
4
- # A simple thread pool management object.
6
+ # Internal Docs for A simple thread pool management object.
7
+ #
8
+ # Each Puma "worker" has a thread pool to process requests.
5
9
  #
10
+ # First a connection to a client is made in `Puma::Server`. It is wrapped in a
11
+ # `Puma::Client` instance and then passed to the `Puma::Reactor` to ensure
12
+ # the whole request is buffered into memory. Once the request is ready, it is passed into
13
+ # a thread pool via the `Puma::ThreadPool#<<` operator where it is stored in a `@todo` array.
14
+ #
15
+ # Each thread in the pool has an internal loop where it pulls a request from the `@todo` array
16
+ # and proceses it.
6
17
  class ThreadPool
7
18
  class ForceShutdown < RuntimeError
8
19
  end
@@ -49,11 +60,11 @@ module Puma
49
60
  @clean_thread_locals = false
50
61
  end
51
62
 
52
- attr_reader :spawned, :trim_requested
63
+ attr_reader :spawned, :trim_requested, :waiting
53
64
  attr_accessor :clean_thread_locals
54
65
 
55
66
  def self.clean_thread_locals
56
- Thread.current.keys.each do |key|
67
+ Thread.current.keys.each do |key| # rubocop: disable Performance/HashEachMethods
57
68
  Thread.current[key] = nil unless key == :__recursive_key__
58
69
  end
59
70
  end
@@ -64,6 +75,10 @@ module Puma
64
75
  @mutex.synchronize { @todo.size }
65
76
  end
66
77
 
78
+ def pool_capacity
79
+ waiting + (@max - spawned)
80
+ end
81
+
67
82
  # :nodoc:
68
83
  #
69
84
  # Must be called with @mutex held!
@@ -71,9 +86,8 @@ module Puma
71
86
  def spawn_thread
72
87
  @spawned += 1
73
88
 
74
- th = Thread.new do
75
- # Thread name is new in Ruby 2.3
76
- Thread.current.name = 'puma %03i' % @spawned if Thread.current.respond_to?(:name=)
89
+ th = Thread.new(@spawned) do |spawned|
90
+ Puma.set_thread_name 'threadpool %03i' % spawned
77
91
  todo = @todo
78
92
  block = @block
79
93
  mutex = @mutex
@@ -153,16 +167,46 @@ module Puma
153
167
  end
154
168
  end
155
169
 
170
+ # This method is used by `Puma::Server` to let the server know when
171
+ # the thread pool can pull more requests from the socket and
172
+ # pass to the reactor.
173
+ #
174
+ # The general idea is that the thread pool can only work on a fixed
175
+ # number of requests at the same time. If it is already processing that
176
+ # number of requests then it is at capacity. If another Puma process has
177
+ # spare capacity, then the request can be left on the socket so the other
178
+ # worker can pick it up and process it.
179
+ #
180
+ # For example: if there are 5 threads, but only 4 working on
181
+ # requests, this method will not wait and the `Puma::Server`
182
+ # can pull a request right away.
183
+ #
184
+ # If there are 5 threads and all 5 of them are busy, then it will
185
+ # pause here, and wait until the `not_full` condition variable is
186
+ # signaled, usually this indicates that a request has been processed.
187
+ #
188
+ # It's important to note that even though the server might accept another
189
+ # request, it might not be added to the `@todo` array right away.
190
+ # For example if a slow client has only sent a header, but not a body
191
+ # then the `@todo` array would stay the same size as the reactor works
192
+ # to try to buffer the request. In that scenario the next call to this
193
+ # method would not block and another request would be added into the reactor
194
+ # by the server. This would continue until a fully bufferend request
195
+ # 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
+ #
156
199
  def wait_until_not_full
157
200
  @mutex.synchronize do
158
201
  while true
159
202
  return if @shutdown
160
- return if @waiting > 0
161
203
 
162
204
  # If we can still spin up new threads and there
163
- # is work queued, then accept more work until we would
205
+ # is work queued that cannot be handled by waiting
206
+ # threads, then accept more work until we would
164
207
  # spin up the max number of threads.
165
- return if @todo.size < @max - @spawned
208
+ busy_threads = @spawned - @waiting + @todo.size
209
+ return busy_threads if @max > busy_threads
166
210
 
167
211
  @not_full.wait @mutex
168
212
  end
@@ -199,10 +243,12 @@ module Puma
199
243
  end
200
244
  end
201
245
 
202
- class AutoTrim
203
- def initialize(pool, timeout)
246
+ class Automaton
247
+ def initialize(pool, timeout, thread_name, message)
204
248
  @pool = pool
205
249
  @timeout = timeout
250
+ @thread_name = thread_name
251
+ @message = message
206
252
  @running = false
207
253
  end
208
254
 
@@ -210,8 +256,9 @@ module Puma
210
256
  @running = true
211
257
 
212
258
  @thread = Thread.new do
259
+ Puma.set_thread_name @thread_name
213
260
  while @running
214
- @pool.trim
261
+ @pool.public_send(@message)
215
262
  sleep @timeout
216
263
  end
217
264
  end
@@ -224,36 +271,12 @@ module Puma
224
271
  end
225
272
 
226
273
  def auto_trim!(timeout=30)
227
- @auto_trim = AutoTrim.new(self, timeout)
274
+ @auto_trim = Automaton.new(self, timeout, "threadpool trimmer", :trim)
228
275
  @auto_trim.start!
229
276
  end
230
277
 
231
- class Reaper
232
- def initialize(pool, timeout)
233
- @pool = pool
234
- @timeout = timeout
235
- @running = false
236
- end
237
-
238
- def start!
239
- @running = true
240
-
241
- @thread = Thread.new do
242
- while @running
243
- @pool.reap
244
- sleep @timeout
245
- end
246
- end
247
- end
248
-
249
- def stop
250
- @running = false
251
- @thread.wakeup
252
- end
253
- end
254
-
255
278
  def auto_reap!(timeout=5)
256
- @reaper = Reaper.new(self, timeout)
279
+ @reaper = Automaton.new(self, timeout, "threadpool reaper", :reap)
257
280
  @reaper.start!
258
281
  end
259
282
 
@@ -1,10 +1,6 @@
1
- major, minor, patch = RUBY_VERSION.split('.').map { |v| v.to_i }
1
+ # frozen_string_literal: true
2
2
 
3
- if major == 1 && minor == 9 && patch == 3 && RUBY_PATCHLEVEL < 125
4
- require 'puma/rack/backports/uri/common_193'
5
- else
6
- require 'uri/common'
7
- end
3
+ require 'uri/common'
8
4
 
9
5
  module Puma
10
6
  module Util
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rack/handler'
2
4
 
3
5
  module Rack
@@ -9,6 +11,7 @@ module Rack
9
11
  }
10
12
 
11
13
  def self.config(app, options = {})
14
+ require 'puma'
12
15
  require 'puma/configuration'
13
16
  require 'puma/events'
14
17
  require 'puma/launcher'
@@ -21,7 +24,7 @@ module Rack
21
24
  # contains an array of all explicitly defined user options. We then
22
25
  # know that all other values are defaults
23
26
  if user_supplied_options = options.delete(:user_supplied_options)
24
- (options.keys - user_supplied_options).each do |k, v|
27
+ (options.keys - user_supplied_options).each do |k|
25
28
  default_options[k] = options.delete(k)
26
29
  end
27
30
  end
@@ -48,6 +51,9 @@ module Rack
48
51
  self.set_host_port_to_config(host, port, user_config)
49
52
  end
50
53
 
54
+ if default_options[:Host]
55
+ file_config.set_default_host(default_options[:Host])
56
+ end
51
57
  self.set_host_port_to_config(default_options[:Host], default_options[:Port], default_config)
52
58
 
53
59
  user_config.app app
@@ -55,8 +61,6 @@ module Rack
55
61
  conf
56
62
  end
57
63
 
58
-
59
-
60
64
  def self.run(app, options = {})
61
65
  conf = self.config(app, options)
62
66
 
@@ -82,8 +86,10 @@ module Rack
82
86
  "Verbose" => "Don't report each request (default: false)"
83
87
  }
84
88
  end
85
- private
89
+
86
90
  def self.set_host_port_to_config(host, port, config)
91
+ config.clear_binds! if host || port
92
+
87
93
  if host && (host[0,1] == '.' || host[0,1] == '/')
88
94
  config.bind "unix://#{host}"
89
95
  elsif host && host =~ /^ssl:\/\//
@@ -0,0 +1,16 @@
1
+ # Use this Dockerfile to create minimal reproductions of issues
2
+
3
+ FROM ruby:2.6
4
+
5
+ # throw errors if Gemfile has been modified since Gemfile.lock
6
+ RUN bundle config --global frozen 1
7
+
8
+ WORKDIR /usr/src/app
9
+
10
+ COPY . .
11
+ RUN gem install bundler
12
+ RUN bundle install
13
+ RUN bundle exec rake compile
14
+
15
+ EXPOSE 9292
16
+ CMD bundle exec bin/puma test/rackup/hello.ru
@@ -1,9 +1,19 @@
1
1
  # Puma as a service
2
2
 
3
+ ## Upstart
4
+
5
+ See `/tools/jungle/upstart` for Ubuntu's upstart scripts.
6
+
7
+ ## Systemd
8
+
9
+ See [/docs/systemd](https://github.com/puma/puma/blob/master/docs/systemd.md).
10
+
3
11
  ## Init.d
4
12
 
13
+ Deprecatation Warning : `init.d` was replaced by `systemd` since Debian 8 and Ubuntu 16.04, you should look into [/docs/systemd](https://github.com/puma/puma/blob/master/docs/systemd.md) unless you are on an older OS.
14
+
5
15
  See `/tools/jungle/init.d` for tools to use with init.d and start-stop-daemon.
6
16
 
7
- ## Upstart
17
+ ## rc.d
8
18
 
9
- See `/tools/jungle/upstart` for Ubuntu's upstart scripts.
19
+ See `/tools/jungle/rc.d` for FreeBSD's rc.d scripts
@@ -1,5 +1,7 @@
1
1
  # Puma daemon service
2
2
 
3
+ Deprecatation Warning : `init.d` was replaced by `systemd` since Debian 8 and Ubuntu 16.04, you should look into [/docs/systemd](https://github.com/puma/puma/blob/master/docs/systemd.md) unless you are on an older OS.
4
+
3
5
  Init script to manage multiple Puma servers on the same box using start-stop-daemon.
4
6
 
5
7
  ## Installation
@@ -47,11 +47,11 @@ do_start_one() {
47
47
  PIDFILE=$1/tmp/puma/pid
48
48
  if [ -e $PIDFILE ]; then
49
49
  PID=`cat $PIDFILE`
50
- # If the puma isn't running, run it, otherwise restart it.
51
- if [ "`ps -A -o pid= | grep -c $PID`" -eq 0 ]; then
52
- do_start_one_do $1
53
- else
50
+ # If the puma is running, restart it, otherwise run it.
51
+ if ps -p $PID > /dev/null; then
54
52
  do_restart_one $1
53
+ else
54
+ do_start_one_do $1
55
55
  fi
56
56
  else
57
57
  do_start_one_do $1
@@ -105,9 +105,7 @@ do_stop_one() {
105
105
  STATEFILE=$1/tmp/puma/state
106
106
  if [ -e $PIDFILE ]; then
107
107
  PID=`cat $PIDFILE`
108
- if [ "`ps -A -o pid= | grep -c $PID`" -eq 0 ]; then
109
- log_daemon_msg "---> Puma $1 isn't running."
110
- else
108
+ if ps -p $PID > /dev/null; then
111
109
  log_daemon_msg "---> About to kill PID `cat $PIDFILE`"
112
110
  if [ "$USE_LOCAL_BUNDLE" -eq 1 ]; then
113
111
  cd $1 && bundle exec pumactl --state $STATEFILE stop
@@ -116,6 +114,8 @@ do_stop_one() {
116
114
  fi
117
115
  # Many daemons don't delete their pidfiles when they exit.
118
116
  rm -f $PIDFILE $STATEFILE
117
+ else
118
+ log_daemon_msg "---> Puma $1 isn't running."
119
119
  fi
120
120
  else
121
121
  log_daemon_msg "---> No puma here..."
@@ -398,7 +398,7 @@ case "$1" in
398
398
  ;;
399
399
  remove)
400
400
  if [ "$#" -lt 2 ]; then
401
- echo "Please, specifiy the app's directory to remove."
401
+ echo "Please, specify the app's directory to remove."
402
402
  exit 1
403
403
  else
404
404
  do_remove $2
@@ -15,4 +15,4 @@ elif [ -f "$HOME/.rvm/scripts/rvm" ]; then
15
15
  fi
16
16
 
17
17
  app=$1; config=$2; log=$3;
18
- cd $app && exec bundle exec puma -C $config 2>&1 >> $log
18
+ cd $app && exec bundle exec puma -C $config >> $log 2>&1
@@ -0,0 +1,74 @@
1
+ # Puma as a service using rc.d
2
+
3
+ Manage multilpe Puma servers as services on one box using FreeBSD's rc.d service.
4
+
5
+ ## Dependencies
6
+
7
+ * `jq` - a command-line json parser is needed to parse the json in the config file
8
+
9
+ ## Installation
10
+
11
+ # Copy the puma script to the rc.d directory (make sure everyone has read/execute perms)
12
+ sudo cp puma /usr/local/etc/rc.d/
13
+
14
+ # Create an empty configuration file
15
+ sudo touch /usr/local/etc/puma.conf
16
+
17
+ # Enable the puma service
18
+ sudo echo 'puma_enable="YES"' >> /etc/rc.conf
19
+
20
+ ## Managing the jungle
21
+
22
+ Puma apps are referenced in /usr/local/etc/puma.conf by default.
23
+
24
+ Start the jungle running:
25
+
26
+ `service puma start`
27
+
28
+ This script will run at boot time.
29
+
30
+
31
+ You can also stop the jungle (stops ALL puma instances) by running:
32
+
33
+ `service puma stop`
34
+
35
+
36
+ To restart the jungle:
37
+
38
+ `service puma restart`
39
+
40
+ ## Conventions
41
+
42
+ * The script expects:
43
+ * a config file to exist under `config/puma.rb` in your app. E.g.: `/home/apps/my-app/config/puma.rb`.
44
+
45
+ You can always change those defaults by editing the scripts.
46
+
47
+ ## Here's what a minimal app's config file should have
48
+
49
+ ```
50
+ {
51
+ "servers" : [
52
+ {
53
+ "dir": "/path/to/rails/project",
54
+ "user": "deploy-user",
55
+ "ruby_version": "ruby.version",
56
+ "ruby_env": "rbenv"
57
+ }
58
+ ]
59
+ }
60
+ ```
61
+
62
+ ## Before starting...
63
+
64
+ You need to customise `puma.conf` to:
65
+
66
+ * Set the right user your app should be running on unless you want root to execute it!
67
+ * Set the directory of the app
68
+ * Set the ruby version to execute
69
+ * Set the ruby environment (currently set to rbenv, since that is the only ruby environment currently supported)
70
+ * Add additional server instances following the scheme in the example
71
+
72
+ ## Notes:
73
+
74
+ Only rbenv is currently supported.