puma 3.11.3 → 3.12.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.
- checksums.yaml +5 -5
- data/History.md +40 -0
- data/README.md +15 -5
- data/docs/architecture.md +1 -1
- data/docs/restart.md +1 -1
- data/docs/systemd.md +10 -0
- data/ext/puma_http11/mini_ssl.c +22 -1
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +6 -0
- data/lib/puma/binder.rb +13 -9
- data/lib/puma/cli.rb +18 -7
- data/lib/puma/client.rb +14 -0
- data/lib/puma/cluster.rb +16 -1
- data/lib/puma/commonlogger.rb +2 -0
- data/lib/puma/configuration.rb +2 -0
- data/lib/puma/const.rb +4 -2
- data/lib/puma/control_cli.rb +13 -11
- data/lib/puma/convenient.rb +2 -0
- data/lib/puma/daemon_ext.rb +2 -0
- data/lib/puma/delegation.rb +2 -0
- data/lib/puma/detect.rb +2 -0
- data/lib/puma/dsl.rb +18 -8
- data/lib/puma/events.rb +2 -0
- data/lib/puma/io_buffer.rb +2 -0
- data/lib/puma/java_io_buffer.rb +2 -0
- data/lib/puma/jruby_restart.rb +2 -0
- data/lib/puma/launcher.rb +5 -2
- data/lib/puma/minissl.rb +7 -3
- data/lib/puma/null_io.rb +2 -0
- data/lib/puma/plugin.rb +2 -0
- data/lib/puma/rack/builder.rb +2 -1
- data/lib/puma/reactor.rb +134 -0
- data/lib/puma/runner.rb +10 -1
- data/lib/puma/server.rb +40 -4
- data/lib/puma/single.rb +12 -1
- data/lib/puma/state_file.rb +2 -0
- data/lib/puma/tcp_logger.rb +2 -0
- data/lib/puma/thread_pool.rb +46 -5
- data/lib/puma/util.rb +1 -0
- data/lib/puma.rb +8 -0
- data/lib/rack/handler/puma.rb +4 -0
- data/tools/jungle/README.md +10 -4
- data/tools/jungle/init.d/README.md +2 -0
- data/tools/jungle/init.d/puma +2 -2
- data/tools/jungle/init.d/run-puma +1 -1
- data/tools/jungle/rc.d/README.md +74 -0
- data/tools/jungle/rc.d/puma +61 -0
- data/tools/jungle/rc.d/puma.conf +10 -0
- metadata +11 -8
data/lib/puma/launcher.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'puma/events'
|
2
4
|
require 'puma/detect'
|
3
5
|
|
@@ -63,8 +65,8 @@ module Puma
|
|
63
65
|
|
64
66
|
generate_restart_data
|
65
67
|
|
66
|
-
if clustered? &&
|
67
|
-
unsupported
|
68
|
+
if clustered? && !Process.respond_to?(:fork)
|
69
|
+
unsupported "worker mode not supported on #{RUBY_ENGINE} on this platform"
|
68
70
|
end
|
69
71
|
|
70
72
|
if @options[:daemon] && Puma.windows?
|
@@ -86,6 +88,7 @@ module Puma
|
|
86
88
|
else
|
87
89
|
@runner = Single.new(self, @events)
|
88
90
|
end
|
91
|
+
Puma.stats_object = @runner
|
89
92
|
|
90
93
|
@status = :run
|
91
94
|
end
|
data/lib/puma/minissl.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
begin
|
2
4
|
require 'io/wait'
|
3
5
|
rescue LoadError
|
@@ -124,7 +126,7 @@ module Puma
|
|
124
126
|
|
125
127
|
def read_and_drop(timeout = 1)
|
126
128
|
return :timeout unless IO.select([@socket], nil, nil, timeout)
|
127
|
-
read_nonblock(1024)
|
129
|
+
return :eof unless read_nonblock(1024)
|
128
130
|
:drop
|
129
131
|
rescue Errno::EAGAIN
|
130
132
|
# do nothing
|
@@ -141,7 +143,7 @@ module Puma
|
|
141
143
|
# Don't let this socket hold this loop forever.
|
142
144
|
# If it can't send more packets within 1s, then give up.
|
143
145
|
while should_drop_bytes?
|
144
|
-
return if read_and_drop(1)
|
146
|
+
return if [:timeout, :eof].include?(read_and_drop(1))
|
145
147
|
end
|
146
148
|
rescue IOError, SystemCallError
|
147
149
|
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
@@ -180,6 +182,7 @@ module Puma
|
|
180
182
|
# jruby-specific Context properties: java uses a keystore and password pair rather than a cert/key pair
|
181
183
|
attr_reader :keystore
|
182
184
|
attr_accessor :keystore_pass
|
185
|
+
attr_accessor :ssl_cipher_list
|
183
186
|
|
184
187
|
def keystore=(keystore)
|
185
188
|
raise ArgumentError, "No such keystore file '#{keystore}'" unless File.exist? keystore
|
@@ -195,6 +198,7 @@ module Puma
|
|
195
198
|
attr_reader :key
|
196
199
|
attr_reader :cert
|
197
200
|
attr_reader :ca
|
201
|
+
attr_accessor :ssl_cipher_filter
|
198
202
|
|
199
203
|
def key=(key)
|
200
204
|
raise ArgumentError, "No such key file '#{key}'" unless File.exist? key
|
@@ -249,7 +253,7 @@ module Puma
|
|
249
253
|
end
|
250
254
|
|
251
255
|
def close
|
252
|
-
@socket.close
|
256
|
+
@socket.close unless @socket.closed? # closed? call is for Windows
|
253
257
|
end
|
254
258
|
end
|
255
259
|
end
|
data/lib/puma/null_io.rb
CHANGED
data/lib/puma/plugin.rb
CHANGED
data/lib/puma/rack/builder.rb
CHANGED
@@ -110,7 +110,8 @@ module Puma::Rack
|
|
110
110
|
|
111
111
|
has_options = false
|
112
112
|
server.valid_options.each do |name, description|
|
113
|
-
next if name.to_s
|
113
|
+
next if name.to_s =~ /^(Host|Port)[^a-zA-Z]/ # ignore handler's host and port options, we do our own.
|
114
|
+
|
114
115
|
info << " -O %-21s %s" % [name, description]
|
115
116
|
has_options = true
|
116
117
|
end
|
data/lib/puma/reactor.rb
CHANGED
@@ -1,16 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'puma/util'
|
2
4
|
require 'puma/minissl'
|
3
5
|
|
4
6
|
module Puma
|
7
|
+
# Internal Docs, Not a public interface.
|
8
|
+
#
|
9
|
+
# The Reactor object is responsible for ensuring that a request has been
|
10
|
+
# completely received before it starts to be processed. This may be known as read buffering.
|
11
|
+
# If read buffering is not done, and no other read buffering is performed (such as by an application server
|
12
|
+
# such as nginx) then the application would be subject to a slow client attack.
|
13
|
+
#
|
14
|
+
# Each Puma "worker" process has its own Reactor. For example if you start puma with `$ puma -w 5` then
|
15
|
+
# it will have 5 workers and each worker will have it's own reactor.
|
16
|
+
#
|
17
|
+
# For a graphical representation of how the reactor works see [architecture.md](https://github.com/puma/puma/blob/master/docs/architecture.md#connection-pipeline).
|
18
|
+
#
|
19
|
+
# ## Reactor Flow
|
20
|
+
#
|
21
|
+
# A request comes into a `Puma::Server` instance, it is then passed to a `Puma::Reactor` instance.
|
22
|
+
# The reactor stores the request in an array and calls `IO.select` on the array in a loop.
|
23
|
+
#
|
24
|
+
# When the request is written to by the client then the `IO.select` will "wake up" and
|
25
|
+
# return the references to any objects that caused it to "wake". The reactor
|
26
|
+
# then loops through each of these request objects, and sees if they're complete. If they
|
27
|
+
# have a full header and body then the reactor passes the request to a thread pool.
|
28
|
+
# Once in a thread pool, a "worker thread" can run the the application's Ruby code against the request.
|
29
|
+
#
|
30
|
+
# If the request is not complete, then it stays in the array, and the next time any
|
31
|
+
# data is written to that socket reference, then the loop is woken up and it is checked for completeness again.
|
32
|
+
#
|
33
|
+
# A detailed example is given in the docs for `run_internal` which is where the bulk
|
34
|
+
# of this logic lives.
|
5
35
|
class Reactor
|
6
36
|
DefaultSleepFor = 5
|
7
37
|
|
38
|
+
# Creates an instance of Puma::Reactor
|
39
|
+
#
|
40
|
+
# The `server` argument is an instance of `Puma::Server`
|
41
|
+
# this is used to write a response for "low level errors"
|
42
|
+
# when there is an exception inside of the reactor.
|
43
|
+
#
|
44
|
+
# The `app_pool` is an instance of `Puma::ThreadPool`.
|
45
|
+
# Once a request is fully formed (header and body are received)
|
46
|
+
# it will be passed to the `app_pool`.
|
8
47
|
def initialize(server, app_pool)
|
9
48
|
@server = server
|
10
49
|
@events = server.events
|
11
50
|
@app_pool = app_pool
|
12
51
|
|
13
52
|
@mutex = Mutex.new
|
53
|
+
|
54
|
+
# Read / Write pipes to wake up internal while loop
|
14
55
|
@ready, @trigger = Puma::Util.pipe
|
15
56
|
@input = []
|
16
57
|
@sleep_for = DefaultSleepFor
|
@@ -21,6 +62,64 @@ module Puma
|
|
21
62
|
|
22
63
|
private
|
23
64
|
|
65
|
+
|
66
|
+
# Until a request is added via the `add` method this method will internally
|
67
|
+
# loop, waiting on the `sockets` array objects. The only object in this
|
68
|
+
# array at first is the `@ready` IO object, which is the read end of a pipe
|
69
|
+
# connected to `@trigger` object. When `@trigger` is written to, then the loop
|
70
|
+
# will break on `IO.select` and return an array.
|
71
|
+
#
|
72
|
+
# ## When a request is added:
|
73
|
+
#
|
74
|
+
# When the `add` method is called, an instance of `Puma::Client` is added to the `@input` array.
|
75
|
+
# Next the `@ready` pipe is "woken" by writing a string of `"*"` to `@trigger`.
|
76
|
+
#
|
77
|
+
# When that happens, the internal loop stops blocking at `IO.select` and returns a reference
|
78
|
+
# to whatever "woke" it up. On the very first loop, the only thing in `sockets` is `@ready`.
|
79
|
+
# When `@trigger` is written-to, the loop "wakes" and the `ready`
|
80
|
+
# variable returns an array of arrays that looks like `[[#<IO:fd 10>], [], []]` where the
|
81
|
+
# first IO object is the `@ready` object. This first array `[#<IO:fd 10>]`
|
82
|
+
# is saved as a `reads` variable.
|
83
|
+
#
|
84
|
+
# The `reads` variable is iterated through. In the case that the object
|
85
|
+
# is the same as the `@ready` input pipe, then we know that there was a `trigger` event.
|
86
|
+
#
|
87
|
+
# If there was a trigger event, then one byte of `@ready` is read into memory. In the case of the first request,
|
88
|
+
# the reactor sees that it's a `"*"` value and the reactor adds the contents of `@input` into the `sockets` array.
|
89
|
+
# The while then loop continues to iterate again, but now the `sockets` array contains a `Puma::Client` instance in addition
|
90
|
+
# to the `@ready` IO object. For example: `[#<IO:fd 10>, #<Puma::Client:0x3fdc1103bee8 @ready=false>]`.
|
91
|
+
#
|
92
|
+
# Since the `Puma::Client` in this example has data that has not been read yet,
|
93
|
+
# the `IO.select` is immediately able to "wake" and read from the `Puma::Client`. At this point the
|
94
|
+
# `ready` output looks like this: `[[#<Puma::Client:0x3fdc1103bee8 @ready=false>], [], []]`.
|
95
|
+
#
|
96
|
+
# Each element in the first entry is iterated over. The `Puma::Client` object is not
|
97
|
+
# the `@ready` pipe, so the reactor checks to see if it has the fully header and body with
|
98
|
+
# the `Puma::Client#try_to_finish` method. If the full request has been sent,
|
99
|
+
# then the request is passed off to the `@app_pool` thread pool so that a "worker thread"
|
100
|
+
# can pick up the request and begin to execute application logic. This is done
|
101
|
+
# via `@app_pool << c`. The `Puma::Client` is then removed from the `sockets` array.
|
102
|
+
#
|
103
|
+
# If the request body is not present then nothing will happen, and the loop will iterate
|
104
|
+
# again. When the client sends more data to the socket the `Puma::Client` object will
|
105
|
+
# wake up the `IO.select` and it can again be checked to see if it's ready to be
|
106
|
+
# passed to the thread pool.
|
107
|
+
#
|
108
|
+
# ## Time Out Case
|
109
|
+
#
|
110
|
+
# In addition to being woken via a write to one of the sockets the `IO.select` will
|
111
|
+
# periodically "time out" of the sleep. One of the functions of this is to check for
|
112
|
+
# any requests that have "timed out". At the end of the loop it's checked to see if
|
113
|
+
# the first element in the `@timeout` array has exceed it's allowed time. If so,
|
114
|
+
# the client object is removed from the timeout aray, a 408 response is written.
|
115
|
+
# Then it's connection is closed, and the object is removed from the `sockets` array
|
116
|
+
# that watches for new data.
|
117
|
+
#
|
118
|
+
# This behavior loops until all the objects that have timed out have been removed.
|
119
|
+
#
|
120
|
+
# Once all the timeouts have been processed, the next duration of the `IO.select` sleep
|
121
|
+
# will be set to be equal to the amount of time it will take for the next timeout to occur.
|
122
|
+
# This calculation happens in `calculate_sleep`.
|
24
123
|
def run_internal
|
25
124
|
sockets = @sockets
|
26
125
|
|
@@ -163,6 +262,16 @@ module Puma
|
|
163
262
|
end
|
164
263
|
end
|
165
264
|
|
265
|
+
# The `calculate_sleep` sets the value that the `IO.select` will
|
266
|
+
# sleep for in the main reactor loop when no sockets are being written to.
|
267
|
+
#
|
268
|
+
# The values kept in `@timeouts` are sorted so that the first timeout
|
269
|
+
# comes first in the array. When there are no timeouts the default timeout is used.
|
270
|
+
#
|
271
|
+
# Otherwise a sleep value is set that is the same as the amount of time it
|
272
|
+
# would take for the first element to time out.
|
273
|
+
#
|
274
|
+
# If that value is in the past, then a sleep value of zero is used.
|
166
275
|
def calculate_sleep
|
167
276
|
if @timeouts.empty?
|
168
277
|
@sleep_for = DefaultSleepFor
|
@@ -177,6 +286,31 @@ module Puma
|
|
177
286
|
end
|
178
287
|
end
|
179
288
|
|
289
|
+
# This method adds a connection to the reactor
|
290
|
+
#
|
291
|
+
# Typically called by `Puma::Server` the value passed in
|
292
|
+
# is usually a `Puma::Client` object that responds like an IO
|
293
|
+
# object.
|
294
|
+
#
|
295
|
+
# The main body of the reactor loop is in `run_internal` and it
|
296
|
+
# will sleep on `IO.select`. When a new connection is added to the
|
297
|
+
# reactor it cannot be added directly to the `sockets` aray, because
|
298
|
+
# the `IO.select` will not be watching for it yet.
|
299
|
+
#
|
300
|
+
# Instead what needs to happen is that `IO.select` needs to be woken up,
|
301
|
+
# the contents of `@input` added to the `sockets` array, and then
|
302
|
+
# another call to `IO.select` needs to happen. Since the `Puma::Client`
|
303
|
+
# object can be read immediately, it does not block, but instead returns
|
304
|
+
# right away.
|
305
|
+
#
|
306
|
+
# This behavior is accomplished by writing to `@trigger` which wakes up
|
307
|
+
# the `IO.select` and then there is logic to detect the value of `*`,
|
308
|
+
# pull the contents from `@input` and add them to the sockets array.
|
309
|
+
#
|
310
|
+
# If the object passed in has a timeout value in `timeout_at` then
|
311
|
+
# it is added to a `@timeouts` array. This array is then re-arranged
|
312
|
+
# so that the first element to timeout will be at the front of the
|
313
|
+
# array. Then a value to sleep for is derived in the call to `calculate_sleep`
|
180
314
|
def add(c)
|
181
315
|
@mutex.synchronize do
|
182
316
|
@input << c
|
data/lib/puma/runner.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'puma/server'
|
2
4
|
require 'puma/const'
|
3
5
|
|
4
6
|
module Puma
|
7
|
+
# Generic class that is used by `Puma::Cluster` and `Puma::Single` to
|
8
|
+
# serve requests. This class spawns a new instance of `Puma::Server` via
|
9
|
+
# a call to `start_server`.
|
5
10
|
class Runner
|
6
11
|
def initialize(cli, events)
|
7
12
|
@launcher = cli
|
@@ -19,6 +24,10 @@ module Puma
|
|
19
24
|
@options[:environment] == "development"
|
20
25
|
end
|
21
26
|
|
27
|
+
def test?
|
28
|
+
@options[:environment] == "test"
|
29
|
+
end
|
30
|
+
|
22
31
|
def log(str)
|
23
32
|
@events.log str
|
24
33
|
end
|
@@ -165,7 +174,7 @@ module Puma
|
|
165
174
|
server.early_hints = true
|
166
175
|
end
|
167
176
|
|
168
|
-
unless development?
|
177
|
+
unless development? || test?
|
169
178
|
server.leak_stack_on_error = false
|
170
179
|
end
|
171
180
|
|
data/lib/puma/server.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'stringio'
|
2
4
|
|
3
5
|
require 'puma/thread_pool'
|
@@ -23,6 +25,15 @@ require 'socket'
|
|
23
25
|
module Puma
|
24
26
|
|
25
27
|
# The HTTP Server itself. Serves out a single Rack app.
|
28
|
+
#
|
29
|
+
# This class is used by the `Puma::Single` and `Puma::Cluster` classes
|
30
|
+
# to generate one or more `Puma::Server` instances capable of handling requests.
|
31
|
+
# Each Puma process will contain one `Puma::Server` instacne.
|
32
|
+
#
|
33
|
+
# The `Puma::Server` instance pulls requests from the socket, adds them to a
|
34
|
+
# `Puma::Reactor` where they get eventually passed to a `Puma::ThreadPool`.
|
35
|
+
#
|
36
|
+
# Each `Puma::Server` will have one reactor and one thread pool.
|
26
37
|
class Server
|
27
38
|
|
28
39
|
include Puma::Const
|
@@ -159,6 +170,18 @@ module Puma
|
|
159
170
|
@thread_pool and @thread_pool.spawned
|
160
171
|
end
|
161
172
|
|
173
|
+
|
174
|
+
# This number represents the number of requests that
|
175
|
+
# the server is capable of taking right now.
|
176
|
+
#
|
177
|
+
# For example if the number is 5 then it means
|
178
|
+
# there are 5 threads sitting idle ready to take
|
179
|
+
# a request. If one request comes in, then the
|
180
|
+
# value would be 4 until it finishes processing.
|
181
|
+
def pool_capacity
|
182
|
+
@thread_pool and @thread_pool.pool_capacity
|
183
|
+
end
|
184
|
+
|
162
185
|
# Lopez Mode == raw tcp apps
|
163
186
|
|
164
187
|
def run_lopez_mode(background=true)
|
@@ -220,7 +243,11 @@ module Puma
|
|
220
243
|
# nothing
|
221
244
|
rescue Errno::ECONNABORTED
|
222
245
|
# client closed the socket even before accept
|
223
|
-
|
246
|
+
begin
|
247
|
+
io.close
|
248
|
+
rescue
|
249
|
+
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
250
|
+
end
|
224
251
|
end
|
225
252
|
end
|
226
253
|
end
|
@@ -237,7 +264,12 @@ module Puma
|
|
237
264
|
STDERR.puts "Exception handling servers: #{e.message} (#{e.class})"
|
238
265
|
STDERR.puts e.backtrace
|
239
266
|
ensure
|
240
|
-
|
267
|
+
begin
|
268
|
+
@check.close
|
269
|
+
rescue
|
270
|
+
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
271
|
+
end
|
272
|
+
|
241
273
|
@notify.close
|
242
274
|
|
243
275
|
if @status != :restart and @own_binder
|
@@ -372,7 +404,11 @@ module Puma
|
|
372
404
|
# nothing
|
373
405
|
rescue Errno::ECONNABORTED
|
374
406
|
# client closed the socket even before accept
|
375
|
-
|
407
|
+
begin
|
408
|
+
io.close
|
409
|
+
rescue
|
410
|
+
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
411
|
+
end
|
376
412
|
end
|
377
413
|
end
|
378
414
|
end
|
@@ -606,7 +642,7 @@ module Puma
|
|
606
642
|
fast_write client, "#{k}: #{v}\r\n"
|
607
643
|
end
|
608
644
|
else
|
609
|
-
fast_write client, "#{k}: #{
|
645
|
+
fast_write client, "#{k}: #{vs}\r\n"
|
610
646
|
end
|
611
647
|
end
|
612
648
|
|
data/lib/puma/single.rb
CHANGED
@@ -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
17
|
b = @server.backlog || 0
|
9
18
|
r = @server.running || 0
|
10
|
-
|
19
|
+
t = @server.pool_capacity || 0
|
20
|
+
m = @server.max_threads || 0
|
21
|
+
%Q!{ "backlog": #{b}, "running": #{r}, "pool_capacity": #{t}, "max_threads": #{m} }!
|
11
22
|
end
|
12
23
|
|
13
24
|
def restart
|
data/lib/puma/state_file.rb
CHANGED
data/lib/puma/tcp_logger.rb
CHANGED
data/lib/puma/thread_pool.rb
CHANGED
@@ -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.
|
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.
|
5
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,7 +60,7 @@ 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
|
@@ -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!
|
@@ -153,16 +168,42 @@ module Puma
|
|
153
168
|
end
|
154
169
|
end
|
155
170
|
|
171
|
+
# This method is used by `Puma::Server` to let the server know when
|
172
|
+
# the thread pool can pull more requests from the socket and
|
173
|
+
# pass to the reactor.
|
174
|
+
#
|
175
|
+
# The general idea is that the thread pool can only work on a fixed
|
176
|
+
# number of requests at the same time. If it is already processing that
|
177
|
+
# number of requests then it is at capacity. If another Puma process has
|
178
|
+
# spare capacity, then the request can be left on the socket so the other
|
179
|
+
# worker can pick it up and process it.
|
180
|
+
#
|
181
|
+
# For example: if there are 5 threads, but only 4 working on
|
182
|
+
# requests, this method will not wait and the `Puma::Server`
|
183
|
+
# can pull a request right away.
|
184
|
+
#
|
185
|
+
# If there are 5 threads and all 5 of them are busy, then it will
|
186
|
+
# pause here, and wait until the `not_full` condition variable is
|
187
|
+
# signaled, usually this indicates that a request has been processed.
|
188
|
+
#
|
189
|
+
# It's important to note that even though the server might accept another
|
190
|
+
# request, it might not be added to the `@todo` array right away.
|
191
|
+
# For example if a slow client has only sent a header, but not a body
|
192
|
+
# then the `@todo` array would stay the same size as the reactor works
|
193
|
+
# to try to buffer the request. In tha scenario the next call to this
|
194
|
+
# method would not block and another request would be added into the reactor
|
195
|
+
# by the server. This would continue until a fully bufferend request
|
196
|
+
# makes it through the reactor and can then be processed by the thread pool.
|
156
197
|
def wait_until_not_full
|
157
198
|
@mutex.synchronize do
|
158
199
|
while true
|
159
200
|
return if @shutdown
|
160
|
-
return if @waiting > 0
|
161
201
|
|
162
202
|
# If we can still spin up new threads and there
|
163
|
-
# is work queued
|
203
|
+
# is work queued that cannot be handled by waiting
|
204
|
+
# threads, then accept more work until we would
|
164
205
|
# spin up the max number of threads.
|
165
|
-
return if @todo.size < @max - @spawned
|
206
|
+
return if @todo.size - @waiting < @max - @spawned
|
166
207
|
|
167
208
|
@not_full.wait @mutex
|
168
209
|
end
|
data/lib/puma/util.rb
CHANGED
data/lib/puma.rb
CHANGED
data/lib/rack/handler/puma.rb
CHANGED
@@ -9,6 +9,7 @@ module Rack
|
|
9
9
|
}
|
10
10
|
|
11
11
|
def self.config(app, options = {})
|
12
|
+
require 'puma'
|
12
13
|
require 'puma/configuration'
|
13
14
|
require 'puma/events'
|
14
15
|
require 'puma/launcher'
|
@@ -48,6 +49,9 @@ module Rack
|
|
48
49
|
self.set_host_port_to_config(host, port, user_config)
|
49
50
|
end
|
50
51
|
|
52
|
+
if default_options[:Host]
|
53
|
+
file_config.set_default_host(default_options[:Host])
|
54
|
+
end
|
51
55
|
self.set_host_port_to_config(default_options[:Host], default_options[:Port], default_config)
|
52
56
|
|
53
57
|
user_config.app app
|
data/tools/jungle/README.md
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# Puma as a service
|
2
2
|
|
3
|
-
## Init.d
|
4
|
-
|
5
|
-
See `/tools/jungle/init.d` for tools to use with init.d and start-stop-daemon.
|
6
|
-
|
7
3
|
## Upstart
|
8
4
|
|
9
5
|
See `/tools/jungle/upstart` for Ubuntu's upstart scripts.
|
@@ -11,3 +7,13 @@ See `/tools/jungle/upstart` for Ubuntu's upstart scripts.
|
|
11
7
|
## Systemd
|
12
8
|
|
13
9
|
See [/docs/systemd](https://github.com/puma/puma/blob/master/docs/systemd.md).
|
10
|
+
|
11
|
+
## Init.d
|
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
|
+
|
15
|
+
See `/tools/jungle/init.d` for tools to use with init.d and start-stop-daemon.
|
16
|
+
|
17
|
+
## rc.d
|
18
|
+
|
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
|
data/tools/jungle/init.d/puma
CHANGED
@@ -48,7 +48,7 @@ do_start_one() {
|
|
48
48
|
if [ -e $PIDFILE ]; then
|
49
49
|
PID=`cat $PIDFILE`
|
50
50
|
# If the puma isn't running, run it, otherwise restart it.
|
51
|
-
if
|
51
|
+
if ps -p $PID > /dev/null; then
|
52
52
|
do_start_one_do $1
|
53
53
|
else
|
54
54
|
do_restart_one $1
|
@@ -105,7 +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
|
108
|
+
if ps -p $PID > /dev/null; then
|
109
109
|
log_daemon_msg "---> Puma $1 isn't running."
|
110
110
|
else
|
111
111
|
log_daemon_msg "---> About to kill PID `cat $PIDFILE`"
|