puma 5.6.4 → 6.0.2
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 +4 -4
- data/History.md +163 -3
- data/README.md +21 -17
- data/bin/puma-wild +1 -1
- data/docs/compile_options.md +34 -0
- data/docs/fork_worker.md +1 -3
- data/docs/nginx.md +1 -1
- data/docs/testing_benchmarks_local_files.md +150 -0
- data/docs/testing_test_rackup_ci_files.md +36 -0
- data/ext/puma_http11/extconf.rb +18 -10
- data/ext/puma_http11/http11_parser.c +1 -1
- data/ext/puma_http11/http11_parser.h +1 -1
- data/ext/puma_http11/http11_parser.java.rl +2 -2
- data/ext/puma_http11/http11_parser.rl +2 -2
- data/ext/puma_http11/http11_parser_common.rl +2 -2
- data/ext/puma_http11/mini_ssl.c +63 -24
- data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +1 -1
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +166 -65
- data/ext/puma_http11/puma_http11.c +17 -9
- data/lib/puma/app/status.rb +6 -3
- data/lib/puma/binder.rb +37 -43
- data/lib/puma/cli.rb +11 -17
- data/lib/puma/client.rb +26 -13
- data/lib/puma/cluster/worker.rb +13 -11
- data/lib/puma/cluster/worker_handle.rb +4 -1
- data/lib/puma/cluster.rb +31 -30
- data/lib/puma/configuration.rb +74 -58
- data/lib/puma/const.rb +76 -88
- data/lib/puma/control_cli.rb +21 -18
- data/lib/puma/detect.rb +2 -0
- data/lib/puma/dsl.rb +97 -49
- data/lib/puma/error_logger.rb +17 -9
- data/lib/puma/events.rb +6 -126
- data/lib/puma/io_buffer.rb +39 -4
- data/lib/puma/jruby_restart.rb +2 -1
- data/lib/puma/launcher/bundle_pruner.rb +104 -0
- data/lib/puma/launcher.rb +107 -156
- data/lib/puma/log_writer.rb +137 -0
- data/lib/puma/minissl/context_builder.rb +23 -12
- data/lib/puma/minissl.rb +91 -15
- data/lib/puma/null_io.rb +5 -0
- data/lib/puma/plugin/tmp_restart.rb +1 -1
- data/lib/puma/rack/builder.rb +4 -4
- data/lib/puma/rack_default.rb +1 -1
- data/lib/puma/reactor.rb +4 -4
- data/lib/puma/request.rb +334 -162
- data/lib/puma/runner.rb +45 -20
- data/lib/puma/server.rb +55 -69
- data/lib/puma/single.rb +11 -11
- data/lib/puma/state_file.rb +2 -4
- data/lib/puma/systemd.rb +3 -2
- data/lib/puma/thread_pool.rb +16 -16
- data/lib/puma/util.rb +12 -14
- data/lib/puma.rb +12 -11
- data/lib/rack/handler/puma.rb +9 -9
- metadata +7 -3
- data/lib/puma/queue_close.rb +0 -26
data/lib/puma/runner.rb
CHANGED
@@ -1,23 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require_relative 'server'
|
4
|
+
require_relative 'const'
|
5
5
|
|
6
6
|
module Puma
|
7
7
|
# Generic class that is used by `Puma::Cluster` and `Puma::Single` to
|
8
8
|
# serve requests. This class spawns a new instance of `Puma::Server` via
|
9
9
|
# a call to `start_server`.
|
10
10
|
class Runner
|
11
|
-
def initialize(
|
12
|
-
@launcher =
|
13
|
-
@
|
14
|
-
@
|
11
|
+
def initialize(launcher)
|
12
|
+
@launcher = launcher
|
13
|
+
@log_writer = launcher.log_writer
|
14
|
+
@events = launcher.events
|
15
|
+
@config = launcher.config
|
16
|
+
@options = launcher.options
|
15
17
|
@app = nil
|
16
18
|
@control = nil
|
17
19
|
@started_at = Time.now
|
18
20
|
@wakeup = nil
|
19
21
|
end
|
20
22
|
|
23
|
+
# Returns the hash of configuration options.
|
24
|
+
# @return [Puma::UserFileDefaultOptions]
|
25
|
+
attr_reader :options
|
26
|
+
|
21
27
|
def wakeup!
|
22
28
|
return unless @wakeup
|
23
29
|
|
@@ -36,27 +42,27 @@ module Puma
|
|
36
42
|
end
|
37
43
|
|
38
44
|
def log(str)
|
39
|
-
@
|
45
|
+
@log_writer.log str
|
40
46
|
end
|
41
47
|
|
42
48
|
# @version 5.0.0
|
43
49
|
def stop_control
|
44
|
-
@control
|
50
|
+
@control&.stop true
|
45
51
|
end
|
46
52
|
|
47
53
|
def error(str)
|
48
|
-
@
|
54
|
+
@log_writer.error str
|
49
55
|
end
|
50
56
|
|
51
57
|
def debug(str)
|
52
|
-
@
|
58
|
+
@log_writer.log "- #{str}" if @options[:debug]
|
53
59
|
end
|
54
60
|
|
55
61
|
def start_control
|
56
62
|
str = @options[:control_url]
|
57
63
|
return unless str
|
58
64
|
|
59
|
-
|
65
|
+
require_relative 'app/status'
|
60
66
|
|
61
67
|
if token = @options[:control_auth_token]
|
62
68
|
token = nil if token.empty? || token == 'none'
|
@@ -64,10 +70,12 @@ module Puma
|
|
64
70
|
|
65
71
|
app = Puma::App::Status.new @launcher, token
|
66
72
|
|
67
|
-
|
68
|
-
|
73
|
+
# A Reactor is not created aand nio4r is not loaded when 'queue_requests: false'
|
74
|
+
# Use `nil` for events, no hooks in control server
|
75
|
+
control = Puma::Server.new app, nil,
|
76
|
+
{ min_threads: 0, max_threads: 1, queue_requests: false, log_writer: @log_writer }
|
69
77
|
|
70
|
-
control.binder.parse [str],
|
78
|
+
control.binder.parse [str], nil, 'Starting control server'
|
71
79
|
|
72
80
|
control.run thread_name: 'ctl'
|
73
81
|
@control = control
|
@@ -141,29 +149,29 @@ module Puma
|
|
141
149
|
end
|
142
150
|
|
143
151
|
def load_and_bind
|
144
|
-
unless @
|
152
|
+
unless @config.app_configured?
|
145
153
|
error "No application configured, nothing to run"
|
146
154
|
exit 1
|
147
155
|
end
|
148
156
|
|
149
157
|
begin
|
150
|
-
@app = @
|
158
|
+
@app = @config.app
|
151
159
|
rescue Exception => e
|
152
160
|
log "! Unable to load application: #{e.class}: #{e.message}"
|
153
161
|
raise e
|
154
162
|
end
|
155
163
|
|
156
|
-
@launcher.binder.parse @options[:binds]
|
164
|
+
@launcher.binder.parse @options[:binds]
|
157
165
|
end
|
158
166
|
|
159
167
|
# @!attribute [r] app
|
160
168
|
def app
|
161
|
-
@app ||= @
|
169
|
+
@app ||= @config.app
|
162
170
|
end
|
163
171
|
|
164
172
|
def start_server
|
165
|
-
server = Puma::Server.new
|
166
|
-
server.inherit_binder
|
173
|
+
server = Puma::Server.new(app, @events, @options)
|
174
|
+
server.inherit_binder(@launcher.binder)
|
167
175
|
server
|
168
176
|
end
|
169
177
|
|
@@ -173,5 +181,22 @@ module Puma
|
|
173
181
|
raise "Cannot redirect #{io_name} to #{path}"
|
174
182
|
end
|
175
183
|
end
|
184
|
+
|
185
|
+
def utc_iso8601(val)
|
186
|
+
"#{val.utc.strftime '%FT%T'}Z"
|
187
|
+
end
|
188
|
+
|
189
|
+
def stats
|
190
|
+
{
|
191
|
+
versions: {
|
192
|
+
puma: Puma::Const::PUMA_VERSION,
|
193
|
+
ruby: {
|
194
|
+
engine: RUBY_ENGINE,
|
195
|
+
version: RUBY_VERSION,
|
196
|
+
patchlevel: RUBY_PATCHLEVEL
|
197
|
+
}
|
198
|
+
}
|
199
|
+
}
|
200
|
+
end
|
176
201
|
end
|
177
202
|
end
|
data/lib/puma/server.rb
CHANGED
@@ -2,19 +2,19 @@
|
|
2
2
|
|
3
3
|
require 'stringio'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
5
|
+
require_relative 'thread_pool'
|
6
|
+
require_relative 'const'
|
7
|
+
require_relative 'log_writer'
|
8
|
+
require_relative 'events'
|
9
|
+
require_relative 'null_io'
|
10
|
+
require_relative 'reactor'
|
11
|
+
require_relative 'client'
|
12
|
+
require_relative 'binder'
|
13
|
+
require_relative 'util'
|
14
|
+
require_relative 'request'
|
15
15
|
|
16
16
|
require 'socket'
|
17
|
-
require 'io/wait'
|
17
|
+
require 'io/wait' unless Puma::HAS_NATIVE_IO_WAIT
|
18
18
|
require 'forwardable'
|
19
19
|
|
20
20
|
module Puma
|
@@ -30,12 +30,12 @@ module Puma
|
|
30
30
|
#
|
31
31
|
# Each `Puma::Server` will have one reactor and one thread pool.
|
32
32
|
class Server
|
33
|
-
|
34
33
|
include Puma::Const
|
35
34
|
include Request
|
36
35
|
extend Forwardable
|
37
36
|
|
38
37
|
attr_reader :thread
|
38
|
+
attr_reader :log_writer
|
39
39
|
attr_reader :events
|
40
40
|
attr_reader :min_threads, :max_threads # for #stats
|
41
41
|
attr_reader :requests_count # @version 5.0.0
|
@@ -45,11 +45,6 @@ module Puma
|
|
45
45
|
:leak_stack_on_error,
|
46
46
|
:persistent_timeout, :reaping_time
|
47
47
|
|
48
|
-
# @deprecated v6.0.0
|
49
|
-
attr_writer :auto_trim_time, :early_hints, :first_data_timeout,
|
50
|
-
:leak_stack_on_error, :min_threads, :max_threads,
|
51
|
-
:persistent_timeout, :reaping_time
|
52
|
-
|
53
48
|
attr_accessor :app
|
54
49
|
attr_accessor :binder
|
55
50
|
|
@@ -60,8 +55,9 @@ module Puma
|
|
60
55
|
|
61
56
|
# Create a server for the rack app +app+.
|
62
57
|
#
|
63
|
-
# +
|
64
|
-
#
|
58
|
+
# +log_writer+ is a Puma::LogWriter object used to log info and error messages.
|
59
|
+
#
|
60
|
+
# +events+ is a Puma::Events object used to notify application status events.
|
65
61
|
#
|
66
62
|
# Server#run returns a thread that you can join on to wait for the server
|
67
63
|
# to do its work.
|
@@ -70,34 +66,40 @@ module Puma
|
|
70
66
|
# and have default values set via +fetch+. Normally the values are set via
|
71
67
|
# `::Puma::Configuration.puma_default_options`.
|
72
68
|
#
|
73
|
-
|
69
|
+
# @note The `events` parameter is set to nil, and set to `Events.new` in code.
|
70
|
+
# Often `options` needs to be passed, but `events` does not. Using nil allows
|
71
|
+
# calling code to not require events.rb.
|
72
|
+
#
|
73
|
+
def initialize(app, events = nil, options = {})
|
74
74
|
@app = app
|
75
|
-
@events = events
|
75
|
+
@events = events || Events.new
|
76
76
|
|
77
77
|
@check, @notify = nil
|
78
78
|
@status = :stop
|
79
79
|
|
80
|
-
@auto_trim_time = 30
|
81
|
-
@reaping_time = 1
|
82
|
-
|
83
80
|
@thread = nil
|
84
81
|
@thread_pool = nil
|
85
82
|
|
86
|
-
@options = options
|
83
|
+
@options = if options.is_a?(UserFileDefaultOptions)
|
84
|
+
options
|
85
|
+
else
|
86
|
+
UserFileDefaultOptions.new(options, Configuration::DEFAULTS)
|
87
|
+
end
|
87
88
|
|
88
|
-
@
|
89
|
-
@
|
90
|
-
@
|
91
|
-
@
|
92
|
-
@
|
93
|
-
@
|
94
|
-
@
|
95
|
-
@
|
89
|
+
@log_writer = @options.fetch :log_writer, LogWriter.stdio
|
90
|
+
@early_hints = @options[:early_hints]
|
91
|
+
@first_data_timeout = @options[:first_data_timeout]
|
92
|
+
@min_threads = @options[:min_threads]
|
93
|
+
@max_threads = @options[:max_threads]
|
94
|
+
@persistent_timeout = @options[:persistent_timeout]
|
95
|
+
@queue_requests = @options[:queue_requests]
|
96
|
+
@max_fast_inline = @options[:max_fast_inline]
|
97
|
+
@io_selector_backend = @options[:io_selector_backend]
|
96
98
|
|
97
99
|
temp = !!(@options[:environment] =~ /\A(development|test)\z/)
|
98
100
|
@leak_stack_on_error = @options[:environment] ? temp : true
|
99
101
|
|
100
|
-
@binder = Binder.new(
|
102
|
+
@binder = Binder.new(log_writer)
|
101
103
|
|
102
104
|
ENV['RACK_ENV'] ||= "development"
|
103
105
|
|
@@ -193,12 +195,12 @@ module Puma
|
|
193
195
|
|
194
196
|
# @!attribute [r] backlog
|
195
197
|
def backlog
|
196
|
-
@thread_pool
|
198
|
+
@thread_pool&.backlog
|
197
199
|
end
|
198
200
|
|
199
201
|
# @!attribute [r] running
|
200
202
|
def running
|
201
|
-
@thread_pool
|
203
|
+
@thread_pool&.spawned
|
202
204
|
end
|
203
205
|
|
204
206
|
|
@@ -211,7 +213,7 @@ module Puma
|
|
211
213
|
# value would be 4 until it finishes processing.
|
212
214
|
# @!attribute [r] pool_capacity
|
213
215
|
def pool_capacity
|
214
|
-
@thread_pool
|
216
|
+
@thread_pool&.pool_capacity
|
215
217
|
end
|
216
218
|
|
217
219
|
# Runs the server.
|
@@ -227,29 +229,16 @@ module Puma
|
|
227
229
|
|
228
230
|
@status = :run
|
229
231
|
|
230
|
-
@thread_pool = ThreadPool.new(
|
231
|
-
thread_name,
|
232
|
-
@min_threads,
|
233
|
-
@max_threads,
|
234
|
-
::Puma::IOBuffer,
|
235
|
-
&method(:process_client)
|
236
|
-
)
|
237
|
-
|
238
|
-
@thread_pool.out_of_band_hook = @options[:out_of_band]
|
239
|
-
@thread_pool.clean_thread_locals = @options[:clean_thread_locals]
|
232
|
+
@thread_pool = ThreadPool.new(thread_name, @options) { |client| process_client client }
|
240
233
|
|
241
234
|
if @queue_requests
|
242
|
-
@reactor = Reactor.new(@io_selector_backend
|
235
|
+
@reactor = Reactor.new(@io_selector_backend) { |c| reactor_wakeup c }
|
243
236
|
@reactor.run
|
244
237
|
end
|
245
238
|
|
246
|
-
if @reaping_time
|
247
|
-
@thread_pool.auto_reap!(@reaping_time)
|
248
|
-
end
|
249
239
|
|
250
|
-
if @
|
251
|
-
|
252
|
-
end
|
240
|
+
@thread_pool.auto_reap! if @options[:reaping_time]
|
241
|
+
@thread_pool.auto_trim! if @options[:auto_trim_time]
|
253
242
|
|
254
243
|
@check, @notify = Puma::Util.pipe unless @notify
|
255
244
|
|
@@ -353,11 +342,11 @@ module Puma
|
|
353
342
|
# In the case that any of the sockets are unexpectedly close.
|
354
343
|
raise
|
355
344
|
rescue StandardError => e
|
356
|
-
@
|
345
|
+
@log_writer.unknown_error e, nil, "Listen loop"
|
357
346
|
end
|
358
347
|
end
|
359
348
|
|
360
|
-
@
|
349
|
+
@log_writer.debug "Drained #{drain} additional connections." if drain
|
361
350
|
@events.fire :state, @status
|
362
351
|
|
363
352
|
if queue_requests
|
@@ -366,14 +355,13 @@ module Puma
|
|
366
355
|
end
|
367
356
|
graceful_shutdown if @status == :stop || @status == :restart
|
368
357
|
rescue Exception => e
|
369
|
-
@
|
358
|
+
@log_writer.unknown_error e, nil, "Exception handling servers"
|
370
359
|
ensure
|
371
|
-
# RuntimeError is Ruby 2.2 issue, can't modify frozen IOError
|
372
360
|
# Errno::EBADF is infrequently raised
|
373
361
|
[@check, @notify].each do |io|
|
374
362
|
begin
|
375
363
|
io.close unless io.closed?
|
376
|
-
rescue Errno::EBADF
|
364
|
+
rescue Errno::EBADF
|
377
365
|
end
|
378
366
|
end
|
379
367
|
@notify = nil
|
@@ -412,7 +400,7 @@ module Puma
|
|
412
400
|
# returning.
|
413
401
|
#
|
414
402
|
# Return true if one or more requests were processed.
|
415
|
-
def process_client(client
|
403
|
+
def process_client(client)
|
416
404
|
# Advertise this server into the thread
|
417
405
|
Thread.current[ThreadLocalKey] = self
|
418
406
|
|
@@ -438,15 +426,13 @@ module Puma
|
|
438
426
|
|
439
427
|
while true
|
440
428
|
@requests_count += 1
|
441
|
-
case handle_request(client,
|
429
|
+
case handle_request(client, requests + 1)
|
442
430
|
when false
|
443
431
|
break
|
444
432
|
when :async
|
445
433
|
close_socket = false
|
446
434
|
break
|
447
435
|
when true
|
448
|
-
buffer.reset
|
449
|
-
|
450
436
|
ThreadPool.clean_thread_locals if clean_thread_locals
|
451
437
|
|
452
438
|
requests += 1
|
@@ -480,7 +466,7 @@ module Puma
|
|
480
466
|
# The ensure tries to close +client+ down
|
481
467
|
requests > 0
|
482
468
|
ensure
|
483
|
-
|
469
|
+
client.io_buffer.reset
|
484
470
|
|
485
471
|
begin
|
486
472
|
client.close if close_socket
|
@@ -488,7 +474,7 @@ module Puma
|
|
488
474
|
Puma::Util.purge_interrupt_queue
|
489
475
|
# Already closed
|
490
476
|
rescue StandardError => e
|
491
|
-
@
|
477
|
+
@log_writer.unknown_error e, nil, "Client"
|
492
478
|
end
|
493
479
|
end
|
494
480
|
end
|
@@ -511,16 +497,16 @@ module Puma
|
|
511
497
|
lowlevel_error(e, client.env)
|
512
498
|
case e
|
513
499
|
when MiniSSL::SSLError
|
514
|
-
@
|
500
|
+
@log_writer.ssl_error e, client.io
|
515
501
|
when HttpParserError
|
516
502
|
client.write_error(400)
|
517
|
-
@
|
503
|
+
@log_writer.parse_error e, client
|
518
504
|
when HttpParserError501
|
519
505
|
client.write_error(501)
|
520
|
-
@
|
506
|
+
@log_writer.parse_error e, client
|
521
507
|
else
|
522
508
|
client.write_error(500)
|
523
|
-
@
|
509
|
+
@log_writer.unknown_error e, nil, "Read"
|
524
510
|
end
|
525
511
|
end
|
526
512
|
|
data/lib/puma/single.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
require_relative 'runner'
|
4
|
+
require_relative 'detect'
|
5
|
+
require_relative 'plugin'
|
6
6
|
|
7
7
|
module Puma
|
8
8
|
# This class is instantiated by the `Puma::Launcher` and used
|
@@ -16,26 +16,26 @@ module Puma
|
|
16
16
|
# @!attribute [r] stats
|
17
17
|
def stats
|
18
18
|
{
|
19
|
-
started_at: @started_at
|
20
|
-
}.merge(@server.stats)
|
19
|
+
started_at: utc_iso8601(@started_at)
|
20
|
+
}.merge(@server.stats).merge(super)
|
21
21
|
end
|
22
22
|
|
23
23
|
def restart
|
24
|
-
@server
|
24
|
+
@server&.begin_restart
|
25
25
|
end
|
26
26
|
|
27
27
|
def stop
|
28
|
-
@server
|
28
|
+
@server&.stop false
|
29
29
|
end
|
30
30
|
|
31
31
|
def halt
|
32
|
-
@server
|
32
|
+
@server&.halt
|
33
33
|
end
|
34
34
|
|
35
35
|
def stop_blocked
|
36
36
|
log "- Gracefully stopping, waiting for requests to finish"
|
37
|
-
@control
|
38
|
-
@server
|
37
|
+
@control&.stop true
|
38
|
+
@server&.stop true
|
39
39
|
end
|
40
40
|
|
41
41
|
def run
|
@@ -55,7 +55,7 @@ module Puma
|
|
55
55
|
log "Use Ctrl-C to stop"
|
56
56
|
redirect_io
|
57
57
|
|
58
|
-
@
|
58
|
+
@events.fire_on_booted!
|
59
59
|
|
60
60
|
begin
|
61
61
|
server_thread.join
|
data/lib/puma/state_file.rb
CHANGED
@@ -15,15 +15,12 @@ module Puma
|
|
15
15
|
|
16
16
|
ALLOWED_FIELDS = %w!control_url control_auth_token pid running_from!
|
17
17
|
|
18
|
-
# @deprecated 6.0.0
|
19
|
-
FIELDS = ALLOWED_FIELDS
|
20
|
-
|
21
18
|
def initialize
|
22
19
|
@options = {}
|
23
20
|
end
|
24
21
|
|
25
22
|
def save(path, permission = nil)
|
26
|
-
contents = "---\n"
|
23
|
+
contents = +"---\n"
|
27
24
|
@options.each do |k,v|
|
28
25
|
next unless ALLOWED_FIELDS.include? k
|
29
26
|
case v
|
@@ -50,6 +47,7 @@ module Puma
|
|
50
47
|
v = v.strip
|
51
48
|
@options[k] =
|
52
49
|
case v
|
50
|
+
when '' then nil
|
53
51
|
when /\A\d+\z/ then v.to_i
|
54
52
|
when /\A\d+\.\d+\z/ then v.to_f
|
55
53
|
else v.gsub(/\A"|"\z/, '')
|
data/lib/puma/systemd.rb
CHANGED
@@ -4,7 +4,8 @@ require 'sd_notify'
|
|
4
4
|
|
5
5
|
module Puma
|
6
6
|
class Systemd
|
7
|
-
def initialize(events)
|
7
|
+
def initialize(log_writer, events)
|
8
|
+
@log_writer = log_writer
|
8
9
|
@events = events
|
9
10
|
end
|
10
11
|
|
@@ -40,7 +41,7 @@ module Puma
|
|
40
41
|
end
|
41
42
|
|
42
43
|
def log(str)
|
43
|
-
@
|
44
|
+
@log_writer.log(str)
|
44
45
|
end
|
45
46
|
end
|
46
47
|
end
|
data/lib/puma/thread_pool.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'thread'
|
4
4
|
|
5
|
+
require_relative 'io_buffer'
|
6
|
+
|
5
7
|
module Puma
|
6
8
|
# Internal Docs for A simple thread pool management object.
|
7
9
|
#
|
@@ -29,7 +31,7 @@ module Puma
|
|
29
31
|
# The block passed is the work that will be performed in each
|
30
32
|
# thread.
|
31
33
|
#
|
32
|
-
def initialize(name,
|
34
|
+
def initialize(name, options = {}, &block)
|
33
35
|
@not_empty = ConditionVariable.new
|
34
36
|
@not_full = ConditionVariable.new
|
35
37
|
@mutex = Mutex.new
|
@@ -40,10 +42,13 @@ module Puma
|
|
40
42
|
@waiting = 0
|
41
43
|
|
42
44
|
@name = name
|
43
|
-
@min = Integer(
|
44
|
-
@max = Integer(
|
45
|
+
@min = Integer(options[:min_threads])
|
46
|
+
@max = Integer(options[:max_threads])
|
45
47
|
@block = block
|
46
|
-
@
|
48
|
+
@out_of_band = options[:out_of_band]
|
49
|
+
@clean_thread_locals = options[:clean_thread_locals]
|
50
|
+
@reaping_time = options[:reaping_time]
|
51
|
+
@auto_trim_time = options[:auto_trim_time]
|
47
52
|
|
48
53
|
@shutdown = false
|
49
54
|
|
@@ -62,14 +67,11 @@ module Puma
|
|
62
67
|
end
|
63
68
|
end
|
64
69
|
|
65
|
-
@clean_thread_locals = false
|
66
70
|
@force_shutdown = false
|
67
71
|
@shutdown_mutex = Mutex.new
|
68
72
|
end
|
69
73
|
|
70
74
|
attr_reader :spawned, :trim_requested, :waiting
|
71
|
-
attr_accessor :clean_thread_locals
|
72
|
-
attr_accessor :out_of_band_hook # @version 5.0.0
|
73
75
|
|
74
76
|
def self.clean_thread_locals
|
75
77
|
Thread.current.keys.each do |key| # rubocop: disable Style/HashEachMethods
|
@@ -109,8 +111,6 @@ module Puma
|
|
109
111
|
not_empty = @not_empty
|
110
112
|
not_full = @not_full
|
111
113
|
|
112
|
-
extra = @extra.map { |i| i.new }
|
113
|
-
|
114
114
|
while true
|
115
115
|
work = nil
|
116
116
|
|
@@ -144,7 +144,7 @@ module Puma
|
|
144
144
|
end
|
145
145
|
|
146
146
|
begin
|
147
|
-
@out_of_band_pending = true if block.call(work
|
147
|
+
@out_of_band_pending = true if block.call(work)
|
148
148
|
rescue Exception => e
|
149
149
|
STDERR.puts "Error reached top of thread-pool: #{e.message} (#{e.class})"
|
150
150
|
end
|
@@ -160,12 +160,12 @@ module Puma
|
|
160
160
|
|
161
161
|
# @version 5.0.0
|
162
162
|
def trigger_out_of_band_hook
|
163
|
-
return false unless
|
163
|
+
return false unless @out_of_band&.any?
|
164
164
|
|
165
165
|
# we execute on idle hook when all threads are free
|
166
166
|
return false unless @spawned == @waiting
|
167
167
|
|
168
|
-
|
168
|
+
@out_of_band.each(&:call)
|
169
169
|
true
|
170
170
|
rescue Exception => e
|
171
171
|
STDERR.puts "Exception calling out_of_band_hook: #{e.message} (#{e.class})"
|
@@ -319,12 +319,12 @@ module Puma
|
|
319
319
|
end
|
320
320
|
end
|
321
321
|
|
322
|
-
def auto_trim!(timeout
|
322
|
+
def auto_trim!(timeout=@auto_trim_time)
|
323
323
|
@auto_trim = Automaton.new(self, timeout, "#{@name} threadpool trimmer", :trim)
|
324
324
|
@auto_trim.start!
|
325
325
|
end
|
326
326
|
|
327
|
-
def auto_reap!(timeout
|
327
|
+
def auto_reap!(timeout=@reaping_time)
|
328
328
|
@reaper = Automaton.new(self, timeout, "#{@name} threadpool reaper", :reap)
|
329
329
|
@reaper.start!
|
330
330
|
end
|
@@ -354,8 +354,8 @@ module Puma
|
|
354
354
|
@not_empty.broadcast
|
355
355
|
@not_full.broadcast
|
356
356
|
|
357
|
-
@auto_trim
|
358
|
-
@reaper
|
357
|
+
@auto_trim&.stop
|
358
|
+
@reaper&.stop
|
359
359
|
# dup workers so that we join them all safely
|
360
360
|
@workers.dup
|
361
361
|
end
|
data/lib/puma/util.rb
CHANGED
@@ -17,29 +17,27 @@ module Puma
|
|
17
17
|
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
18
18
|
end
|
19
19
|
|
20
|
-
#
|
21
|
-
#
|
20
|
+
# Escapes and unescapes a URI escaped string with
|
21
|
+
# +encoding+. +encoding+ will be the target encoding of the string
|
22
|
+
# returned, and it defaults to UTF-8
|
22
23
|
if defined?(::Encoding)
|
24
|
+
def escape(s, encoding = Encoding::UTF_8)
|
25
|
+
URI.encode_www_form_component(s, encoding)
|
26
|
+
end
|
27
|
+
|
23
28
|
def unescape(s, encoding = Encoding::UTF_8)
|
24
29
|
URI.decode_www_form_component(s, encoding)
|
25
30
|
end
|
26
31
|
else
|
27
|
-
def
|
28
|
-
URI.
|
32
|
+
def escape(s, encoding = nil)
|
33
|
+
URI.encode_www_form_component(s, encoding)
|
29
34
|
end
|
30
|
-
end
|
31
|
-
module_function :unescape
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
events.log "! Promoting existing objects to old generation..."
|
36
|
-
4.times { GC.start(full_mark: false) }
|
37
|
-
if GC.respond_to?(:compact)
|
38
|
-
events.log "! Compacting..."
|
39
|
-
GC.compact
|
36
|
+
def unescape(s, encoding = nil)
|
37
|
+
URI.decode_www_form_component(s, encoding)
|
40
38
|
end
|
41
|
-
events.log "! Friendly fork preparation complete."
|
42
39
|
end
|
40
|
+
module_function :unescape, :escape
|
43
41
|
|
44
42
|
DEFAULT_SEP = /[&;] */n
|
45
43
|
|