puma 8.0.2 → 8.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +154 -0
  3. data/README.md +28 -378
  4. data/docs/binding.md +135 -0
  5. data/docs/cluster_mode.md +241 -0
  6. data/docs/configuration.md +78 -0
  7. data/docs/{stats.md → control_server.md} +28 -8
  8. data/docs/{deployment.md → deployment/README.md} +23 -7
  9. data/docs/{kubernetes.md → deployment/kubernetes.md} +4 -4
  10. data/docs/{systemd.md → deployment/systemd.md} +4 -2
  11. data/docs/plugins.md +62 -3
  12. data/docs/puma_support.md +92 -0
  13. data/docs/rails_dev_mode.md +2 -2
  14. data/docs/restart.md +8 -3
  15. data/docs/signals.md +4 -2
  16. data/docs/{5.0-Upgrade.md → upgrading/5.0-Upgrade.md} +3 -3
  17. data/docs/{6.0-Upgrade.md → upgrading/6.0-Upgrade.md} +3 -3
  18. data/docs/{7.0-Upgrade.md → upgrading/7.0-Upgrade.md} +2 -2
  19. data/docs/{8.0-Upgrade.md → upgrading/8.0-Upgrade.md} +4 -4
  20. data/ext/puma_http11/extconf.rb +2 -0
  21. data/ext/puma_http11/http11_parser.c +6 -2
  22. data/ext/puma_http11/mini_ssl.c +18 -7
  23. data/ext/puma_http11/puma_http11.c +28 -3
  24. data/lib/puma/binder.rb +29 -0
  25. data/lib/puma/cli.rb +6 -0
  26. data/lib/puma/client.rb +178 -44
  27. data/lib/puma/client_env.rb +32 -18
  28. data/lib/puma/cluster/worker.rb +109 -33
  29. data/lib/puma/cluster/worker_handle.rb +16 -0
  30. data/lib/puma/cluster.rb +173 -45
  31. data/lib/puma/commonlogger.rb +3 -3
  32. data/lib/puma/configuration.rb +23 -3
  33. data/lib/puma/const.rb +14 -9
  34. data/lib/puma/control_cli.rb +14 -3
  35. data/lib/puma/dsl.rb +279 -92
  36. data/lib/puma/launcher.rb +6 -4
  37. data/lib/puma/minissl.rb +98 -56
  38. data/lib/puma/plugin/systemd.rb +1 -1
  39. data/lib/puma/reactor.rb +35 -3
  40. data/lib/puma/response.rb +28 -6
  41. data/lib/puma/runner.rb +5 -1
  42. data/lib/puma/sd_notify.rb +4 -1
  43. data/lib/puma/server.rb +131 -35
  44. data/lib/puma/thread_pool.rb +12 -1
  45. data/lib/puma/util.rb +8 -4
  46. data/lib/puma.rb +1 -1
  47. metadata +19 -31
  48. data/docs/fork_worker.md +0 -41
  49. data/docs/grpc.md +0 -62
  50. data/docs/jungle/README.md +0 -9
  51. data/docs/testing_benchmarks_local_files.md +0 -150
  52. data/docs/testing_test_rackup_ci_files.md +0 -36
  53. data/ext/puma_http11/PumaHttp11Service.java +0 -17
  54. data/ext/puma_http11/http11_parser.java.rl +0 -131
  55. data/ext/puma_http11/http11_parser.rl +0 -149
  56. data/ext/puma_http11/http11_parser_common.rl +0 -54
  57. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +0 -15
  58. data/ext/puma_http11/org/jruby/puma/EnvKey.java +0 -241
  59. data/ext/puma_http11/org/jruby/puma/Http11.java +0 -321
  60. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +0 -441
  61. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +0 -509
  62. data/tools/Dockerfile +0 -26
  63. data/tools/trickletest.rb +0 -44
  64. /data/docs/{nginx.md → deployment/nginx.md} +0 -0
  65. /data/docs/{jungle → deployment}/rc.d/README.md +0 -0
  66. /data/docs/{jungle → deployment}/rc.d/puma +0 -0
  67. /data/docs/{jungle → deployment}/rc.d/puma.conf +0 -0
data/lib/puma/launcher.rb CHANGED
@@ -78,6 +78,7 @@ module Puma
78
78
  @binder = Binder.new(@log_writer, @options)
79
79
  @binder.create_inherited_fds(env).each { |k| env.delete k }
80
80
  @binder.create_activated_fds(env).each { |k| env.delete k }
81
+ @control_activated_socket = @binder.take_activated_socket(@options[:control_url]) if @options[:control_url]
81
82
 
82
83
  @environment = @config.environment
83
84
 
@@ -112,7 +113,7 @@ module Puma
112
113
  @status = :run
113
114
  end
114
115
 
115
- attr_reader :binder, :log_writer, :events, :config, :options, :restart_dir
116
+ attr_reader :binder, :log_writer, :events, :config, :options, :restart_dir, :control_activated_socket
116
117
 
117
118
  # Return stats about the server
118
119
  def stats
@@ -187,6 +188,9 @@ module Puma
187
188
  if clustered? && @runner.respond_to?(:fork_worker!) && @options[:fork_worker]
188
189
  @runner.fork_worker!
189
190
  true
191
+ elsif clustered? && @runner.respond_to?(:mold_worker!) && @options[:mold_worker]
192
+ @runner.mold_worker!
193
+ true
190
194
  else
191
195
  log "* refork called but not available."
192
196
  false
@@ -197,8 +201,6 @@ module Puma
197
201
  def run
198
202
  previous_env = get_env
199
203
 
200
- @config.clamp
201
-
202
204
  @config.plugins.fire_starts self
203
205
 
204
206
  setup_signals
@@ -282,8 +284,8 @@ module Puma
282
284
  end
283
285
 
284
286
  def do_graceful_stop
285
- @events.fire_after_stopped!
286
287
  @runner.stop_blocked
288
+ @events.fire_after_stopped!
287
289
  end
288
290
 
289
291
  def do_restart(previous_env)
data/lib/puma/minissl.rb CHANGED
@@ -25,6 +25,9 @@ module Puma
25
25
  @engine = engine
26
26
  @peercert = nil
27
27
  @reuse = nil
28
+ @write_buffer = +''
29
+ @write_owed = 0
30
+ @write_owed_data = nil
28
31
  end
29
32
 
30
33
  # @!attribute [r] to_io
@@ -68,9 +71,8 @@ module Puma
68
71
 
69
72
  return output if output
70
73
 
71
- while neg_data = @engine.extract
72
- @socket.write neg_data
73
- end
74
+ queue_ciphertext
75
+ flush_write_buffer
74
76
  end
75
77
  end
76
78
 
@@ -85,52 +87,60 @@ module Puma
85
87
  def read_nonblock(size, *_)
86
88
  # *_ is to deal with keyword args that were added
87
89
  # at some point (and being used in the wild)
88
- while true
89
- output = engine_read_all
90
- return output if output
91
-
92
- data = @socket.read_nonblock(size, exception: false)
93
- if data == :wait_readable || data == :wait_writable
94
- # It would make more sense to let @socket.read_nonblock raise
95
- # EAGAIN if necessary but it seems like it'll misbehave on Windows.
96
- # I don't have a Windows machine to debug this so I can't explain
97
- # exactly whats happening in that OS. Please let me know if you
98
- # find out!
99
- #
100
- # In the meantime, we can emulate the correct behavior by
101
- # capturing :wait_readable & :wait_writable and raising EAGAIN
102
- # ourselves.
103
- raise IO::EAGAINWaitReadable
104
- elsif data.nil?
105
- raise SSLError.exception "HTTP connection?" if bad_tlsv1_3?
106
- return nil
107
- end
90
+ output = engine_read_all
91
+ return output if output
92
+
93
+ raise IO::EAGAINWaitReadable unless flush_write_buffer_nonblock
94
+
95
+ data = @socket.read_nonblock(size, exception: false)
96
+ if data == :wait_readable || data == :wait_writable
97
+ # It would make more sense to let @socket.read_nonblock raise
98
+ # EAGAIN if necessary but it seems like it'll misbehave on Windows.
99
+ # I don't have a Windows machine to debug this so I can't explain
100
+ # exactly what's happening in that OS. Please let me know if you
101
+ # find out!
102
+ #
103
+ # In the meantime, we can emulate the correct behavior by
104
+ # capturing :wait_readable & :wait_writable and raising EAGAIN
105
+ # ourselves.
106
+ raise IO::EAGAINWaitReadable
107
+ elsif data.nil?
108
+ raise SSLError.exception "HTTP connection?" if bad_tlsv1_3?
109
+ return nil
110
+ end
108
111
 
109
- @engine.inject(data)
110
- output = engine_read_all
112
+ @engine.inject(data)
113
+ output = engine_read_all
114
+ return output if output
111
115
 
112
- return output if output
116
+ queue_ciphertext
117
+ flush_write_buffer_nonblock
118
+ raise IO::EAGAINWaitReadable
119
+ end
113
120
 
114
- while neg_data = @engine.extract
115
- @socket.write neg_data
116
- end
117
- end
121
+ def write_pending?
122
+ !@write_buffer.empty?
118
123
  end
119
124
 
120
125
  def write(data)
121
126
  return 0 if data.empty?
122
127
 
123
128
  data_size = data.bytesize
124
- need = data_size
129
+
130
+ flush_write_buffer
131
+ if @write_owed_data && data.byteslice(0, @write_owed_data.bytesize).b == @write_owed_data
132
+ data = data.byteslice(@write_owed..-1)
133
+ @write_owed = 0
134
+ @write_owed_data = nil
135
+ return data_size if data.empty?
136
+ end
137
+
138
+ need = data.bytesize
125
139
 
126
140
  while true
127
141
  wrote = @engine.write data
128
-
129
- enc_wr = +''
130
- while (enc = @engine.extract)
131
- enc_wr << enc
132
- end
133
- @socket.write enc_wr unless enc_wr.empty?
142
+ queue_ciphertext
143
+ flush_write_buffer
134
144
 
135
145
  need -= wrote
136
146
 
@@ -143,34 +153,43 @@ module Puma
143
153
  alias_method :syswrite, :write
144
154
  alias_method :<<, :write
145
155
 
146
- # This is a temporary fix to deal with websockets code using
147
- # write_nonblock.
156
+ # As with SSL_write, retry with the same data after IO::EAGAINWaitWritable
157
+ def write_nonblock(data, exception: true)
158
+ return 0 if data.empty?
148
159
 
149
- # The problem with implementing it properly
150
- # is that it means we'd have to have the ability to rewind
151
- # an engine because after we write+extract, the socket
152
- # write_nonblock call might raise an exception and later
153
- # code would pass the same data in, but the engine would think
154
- # it had already written the data in.
155
- #
156
- # So for the time being (and since write blocking is quite rare),
157
- # go ahead and actually block in write_nonblock.
158
- #
159
- def write_nonblock(data, *_)
160
- write data
160
+ @write_owed = 0 if @write_owed_data && data.byteslice(0, @write_owed_data.bytesize).b != @write_owed_data
161
+
162
+ if @write_owed == 0
163
+ @write_owed = @engine.write data
164
+ @write_owed_data = nil
165
+ queue_ciphertext
166
+ end
167
+
168
+ unless flush_write_buffer_nonblock
169
+ @write_owed_data ||= data.byteslice(0, @write_owed).b
170
+ raise IO::EAGAINWaitWritable if exception
171
+ return :wait_writable
172
+ end
173
+
174
+ wrote = @write_owed
175
+ @write_owed = 0
176
+ @write_owed_data = nil
177
+ wrote
178
+ end
179
+
180
+ def wait_writable(timeout = nil)
181
+ @socket.wait_writable timeout
161
182
  end
162
183
 
163
184
  def flush
185
+ flush_write_buffer
164
186
  @socket.flush
165
187
  end
166
188
 
167
189
  def close
168
190
  begin
169
- unless @engine.shutdown
170
- while alert_data = @engine.extract
171
- @socket.write alert_data
172
- end
173
- end
191
+ queue_ciphertext unless @engine.shutdown
192
+ flush_write_buffer_nonblock
174
193
  rescue IOError, SystemCallError
175
194
  # nothing
176
195
  ensure
@@ -178,6 +197,29 @@ module Puma
178
197
  end
179
198
  end
180
199
 
200
+ def queue_ciphertext
201
+ while (enc = @engine.extract)
202
+ @write_buffer << enc
203
+ end
204
+ end
205
+ private :queue_ciphertext
206
+
207
+ def flush_write_buffer
208
+ @socket.wait_writable until flush_write_buffer_nonblock
209
+ end
210
+ private :flush_write_buffer
211
+
212
+ def flush_write_buffer_nonblock
213
+ until @write_buffer.empty?
214
+ n = @socket.write_nonblock @write_buffer, exception: false
215
+ return false if n == :wait_writable
216
+
217
+ @write_buffer = @write_buffer.byteslice(n..-1)
218
+ end
219
+ true
220
+ end
221
+ private :flush_write_buffer_nonblock
222
+
181
223
  # @!attribute [r] peeraddr
182
224
  def peeraddr
183
225
  @socket.peeraddr
@@ -5,7 +5,7 @@ require_relative '../plugin'
5
5
  # Puma's systemd integration allows Puma to inform systemd:
6
6
  # 1. when it has successfully started
7
7
  # 2. when it is starting shutdown
8
- # 3. periodically for a liveness check with a watchdog thread
8
+ # 3. at startup and then periodically for a liveness check with a watchdog thread
9
9
  # 4. periodically set the status
10
10
  Puma::Plugin.create do
11
11
  def start(launcher)
data/lib/puma/reactor.rb CHANGED
@@ -34,6 +34,7 @@ module Puma
34
34
  @selector = ::NIO::Selector.new(NIO::Selector.backends.delete(backend))
35
35
  @input = Queue.new
36
36
  @timeouts = []
37
+ @timeout_keys = {}.compare_by_identity
37
38
  @block = block
38
39
  @reactor_size = 0
39
40
  @reactor_max = 0
@@ -77,11 +78,13 @@ module Puma
77
78
  def select_loop
78
79
  begin
79
80
  until @input.closed? && @input.empty?
81
+ requeue_moved_timeouts
82
+
80
83
  # Wakeup any registered object that receives incoming data.
81
84
  # Block until the earliest timeout or Selector#wakeup is called.
82
85
  timeout = (earliest = @timeouts.first) && earliest.timeout
83
86
  @selector.select(timeout) do |monitor|
84
- wakeup!(monitor.value)
87
+ monitor.interests = interests(monitor.value) unless wakeup!(monitor.value)
85
88
  end
86
89
 
87
90
  # Wakeup all objects that timed out.
@@ -93,7 +96,7 @@ module Puma
93
96
  client = @input.pop
94
97
  register(client) if client.io_ok?
95
98
  end
96
- @timeouts.sort_by!(&:timeout_at)
99
+ @timeouts.sort_by! { |client| @timeout_keys[client] }
97
100
  end
98
101
  end
99
102
  rescue StandardError => e
@@ -110,14 +113,19 @@ module Puma
110
113
 
111
114
  # Start monitoring the object.
112
115
  def register(client)
113
- @selector.register(client.to_io, :r).value = client
116
+ @selector.register(client.to_io, interests(client)).value = client
114
117
  @reactor_size += 1
115
118
  @reactor_max = @reactor_size if @reactor_max < @reactor_size
116
119
  @timeouts << client
120
+ @timeout_keys[client] = client.timeout_at
117
121
  rescue ArgumentError
118
122
  # unreadable clients raise error when processed by NIO
119
123
  end
120
124
 
125
+ def interests(client)
126
+ client.write_pending? ? :w : :r
127
+ end
128
+
121
129
  # 'Wake up' a monitored object by calling the provided block.
122
130
  # Stop monitoring the object if the block returns `true`.
123
131
  def wakeup!(client)
@@ -125,7 +133,31 @@ module Puma
125
133
  @selector.deregister client.to_io
126
134
  @reactor_size -= 1
127
135
  @timeouts.delete client
136
+ @timeout_keys.delete client
137
+ true
138
+ else
139
+ if client.timeout_at < @timeout_keys[client]
140
+ @timeouts.delete client
141
+ requeue client
142
+ end
143
+ false
128
144
  end
129
145
  end
146
+
147
+ # `@timeouts` is sorted by the timeout each client had when it was queued.
148
+ # Timeouts that moved earlier are requeued in `wakeup!`. Timeouts that moved
149
+ # later are requeued here, once the client reaches the front.
150
+ def requeue_moved_timeouts
151
+ while (client = @timeouts.first) && @timeout_keys[client] < client.timeout_at
152
+ @timeouts.shift
153
+ requeue client
154
+ end
155
+ end
156
+
157
+ def requeue(client)
158
+ key = @timeout_keys[client] = client.timeout_at
159
+ index = @timeouts.bsearch_index { |c| @timeout_keys[c] > key } || @timeouts.size
160
+ @timeouts.insert index, client
161
+ end
130
162
  end
131
163
  end
data/lib/puma/response.rb CHANGED
@@ -59,6 +59,8 @@ module Puma
59
59
 
60
60
  return :close if closed_socket?(socket)
61
61
 
62
+ write_pending_interim socket, client
63
+
62
64
  if @early_hints
63
65
  env[EARLY_HINTS] = lambda { |headers|
64
66
  begin
@@ -73,6 +75,7 @@ module Puma
73
75
  end
74
76
 
75
77
  env["puma.mark_as_io_bound"] = -> { processor.mark_as_io_thread! }
78
+ env["puma.connection_closed?"] = -> { closed_socket?(socket) }
76
79
 
77
80
  begin
78
81
  status, headers, app_body = @thread_pool.with_force_shutdown do
@@ -148,6 +151,8 @@ module Puma
148
151
 
149
152
  return :close if closed_socket?(socket)
150
153
 
154
+ write_pending_interim socket, client
155
+
151
156
  # Close the connection after a reasonable number of inline requests
152
157
  force_keep_alive = @enable_keep_alives && client.requests_served < @max_keep_alive
153
158
 
@@ -161,7 +166,7 @@ module Puma
161
166
  if res_body.respond_to?(:each) && !resp_info[:response_hijack]
162
167
  # below converts app_body into body, dependent on app_body's characteristics, and
163
168
  # content_length will be set if it can be determined
164
- if !content_length && !resp_info[:transfer_encoding] && status != 204
169
+ if !content_length && !resp_info[:transfer_encoding] && status != 204 && status != 304
165
170
  if res_body.respond_to?(:to_ary) && (array_body = res_body.to_ary) &&
166
171
  array_body.is_a?(Array)
167
172
  body = array_body.compact
@@ -172,16 +177,16 @@ module Puma
172
177
  elsif res_body.respond_to?(:to_path) && (fn = res_body.to_path) &&
173
178
  File.readable?(fn)
174
179
  body = File.open fn, 'rb'
175
- content_length = body.size
176
180
  close_body = true
181
+ content_length = body.size
177
182
  else
178
183
  body = res_body
179
184
  end
180
185
  elsif !res_body.is_a?(::File) && res_body.respond_to?(:to_path) &&
181
186
  (fn = res_body.to_path) && File.readable?(fn = res_body.to_path)
182
187
  body = File.open fn, 'rb'
183
- content_length = body.size
184
188
  close_body = true
189
+ content_length = body.size
185
190
  elsif !res_body.is_a?(::File) && res_body.respond_to?(:filename) &&
186
191
  res_body.respond_to?(:bytesize) && File.readable?(fn = res_body.filename)
187
192
  # Sprockets::Asset
@@ -242,11 +247,18 @@ module Puma
242
247
  end
243
248
 
244
249
  fast_write_response socket, body, io_buffer, chunked, content_length.to_i
245
- body.close if close_body
246
250
 
247
251
  # if we're shutting down, close keep_alive connections
248
252
  !shutting_down? && keep_alive ? :keep_alive : :close
253
+ ensure
254
+ body.close if close_body
255
+ end
256
+
257
+ def write_pending_interim(socket, client)
258
+ pending = client.take_pending_write
259
+ fast_write_str socket, pending if pending
249
260
  end
261
+ private :write_pending_interim
250
262
 
251
263
  # Used to write 'early hints', 'no body' responses, 'hijacked' responses,
252
264
  # and body segments (called by `fast_write_response`).
@@ -297,7 +309,11 @@ module Puma
297
309
  fast_write_str socket, io_buffer.read_and_reset
298
310
  else
299
311
  fast_write_str socket, io_buffer.read_and_reset
300
- IO.copy_stream body, socket
312
+ remaining = content_length
313
+ while remaining > 0 && (chunk = body.read([remaining, BODY_LEN_MAX].min))
314
+ fast_write_str socket, chunk
315
+ remaining -= chunk.bytesize
316
+ end
301
317
  end
302
318
  end
303
319
  elsif body.is_a?(::Array) && body.length == 1
@@ -416,6 +432,12 @@ module Puma
416
432
  end
417
433
  private :fetch_status_code
418
434
 
435
+ def request_has_close?(env)
436
+ conn = env.fetch(HTTP_CONNECTION, "").downcase
437
+ conn.include?(CLOSE) && conn.split(",").any? { |option| option.strip == CLOSE }
438
+ end
439
+ private :request_has_close?
440
+
419
441
  # Processes and write headers to the IOBuffer.
420
442
  # @param env [Hash] see Puma::Client#env, from request
421
443
  # @param status [Integer] the status returned by the Rack application
@@ -439,7 +461,7 @@ module Puma
439
461
  http_11 = env[SERVER_PROTOCOL] == HTTP_11
440
462
  if http_11
441
463
  resp_info[:allow_chunked] = true
442
- resp_info[:keep_alive] = env.fetch(HTTP_CONNECTION, "").downcase != CLOSE
464
+ resp_info[:keep_alive] = !request_has_close?(env)
443
465
 
444
466
  # An optimization. The most common response is 200, so we can
445
467
  # reply with the proper 200 status without having to compute
data/lib/puma/runner.rb CHANGED
@@ -30,7 +30,7 @@ module Puma
30
30
  def wakeup!
31
31
  return unless @wakeup
32
32
 
33
- @wakeup.write PIPE_WAKEUP unless @wakeup.closed?
33
+ @wakeup.write_nonblock(PIPE_WAKEUP) unless @wakeup.closed?
34
34
 
35
35
  rescue SystemCallError, IOError
36
36
  end
@@ -77,6 +77,10 @@ module Puma
77
77
  control = Puma::Server.new app, nil,
78
78
  { min_threads: 0, max_threads: 1, queue_requests: false, log_writer: @log_writer }
79
79
 
80
+ if (activated = @launcher.control_activated_socket)
81
+ control.binder.activated_sockets.store(*activated)
82
+ end
83
+
80
84
  begin
81
85
  control.binder.parse [str], nil, 'Starting control server'
82
86
  rescue Errno::EADDRINUSE, Errno::EACCES => e
@@ -49,8 +49,11 @@ module Puma
49
49
  WATCHDOG = "WATCHDOG=1"
50
50
  FDSTORE = "FDSTORE=1"
51
51
 
52
+ # @note When the service manager expects watchdog pings, this sends an
53
+ # immediate `WATCHDOG=1` in the same datagram, which closes the watchdog
54
+ # timeout window between startup and the first periodic watchdog tick.
52
55
  def self.ready(unset_env=false)
53
- notify(READY, unset_env)
56
+ notify(watchdog? ? "#{READY}\n#{WATCHDOG}" : READY, unset_env)
54
57
  end
55
58
 
56
59
  def self.reloading(unset_env=false)