puma 3.12.0 → 5.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 (93) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +1413 -439
  3. data/LICENSE +23 -20
  4. data/README.md +131 -60
  5. data/bin/puma-wild +3 -9
  6. data/docs/architecture.md +24 -19
  7. data/docs/compile_options.md +19 -0
  8. data/docs/deployment.md +38 -13
  9. data/docs/fork_worker.md +33 -0
  10. data/docs/jungle/README.md +9 -0
  11. data/{tools → docs}/jungle/rc.d/README.md +1 -1
  12. data/{tools → docs}/jungle/rc.d/puma +2 -2
  13. data/{tools → docs}/jungle/rc.d/puma.conf +0 -0
  14. data/docs/kubernetes.md +66 -0
  15. data/docs/nginx.md +1 -1
  16. data/docs/plugins.md +20 -10
  17. data/docs/rails_dev_mode.md +29 -0
  18. data/docs/restart.md +47 -22
  19. data/docs/signals.md +7 -6
  20. data/docs/stats.md +142 -0
  21. data/docs/systemd.md +48 -70
  22. data/ext/puma_http11/PumaHttp11Service.java +2 -2
  23. data/ext/puma_http11/ext_help.h +1 -1
  24. data/ext/puma_http11/extconf.rb +27 -0
  25. data/ext/puma_http11/http11_parser.c +84 -109
  26. data/ext/puma_http11/http11_parser.h +1 -1
  27. data/ext/puma_http11/http11_parser.java.rl +22 -38
  28. data/ext/puma_http11/http11_parser.rl +4 -2
  29. data/ext/puma_http11/http11_parser_common.rl +3 -3
  30. data/ext/puma_http11/mini_ssl.c +262 -87
  31. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  32. data/ext/puma_http11/org/jruby/puma/Http11.java +108 -116
  33. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +89 -106
  34. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +92 -22
  35. data/ext/puma_http11/puma_http11.c +34 -50
  36. data/lib/puma/app/status.rb +68 -49
  37. data/lib/puma/binder.rb +197 -144
  38. data/lib/puma/cli.rb +17 -15
  39. data/lib/puma/client.rb +257 -226
  40. data/lib/puma/cluster/worker.rb +176 -0
  41. data/lib/puma/cluster/worker_handle.rb +90 -0
  42. data/lib/puma/cluster.rb +223 -212
  43. data/lib/puma/commonlogger.rb +4 -2
  44. data/lib/puma/configuration.rb +58 -51
  45. data/lib/puma/const.rb +41 -19
  46. data/lib/puma/control_cli.rb +117 -73
  47. data/lib/puma/detect.rb +26 -3
  48. data/lib/puma/dsl.rb +531 -123
  49. data/lib/puma/error_logger.rb +104 -0
  50. data/lib/puma/events.rb +57 -31
  51. data/lib/puma/io_buffer.rb +9 -5
  52. data/lib/puma/jruby_restart.rb +2 -58
  53. data/lib/puma/json.rb +96 -0
  54. data/lib/puma/launcher.rb +182 -70
  55. data/lib/puma/minissl/context_builder.rb +79 -0
  56. data/lib/puma/minissl.rb +149 -48
  57. data/lib/puma/null_io.rb +15 -1
  58. data/lib/puma/plugin/tmp_restart.rb +2 -0
  59. data/lib/puma/plugin.rb +8 -12
  60. data/lib/puma/queue_close.rb +26 -0
  61. data/lib/puma/rack/builder.rb +4 -5
  62. data/lib/puma/rack/urlmap.rb +2 -0
  63. data/lib/puma/rack_default.rb +2 -0
  64. data/lib/puma/reactor.rb +87 -316
  65. data/lib/puma/request.rb +456 -0
  66. data/lib/puma/runner.rb +33 -52
  67. data/lib/puma/server.rb +288 -679
  68. data/lib/puma/single.rb +13 -67
  69. data/lib/puma/state_file.rb +10 -3
  70. data/lib/puma/systemd.rb +46 -0
  71. data/lib/puma/thread_pool.rb +131 -81
  72. data/lib/puma/util.rb +14 -6
  73. data/lib/puma.rb +54 -0
  74. data/lib/rack/handler/puma.rb +8 -6
  75. data/tools/Dockerfile +16 -0
  76. data/tools/trickletest.rb +0 -1
  77. metadata +45 -29
  78. data/ext/puma_http11/io_buffer.c +0 -155
  79. data/lib/puma/accept_nonblock.rb +0 -23
  80. data/lib/puma/compat.rb +0 -14
  81. data/lib/puma/convenient.rb +0 -23
  82. data/lib/puma/daemon_ext.rb +0 -31
  83. data/lib/puma/delegation.rb +0 -11
  84. data/lib/puma/java_io_buffer.rb +0 -45
  85. data/lib/puma/rack/backports/uri/common_193.rb +0 -33
  86. data/lib/puma/tcp_logger.rb +0 -39
  87. data/tools/jungle/README.md +0 -19
  88. data/tools/jungle/init.d/README.md +0 -61
  89. data/tools/jungle/init.d/puma +0 -421
  90. data/tools/jungle/init.d/run-puma +0 -18
  91. data/tools/jungle/upstart/README.md +0 -61
  92. data/tools/jungle/upstart/puma-manager.conf +0 -31
  93. data/tools/jungle/upstart/puma.conf +0 -69
data/lib/puma/reactor.rb CHANGED
@@ -1,345 +1,116 @@
1
- require 'puma/util'
2
- require 'puma/minissl'
1
+ # frozen_string_literal: true
2
+
3
+ require 'puma/queue_close' unless ::Queue.instance_methods.include? :close
3
4
 
4
5
  module Puma
5
- # Internal Docs, Not a public interface.
6
- #
7
- # The Reactor object is responsible for ensuring that a request has been
8
- # completely received before it starts to be processed. This may be known as read buffering.
9
- # If read buffering is not done, and no other read buffering is performed (such as by an application server
10
- # such as nginx) then the application would be subject to a slow client attack.
11
- #
12
- # Each Puma "worker" process has its own Reactor. For example if you start puma with `$ puma -w 5` then
13
- # it will have 5 workers and each worker will have it's own reactor.
14
- #
15
- # For a graphical representation of how the reactor works see [architecture.md](https://github.com/puma/puma/blob/master/docs/architecture.md#connection-pipeline).
16
- #
17
- # ## Reactor Flow
18
- #
19
- # A request comes into a `Puma::Server` instance, it is then passed to a `Puma::Reactor` instance.
20
- # The reactor stores the request in an array and calls `IO.select` on the array in a loop.
6
+ class UnsupportedBackend < StandardError; end
7
+
8
+ # Monitors a collection of IO objects, calling a block whenever
9
+ # any monitored object either receives data or times out, or when the Reactor shuts down.
21
10
  #
22
- # When the request is written to by the client then the `IO.select` will "wake up" and
23
- # return the references to any objects that caused it to "wake". The reactor
24
- # then loops through each of these request objects, and sees if they're complete. If they
25
- # have a full header and body then the reactor passes the request to a thread pool.
26
- # Once in a thread pool, a "worker thread" can run the the application's Ruby code against the request.
11
+ # The waiting/wake up is performed with nio4r, which will use the appropriate backend (libev,
12
+ # Java NIO or just plain IO#select). The call to `NIO::Selector#select` will
13
+ # 'wakeup' any IO object that receives data.
27
14
  #
28
- # If the request is not complete, then it stays in the array, and the next time any
29
- # data is written to that socket reference, then the loop is woken up and it is checked for completeness again.
15
+ # This class additionally tracks a timeout for every added object,
16
+ # and wakes up any object when its timeout elapses.
30
17
  #
31
- # A detailed example is given in the docs for `run_internal` which is where the bulk
32
- # of this logic lives.
18
+ # The implementation uses a Queue to synchronize adding new objects from the internal select loop.
33
19
  class Reactor
34
- DefaultSleepFor = 5
35
-
36
- # Creates an instance of Puma::Reactor
37
- #
38
- # The `server` argument is an instance of `Puma::Server`
39
- # this is used to write a response for "low level errors"
40
- # when there is an exception inside of the reactor.
41
- #
42
- # The `app_pool` is an instance of `Puma::ThreadPool`.
43
- # Once a request is fully formed (header and body are received)
44
- # it will be passed to the `app_pool`.
45
- def initialize(server, app_pool)
46
- @server = server
47
- @events = server.events
48
- @app_pool = app_pool
49
-
50
- @mutex = Mutex.new
51
-
52
- # Read / Write pipes to wake up internal while loop
53
- @ready, @trigger = Puma::Util.pipe
54
- @input = []
55
- @sleep_for = DefaultSleepFor
20
+ # Create a new Reactor to monitor IO objects added by #add.
21
+ # The provided block will be invoked when an IO has data available to read,
22
+ # its timeout elapses, or when the Reactor shuts down.
23
+ def initialize(backend, &block)
24
+ require 'nio'
25
+ unless backend == :auto || NIO::Selector.backends.include?(backend)
26
+ raise "unsupported IO selector backend: #{backend} (available backends: #{NIO::Selector.backends.join(', ')})"
27
+ end
28
+ @selector = backend == :auto ? NIO::Selector.new : NIO::Selector.new(backend)
29
+ @input = Queue.new
56
30
  @timeouts = []
57
-
58
- @sockets = [@ready]
31
+ @block = block
59
32
  end
60
33
 
61
- private
62
-
63
-
64
- # Until a request is added via the `add` method this method will internally
65
- # loop, waiting on the `sockets` array objects. The only object in this
66
- # array at first is the `@ready` IO object, which is the read end of a pipe
67
- # connected to `@trigger` object. When `@trigger` is written to, then the loop
68
- # will break on `IO.select` and return an array.
69
- #
70
- # ## When a request is added:
71
- #
72
- # When the `add` method is called, an instance of `Puma::Client` is added to the `@input` array.
73
- # Next the `@ready` pipe is "woken" by writing a string of `"*"` to `@trigger`.
74
- #
75
- # When that happens, the internal loop stops blocking at `IO.select` and returns a reference
76
- # to whatever "woke" it up. On the very first loop, the only thing in `sockets` is `@ready`.
77
- # When `@trigger` is written-to, the loop "wakes" and the `ready`
78
- # variable returns an array of arrays that looks like `[[#<IO:fd 10>], [], []]` where the
79
- # first IO object is the `@ready` object. This first array `[#<IO:fd 10>]`
80
- # is saved as a `reads` variable.
81
- #
82
- # The `reads` variable is iterated through. In the case that the object
83
- # is the same as the `@ready` input pipe, then we know that there was a `trigger` event.
84
- #
85
- # If there was a trigger event, then one byte of `@ready` is read into memory. In the case of the first request,
86
- # the reactor sees that it's a `"*"` value and the reactor adds the contents of `@input` into the `sockets` array.
87
- # The while then loop continues to iterate again, but now the `sockets` array contains a `Puma::Client` instance in addition
88
- # to the `@ready` IO object. For example: `[#<IO:fd 10>, #<Puma::Client:0x3fdc1103bee8 @ready=false>]`.
89
- #
90
- # Since the `Puma::Client` in this example has data that has not been read yet,
91
- # the `IO.select` is immediately able to "wake" and read from the `Puma::Client`. At this point the
92
- # `ready` output looks like this: `[[#<Puma::Client:0x3fdc1103bee8 @ready=false>], [], []]`.
93
- #
94
- # Each element in the first entry is iterated over. The `Puma::Client` object is not
95
- # the `@ready` pipe, so the reactor checks to see if it has the fully header and body with
96
- # the `Puma::Client#try_to_finish` method. If the full request has been sent,
97
- # then the request is passed off to the `@app_pool` thread pool so that a "worker thread"
98
- # can pick up the request and begin to execute application logic. This is done
99
- # via `@app_pool << c`. The `Puma::Client` is then removed from the `sockets` array.
100
- #
101
- # If the request body is not present then nothing will happen, and the loop will iterate
102
- # again. When the client sends more data to the socket the `Puma::Client` object will
103
- # wake up the `IO.select` and it can again be checked to see if it's ready to be
104
- # passed to the thread pool.
105
- #
106
- # ## Time Out Case
107
- #
108
- # In addition to being woken via a write to one of the sockets the `IO.select` will
109
- # periodically "time out" of the sleep. One of the functions of this is to check for
110
- # any requests that have "timed out". At the end of the loop it's checked to see if
111
- # the first element in the `@timeout` array has exceed it's allowed time. If so,
112
- # the client object is removed from the timeout aray, a 408 response is written.
113
- # Then it's connection is closed, and the object is removed from the `sockets` array
114
- # that watches for new data.
115
- #
116
- # This behavior loops until all the objects that have timed out have been removed.
117
- #
118
- # Once all the timeouts have been processed, the next duration of the `IO.select` sleep
119
- # will be set to be equal to the amount of time it will take for the next timeout to occur.
120
- # This calculation happens in `calculate_sleep`.
121
- def run_internal
122
- sockets = @sockets
123
-
124
- while true
125
- begin
126
- ready = IO.select sockets, nil, nil, @sleep_for
127
- rescue IOError => e
128
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
129
- if sockets.any? { |socket| socket.closed? }
130
- STDERR.puts "Error in select: #{e.message} (#{e.class})"
131
- STDERR.puts e.backtrace
132
- sockets = sockets.reject { |socket| socket.closed? }
133
- retry
134
- else
135
- raise
136
- end
137
- end
138
-
139
- if ready and reads = ready[0]
140
- reads.each do |c|
141
- if c == @ready
142
- @mutex.synchronize do
143
- case @ready.read(1)
144
- when "*"
145
- sockets += @input
146
- @input.clear
147
- when "c"
148
- sockets.delete_if do |s|
149
- if s == @ready
150
- false
151
- else
152
- s.close
153
- true
154
- end
155
- end
156
- when "!"
157
- return
158
- end
159
- end
160
- else
161
- # We have to be sure to remove it from the timeout
162
- # list or we'll accidentally close the socket when
163
- # it's in use!
164
- if c.timeout_at
165
- @mutex.synchronize do
166
- @timeouts.delete c
167
- end
168
- end
169
-
170
- begin
171
- if c.try_to_finish
172
- @app_pool << c
173
- sockets.delete c
174
- end
175
-
176
- # Don't report these to the lowlevel_error handler, otherwise
177
- # will be flooding them with errors when persistent connections
178
- # are closed.
179
- rescue ConnectionError
180
- c.write_500
181
- c.close
182
-
183
- sockets.delete c
184
-
185
- # SSL handshake failure
186
- rescue MiniSSL::SSLError => e
187
- @server.lowlevel_error(e, c.env)
188
-
189
- ssl_socket = c.io
190
- addr = ssl_socket.peeraddr.last
191
- cert = ssl_socket.peercert
192
-
193
- c.close
194
- sockets.delete c
195
-
196
- @events.ssl_error @server, addr, cert, e
197
-
198
- # The client doesn't know HTTP well
199
- rescue HttpParserError => e
200
- @server.lowlevel_error(e, c.env)
201
-
202
- c.write_400
203
- c.close
204
-
205
- sockets.delete c
206
-
207
- @events.parse_error @server, c.env, e
208
- rescue StandardError => e
209
- @server.lowlevel_error(e, c.env)
210
-
211
- c.write_500
212
- c.close
213
-
214
- sockets.delete c
215
- end
216
- end
217
- end
218
- end
219
-
220
- unless @timeouts.empty?
221
- @mutex.synchronize do
222
- now = Time.now
223
-
224
- while @timeouts.first.timeout_at < now
225
- c = @timeouts.shift
226
- c.write_408 if c.in_data_phase
227
- c.close
228
- sockets.delete c
229
-
230
- break if @timeouts.empty?
231
- end
232
-
233
- calculate_sleep
234
- end
34
+ # Run the internal select loop, using a background thread by default.
35
+ def run(background=true)
36
+ if background
37
+ @thread = Thread.new do
38
+ Puma.set_thread_name "reactor"
39
+ select_loop
235
40
  end
41
+ else
42
+ select_loop
236
43
  end
237
44
  end
238
45
 
239
- public
240
-
241
- def run
242
- run_internal
243
- ensure
244
- @trigger.close
245
- @ready.close
246
- end
247
-
248
- def run_in_thread
249
- @thread = Thread.new do
250
- begin
251
- run_internal
252
- rescue StandardError => e
253
- STDERR.puts "Error in reactor loop escaped: #{e.message} (#{e.class})"
254
- STDERR.puts e.backtrace
255
- retry
256
- ensure
257
- @trigger.close
258
- @ready.close
259
- end
260
- end
46
+ # Add a new client to monitor.
47
+ # The object must respond to #timeout and #timeout_at.
48
+ # Returns false if the reactor is already shut down.
49
+ def add(client)
50
+ @input << client
51
+ @selector.wakeup
52
+ true
53
+ rescue ClosedQueueError
54
+ false
261
55
  end
262
56
 
263
- # The `calculate_sleep` sets the value that the `IO.select` will
264
- # sleep for in the main reactor loop when no sockets are being written to.
265
- #
266
- # The values kept in `@timeouts` are sorted so that the first timeout
267
- # comes first in the array. When there are no timeouts the default timeout is used.
268
- #
269
- # Otherwise a sleep value is set that is the same as the amount of time it
270
- # would take for the first element to time out.
271
- #
272
- # If that value is in the past, then a sleep value of zero is used.
273
- def calculate_sleep
274
- if @timeouts.empty?
275
- @sleep_for = DefaultSleepFor
276
- else
277
- diff = @timeouts.first.timeout_at.to_f - Time.now.to_f
278
-
279
- if diff < 0.0
280
- @sleep_for = 0
281
- else
282
- @sleep_for = diff
283
- end
57
+ # Shutdown the reactor, blocking until the background thread is finished.
58
+ def shutdown
59
+ @input.close
60
+ begin
61
+ @selector.wakeup
62
+ rescue IOError # Ignore if selector is already closed
284
63
  end
64
+ @thread.join if @thread
285
65
  end
286
66
 
287
- # This method adds a connection to the reactor
288
- #
289
- # Typically called by `Puma::Server` the value passed in
290
- # is usually a `Puma::Client` object that responds like an IO
291
- # object.
292
- #
293
- # The main body of the reactor loop is in `run_internal` and it
294
- # will sleep on `IO.select`. When a new connection is added to the
295
- # reactor it cannot be added directly to the `sockets` aray, because
296
- # the `IO.select` will not be watching for it yet.
297
- #
298
- # Instead what needs to happen is that `IO.select` needs to be woken up,
299
- # the contents of `@input` added to the `sockets` array, and then
300
- # another call to `IO.select` needs to happen. Since the `Puma::Client`
301
- # object can be read immediately, it does not block, but instead returns
302
- # right away.
303
- #
304
- # This behavior is accomplished by writing to `@trigger` which wakes up
305
- # the `IO.select` and then there is logic to detect the value of `*`,
306
- # pull the contents from `@input` and add them to the sockets array.
307
- #
308
- # If the object passed in has a timeout value in `timeout_at` then
309
- # it is added to a `@timeouts` array. This array is then re-arranged
310
- # so that the first element to timeout will be at the front of the
311
- # array. Then a value to sleep for is derived in the call to `calculate_sleep`
312
- def add(c)
313
- @mutex.synchronize do
314
- @input << c
315
- @trigger << "*"
316
-
317
- if c.timeout_at
318
- @timeouts << c
319
- @timeouts.sort! { |a,b| a.timeout_at <=> b.timeout_at }
67
+ private
320
68
 
321
- calculate_sleep
69
+ def select_loop
70
+ begin
71
+ until @input.closed? && @input.empty?
72
+ # Wakeup any registered object that receives incoming data.
73
+ # Block until the earliest timeout or Selector#wakeup is called.
74
+ timeout = (earliest = @timeouts.first) && earliest.timeout
75
+ @selector.select(timeout) {|mon| wakeup!(mon.value)}
76
+
77
+ # Wakeup all objects that timed out.
78
+ timed_out = @timeouts.take_while {|t| t.timeout == 0}
79
+ timed_out.each(&method(:wakeup!))
80
+
81
+ unless @input.empty?
82
+ until @input.empty?
83
+ client = @input.pop
84
+ register(client) if client.io_ok?
85
+ end
86
+ @timeouts.sort_by!(&:timeout_at)
87
+ end
322
88
  end
89
+ rescue StandardError => e
90
+ STDERR.puts "Error in reactor loop escaped: #{e.message} (#{e.class})"
91
+ STDERR.puts e.backtrace
92
+ retry
323
93
  end
94
+ # Wakeup all remaining objects on shutdown.
95
+ @timeouts.each(&@block)
96
+ @selector.close
324
97
  end
325
98
 
326
- # Close all watched sockets and clear them from being watched
327
- def clear!
328
- begin
329
- @trigger << "c"
330
- rescue IOError
331
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
332
- end
99
+ # Start monitoring the object.
100
+ def register(client)
101
+ @selector.register(client.to_io, :r).value = client
102
+ @timeouts << client
103
+ rescue ArgumentError
104
+ # unreadable clients raise error when processed by NIO
333
105
  end
334
106
 
335
- def shutdown
336
- begin
337
- @trigger << "!"
338
- rescue IOError
339
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
107
+ # 'Wake up' a monitored object by calling the provided block.
108
+ # Stop monitoring the object if the block returns `true`.
109
+ def wakeup!(client)
110
+ if @block.call client
111
+ @selector.deregister client.to_io
112
+ @timeouts.delete client
340
113
  end
341
-
342
- @thread.join
343
114
  end
344
115
  end
345
116
  end