puma 8.1.3 → 8.2.0.pre1

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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +120 -0
  3. data/README.md +4 -3
  4. data/docs/GLOSSARY.md +160 -0
  5. data/docs/architecture.md +41 -42
  6. data/docs/binding.md +3 -0
  7. data/docs/cluster_mode.md +21 -21
  8. data/docs/control_server.md +4 -4
  9. data/docs/deployment/nginx.md +2 -0
  10. data/docs/deployment/systemd.md +15 -0
  11. data/docs/puma_support.md +12 -0
  12. data/docs/restart.md +2 -2
  13. data/docs/signals.md +3 -3
  14. data/docs/ssl.md +65 -0
  15. data/docs/upgrading/5.0-Upgrade.md +1 -1
  16. data/docs/upgrading/6.0-Upgrade.md +1 -1
  17. data/docs/upgrading/7.0-Upgrade.md +1 -1
  18. data/docs/upgrading/8.0-Upgrade.md +1 -1
  19. data/ext/puma_http11/extconf.rb +7 -5
  20. data/ext/puma_http11/http11_parser.c +66 -84
  21. data/ext/puma_http11/http11_parser.h +0 -5
  22. data/ext/puma_http11/mini_ssl.c +31 -68
  23. data/ext/puma_http11/puma_http11.c +125 -7
  24. data/lib/puma/app/status.rb +22 -7
  25. data/lib/puma/binder.rb +0 -1
  26. data/lib/puma/cli.rb +1 -3
  27. data/lib/puma/client.rb +133 -57
  28. data/lib/puma/client_env.rb +9 -11
  29. data/lib/puma/cluster/worker.rb +0 -6
  30. data/lib/puma/cluster.rb +3 -14
  31. data/lib/puma/cluster_accept_loop_delay.rb +18 -7
  32. data/lib/puma/commonlogger.rb +3 -4
  33. data/lib/puma/configuration.rb +4 -1
  34. data/lib/puma/const.rb +5 -5
  35. data/lib/puma/control_cli.rb +5 -7
  36. data/lib/puma/detect.rb +1 -1
  37. data/lib/puma/dsl.rb +70 -14
  38. data/lib/puma/http_parser_error.rb +8 -0
  39. data/lib/puma/launcher/bundle_pruner.rb +3 -21
  40. data/lib/puma/launcher.rb +55 -25
  41. data/lib/puma/minissl/context_builder.rb +3 -2
  42. data/lib/puma/minissl.rb +12 -16
  43. data/lib/puma/plugin/systemd.rb +69 -3
  44. data/lib/puma/rack/urlmap.rb +2 -2
  45. data/lib/puma/reactor.rb +138 -25
  46. data/lib/puma/response.rb +65 -38
  47. data/lib/puma/runner.rb +4 -1
  48. data/lib/puma/sd_notify.rb +21 -0
  49. data/lib/puma/server.rb +14 -14
  50. data/lib/puma/server_plugin_control.rb +2 -1
  51. data/lib/puma/single.rb +0 -1
  52. data/lib/puma/thread_pool.rb +87 -14
  53. data/lib/puma/util.rb +5 -16
  54. data/lib/puma.rb +22 -5
  55. data/lib/rack/handler/puma.rb +1 -5
  56. metadata +5 -2
data/lib/puma/dsl.rb CHANGED
@@ -134,7 +134,6 @@ module Puma
134
134
 
135
135
  def _load_from(path)
136
136
  if path
137
- @path = path
138
137
  instance_eval(File.read(path), path, 1)
139
138
  end
140
139
  ensure
@@ -144,7 +143,6 @@ module Puma
144
143
  def _offer_plugins
145
144
  @plugins.each do |o|
146
145
  if o.respond_to? :config
147
- @options.shift
148
146
  o.config self
149
147
  end
150
148
  end
@@ -219,7 +217,13 @@ module Puma
219
217
  # @example
220
218
  # activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true }
221
219
  # @example
222
- # activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true, data_only: true}
220
+ # activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true, data_only: true }
221
+ # @example
222
+ # activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345', data_token: '67890' }
223
+ # @example
224
+ # activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345', data_token: 'none' }
225
+ # @example
226
+ # activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345', data_token: :none }
223
227
  #
224
228
  def activate_control_app(url="auto", opts={})
225
229
  if url == "auto"
@@ -243,6 +247,7 @@ module Puma
243
247
  end
244
248
 
245
249
  @options[:control_auth_token] = auth_token
250
+ @options[:control_data_token] = opts[:data_token] if opts[:data_token]
246
251
  @options[:control_url_umask] = opts[:umask] if opts[:umask]
247
252
  @options[:control_data_only] = opts[:data_only] if opts[:data_only]
248
253
  end
@@ -336,6 +341,12 @@ module Puma
336
341
 
337
342
  # Define how long the TCP socket stays open when no data has been received.
338
343
  #
344
+ # This is also the maximum time a client may take to send its request line
345
+ # and headers (and PROXY protocol line, if enabled), even if it keeps
346
+ # sending data. The deadline starts when Puma begins reading each request.
347
+ # While reading the request body, this timeout restarts whenever data is
348
+ # received.
349
+ #
339
350
  # The default is 30 seconds.
340
351
  #
341
352
  # @example
@@ -1225,7 +1236,6 @@ module Puma
1225
1236
  #
1226
1237
  # @note Cluster mode only.
1227
1238
  # @note This is incompatible with +preload_app!+.
1228
- # @note This is only supported for RubyGems 2.2+.
1229
1239
  #
1230
1240
  # @example
1231
1241
  # prune_bundler
@@ -1266,7 +1276,7 @@ module Puma
1266
1276
  # @example
1267
1277
  # extra_runtime_dependencies ['puma_worker_killer', 'puma-heroku']
1268
1278
  #
1269
- # @see Puma::Launcher#extra_runtime_deps_directories
1279
+ # @see Puma::Launcher::BundlePruner#extra_runtime_deps_paths
1270
1280
  #
1271
1281
  def extra_runtime_dependencies(answer = [])
1272
1282
  @options[:extra_runtime_dependencies] = Array(answer)
@@ -1317,7 +1327,7 @@ module Puma
1317
1327
  # @example
1318
1328
  # worker_timeout 60
1319
1329
  #
1320
- # @see Puma::Cluster::Worker#ping_timeout
1330
+ # @see Puma::Cluster::WorkerHandle#ping_timeout
1321
1331
  #
1322
1332
  def worker_timeout(timeout)
1323
1333
  timeout = Integer(timeout)
@@ -1339,7 +1349,7 @@ module Puma
1339
1349
  # @example
1340
1350
  # worker_boot_timeout 60
1341
1351
  #
1342
- # @see Puma::Cluster::Worker#ping_timeout
1352
+ # @see Puma::Cluster::WorkerHandle#ping_timeout
1343
1353
  #
1344
1354
  def worker_boot_timeout(timeout)
1345
1355
  @options[:worker_boot_timeout] = Integer(timeout)
@@ -1354,7 +1364,7 @@ module Puma
1354
1364
  # @example
1355
1365
  # worker_shutdown_timeout 90
1356
1366
  #
1357
- # @see Puma::Cluster::Worker#term
1367
+ # @see Puma::Cluster::WorkerHandle#term
1358
1368
  #
1359
1369
  def worker_shutdown_timeout(timeout)
1360
1370
  @options[:worker_shutdown_timeout] = Integer(timeout)
@@ -1388,15 +1398,15 @@ module Puma
1388
1398
  @options[:worker_culling_strategy] = strategy
1389
1399
  end
1390
1400
 
1391
- # When `true`, a worker thread that receives a client with an
1392
- # incomplete request body hands the client to a Reactor thread
1401
+ # When `true`, a thread that receives a client with an
1402
+ # incomplete request body hands the client to the Reactor thread
1393
1403
  # and returns to the pool. The Reactor re-enqueues the client
1394
- # once the body has been received. This frees workers from
1404
+ # once the body has been received. This frees threads from
1395
1405
  # blocking on slow client sends and enables HTTP keep-alive. A
1396
- # worker must still receive each client first, so this does
1406
+ # thread must still receive each client first, so this does
1397
1407
  # not queue new connections when the pool is saturated.
1398
1408
  #
1399
- # When `false`, no Reactor runs. A worker thread reads each
1409
+ # When `false`, no Reactor runs. A thread reads each
1400
1410
  # request synchronously and remains occupied for the duration
1401
1411
  # of the client's request send, and HTTP keep-alive is
1402
1412
  # disabled. Consider running Puma behind a reverse proxy such
@@ -1448,7 +1458,7 @@ module Puma
1448
1458
  # @note Interpreters with forking support only.
1449
1459
  #
1450
1460
  # @see Puma::Server#handle_servers
1451
- # @see Puma::ThreadPool#wait_for_less_busy_worker
1461
+ # @see Puma::ClusterAcceptLoopDelay
1452
1462
  #
1453
1463
  def wait_for_less_busy_worker(val=0.005)
1454
1464
  @options[:wait_for_less_busy_worker] = val.to_f
@@ -1625,6 +1635,18 @@ module Puma
1625
1635
  @options[:allow_underscore_headers] = allowed
1626
1636
  end
1627
1637
 
1638
+ # Add a +Date+ response header (RFC 9110 §6.6.1) when the application
1639
+ # does not set one.
1640
+ #
1641
+ # The default is +true+.
1642
+ #
1643
+ # @example
1644
+ # date_header false
1645
+ #
1646
+ def date_header(enabled = true)
1647
+ @options[:date_header] = enabled
1648
+ end
1649
+
1628
1650
  # Specify the backend for the IO selector.
1629
1651
  #
1630
1652
  # Provided values will be passed directly to +NIO::Selector.new+, with the
@@ -1676,6 +1698,40 @@ module Puma
1676
1698
  @options[:http_content_length_limit] = limit
1677
1699
  end
1678
1700
 
1701
+ # Leave +rack.input+ unset for requests with no body, as Rack 3.1 allows,
1702
+ # instead of setting it to an empty stream. Apps then need to check for
1703
+ # +nil+ before reading it. Has no effect with Rack before 3.1, which
1704
+ # requires +rack.input+.
1705
+ #
1706
+ # The default is +false+.
1707
+ #
1708
+ # @example
1709
+ # nil_rack_input_on_empty_body
1710
+ #
1711
+ def nil_rack_input_on_empty_body(enabled = true)
1712
+ @options[:nil_rack_input_on_empty_body] = enabled
1713
+ end
1714
+
1715
+ # The class or callable factory used to parse HTTP requests, instead of the
1716
+ # default `Puma::HttpParser` from Puma's `puma_http11` extension.
1717
+ #
1718
+ # Pass a class (instantiated with +new+ per connection), a callable object
1719
+ # responding to +call+, or a block that returns a new parser instance.
1720
+ # The parser instance must follow the same interface as `Puma::HttpParser`.
1721
+ #
1722
+ # The default is +nil+, which uses `Puma::HttpParser`.
1723
+ #
1724
+ # @example With a parser class
1725
+ # require "puma/http1"
1726
+ # http_parser Puma::HTTP1::Parser
1727
+ #
1728
+ # @example With a block factory
1729
+ # http_parser { MyParser.new(max_header_size: 8192) }
1730
+ #
1731
+ def http_parser(parser = nil, &block)
1732
+ @options[:http_parser] = parser || block
1733
+ end
1734
+
1679
1735
  # Supported http methods, which will replace `Puma::Const::SUPPORTED_HTTP_METHODS`.
1680
1736
  # The value of `:any` will allow all methods, otherwise, the value must be
1681
1737
  # an array of strings. Note that methods are all uppercase.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Puma
4
+ # Raised for a request that is not valid HTTP. It is defined here, as well
5
+ # as in the puma_http11 extension, so that a parser set with the
6
+ # `http_parser` option can raise it when the extension is not loaded.
7
+ class HttpParserError < StandardError; end
8
+ end
@@ -17,8 +17,6 @@ module Puma
17
17
  return if ENV['PUMA_BUNDLER_PRUNED']
18
18
  return unless defined?(Bundler)
19
19
 
20
- require_rubygems_min_version!
21
-
22
20
  unless puma_wild_path
23
21
  log "! Unable to prune Bundler environment, continuing"
24
22
  return
@@ -30,7 +28,7 @@ module Puma
30
28
  home = ENV['GEM_HOME']
31
29
  original_bundle_env = Bundler.original_env.select { |k, v| k.start_with?('BUNDLE_') && v }
32
30
 
33
- with_unbundled_env do
31
+ Bundler.with_unbundled_env do
34
32
  ENV['GEM_HOME'] = home
35
33
  ENV['PUMA_BUNDLER_PRUNED'] = '1'
36
34
  original_bundle_env.each { |k, v| ENV[k] = v }
@@ -43,27 +41,11 @@ module Puma
43
41
 
44
42
  private
45
43
 
46
- def require_rubygems_min_version!
47
- min_version = Gem::Version.new('2.2')
48
-
49
- return if min_version <= Gem::Version.new(Gem::VERSION)
50
-
51
- raise "prune_bundler is not supported on your version of RubyGems. " \
52
- "You must have RubyGems #{min_version}+ to use this feature."
53
- end
54
-
55
44
  def puma_wild_path
56
45
  puma_lib_dir = puma_require_paths.detect { |x| File.exist? File.join(x, '../bin/puma-wild') }
57
- File.expand_path(File.join(puma_lib_dir, '../bin/puma-wild'))
58
- end
46
+ return unless puma_lib_dir
59
47
 
60
- def with_unbundled_env
61
- bundler_ver = Gem::Version.new(Bundler::VERSION)
62
- if bundler_ver < Gem::Version.new('2.1.0')
63
- Bundler.with_clean_env { yield }
64
- else
65
- Bundler.with_unbundled_env { yield }
66
- end
48
+ File.expand_path(File.join(puma_lib_dir, '../bin/puma-wild'))
67
49
  end
68
50
 
69
51
  def paths_to_require_after_prune
data/lib/puma/launcher.rb CHANGED
@@ -18,6 +18,9 @@ module Puma
18
18
  class Launcher
19
19
  autoload :BundlePruner, 'puma/launcher/bundle_pruner'
20
20
 
21
+ class JRubySigterm < SignalException; end
22
+ private_constant :JRubySigterm
23
+
21
24
  # Returns an instance of Launcher
22
25
  #
23
26
  # +conf+ A Puma::Configuration object indicating how to run the server.
@@ -60,8 +63,7 @@ module Puma
60
63
 
61
64
  @events = launcher_args[:events] || Events.new
62
65
 
63
- @argv = launcher_args[:argv] || []
64
- @original_argv = @argv.dup
66
+ @original_argv = (launcher_args[:argv] || []).dup
65
67
 
66
68
  ## End minimal initialization
67
69
 
@@ -100,7 +102,6 @@ module Puma
100
102
  unsupported "worker mode not supported on #{RUBY_ENGINE} on this platform"
101
103
  end
102
104
 
103
- @environment = @options[:environment] if @options[:environment]
104
105
  set_rack_environment
105
106
 
106
107
  if clustered?
@@ -199,17 +200,18 @@ module Puma
199
200
 
200
201
  # Run the server. This blocks until the server is stopped
201
202
  def run
202
- previous_env = get_env
203
-
204
- @config.plugins.fire_starts self
205
-
206
- setup_signals
207
- set_process_title
208
-
209
- # This blocks until the server is stopped
210
- @runner.run
203
+ return run_server unless Puma.jruby? && Thread.current == Thread.main
211
204
 
212
- do_run_finished(previous_env)
205
+ Thread.handle_interrupt(JRubySigterm => :never) do
206
+ begin
207
+ Thread.handle_interrupt(JRubySigterm => :immediate) { run_server(jruby_sigterm: true) }
208
+ rescue JRubySigterm
209
+ do_graceful_stop
210
+ raise
211
+ end
212
+ end
213
+ rescue JRubySigterm
214
+ raise SignalException, "SIGTERM"
213
215
  end
214
216
 
215
217
  # Return all tcp ports the launcher may be using, TCP or SSL
@@ -244,7 +246,7 @@ module Puma
244
246
  Thread.list.each do |thread|
245
247
  name = "Thread: TID-#{thread.object_id.to_s(36)}"
246
248
  name += " #{thread['label']}" if thread['label']
247
- name += " #{thread.name}" if thread.respond_to?(:name) && thread.name
249
+ name += " #{thread.name}" if thread.name
248
250
  backtrace = thread.backtrace || ["<no backtrace available>"]
249
251
 
250
252
  yield name, backtrace
@@ -253,6 +255,20 @@ module Puma
253
255
 
254
256
  private
255
257
 
258
+ def run_server(jruby_sigterm: false)
259
+ previous_env = get_env
260
+
261
+ @config.plugins.fire_starts self
262
+
263
+ setup_signals(jruby_sigterm)
264
+ set_process_title
265
+
266
+ # This blocks until the server is stopped
267
+ @runner.run
268
+
269
+ do_run_finished(previous_env)
270
+ end
271
+
256
272
  def get_env
257
273
  if defined?(Bundler)
258
274
  env = Bundler::ORIGINAL_ENV.dup
@@ -351,7 +367,7 @@ module Puma
351
367
  end
352
368
 
353
369
  def set_process_title
354
- Process.respond_to?(:setproctitle) ? Process.setproctitle(title) : $0 = title
370
+ Process.setproctitle(title)
355
371
  end
356
372
 
357
373
  # @!attribute [r] title
@@ -425,13 +441,13 @@ module Puma
425
441
  end
426
442
  end
427
443
 
428
- def setup_signals
444
+ def setup_signals(jruby_sigterm)
429
445
  unless ENV["PUMA_SKIP_SIGUSR2"]
430
446
  begin
431
447
  Signal.trap "SIGUSR2" do
432
448
  restart
433
449
  end
434
- rescue Exception
450
+ rescue StandardError, NotImplementedError
435
451
  log "*** SIGUSR2 not implemented, signal based restart unavailable!"
436
452
  end
437
453
  end
@@ -441,19 +457,33 @@ module Puma
441
457
  Signal.trap "SIGUSR1" do
442
458
  phased_restart
443
459
  end
444
- rescue Exception
460
+ rescue StandardError, NotImplementedError
445
461
  log "*** SIGUSR1 not implemented, signal based restart unavailable!"
446
462
  end
447
463
  end
448
464
 
449
465
  begin
450
466
  Signal.trap "SIGTERM" do
451
- # Shortcut the control flow in case raise_exception_on_sigterm is true
452
- do_graceful_stop
467
+ if jruby_sigterm && @options[:raise_exception_on_sigterm]
468
+ Thread.main.raise(JRubySigterm, "SIGTERM")
469
+ else
470
+ # Shortcut the control flow in case raise_exception_on_sigterm is true
471
+ do_graceful_stop
453
472
 
454
- raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
473
+ raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
474
+ end
475
+ end
476
+
477
+ if @options[:raise_exception_on_sigterm] && Process.pid == 1
478
+ # PID 1 cannot die from its own SIGTERM (the kernel drops default-action
479
+ # signals sent to PID 1) and Ruby falls back to exit status 1.
480
+ # Convert an unhandled SIGTERM to exit 143 (128 + SIGTERM) after app
481
+ # at_exit hooks run so rescue blocks and app hooks still see SignalException.
482
+ at_exit do
483
+ exit 143 if Process.pid == 1 && $!.is_a?(SignalException) && $!.signm == "SIGTERM"
484
+ end
455
485
  end
456
- rescue Exception
486
+ rescue StandardError, NotImplementedError
457
487
  log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!"
458
488
  end
459
489
 
@@ -461,7 +491,7 @@ module Puma
461
491
  Signal.trap "SIGINT" do
462
492
  stop
463
493
  end
464
- rescue Exception
494
+ rescue StandardError, NotImplementedError
465
495
  log "*** SIGINT not implemented, signal based gracefully stopping unavailable!"
466
496
  end
467
497
 
@@ -473,7 +503,7 @@ module Puma
473
503
  stop
474
504
  end
475
505
  end
476
- rescue Exception
506
+ rescue StandardError, NotImplementedError
477
507
  log "*** SIGHUP not implemented, signal based logs reopening unavailable!"
478
508
  end
479
509
 
@@ -486,7 +516,7 @@ module Puma
486
516
  end
487
517
  end
488
518
  end
489
- rescue Exception
519
+ rescue StandardError, NotImplementedError
490
520
  log "*** SIGINFO/SIGPWR not implemented, signal based backtrace unavailable!"
491
521
  end
492
522
  end
@@ -51,8 +51,6 @@ module Puma
51
51
  unless params['ca']
52
52
  log_writer.error "Please specify the SSL ca via 'ca='"
53
53
  end
54
- # needed for Puma::MiniSSL::Socket#peercert, env['puma.peercert']
55
- require 'openssl'
56
54
  end
57
55
 
58
56
  ctx.ca = params['ca'] if params['ca']
@@ -77,6 +75,9 @@ module Puma
77
75
  log_writer.error "Please specify a valid verify_mode="
78
76
  MiniSSL::VERIFY_NONE
79
77
  end
78
+
79
+ # needed for Puma::MiniSSL::Socket#peercert, env['puma.peercert']
80
+ require 'openssl' if ctx.verify_mode != MiniSSL::VERIFY_NONE
80
81
  end
81
82
 
82
83
  if params['verification_flags']
data/lib/puma/minissl.rb CHANGED
@@ -1,9 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- begin
4
- require 'io/wait' unless Puma::HAS_NATIVE_IO_WAIT
5
- rescue LoadError
6
- end
3
+ require 'io/wait' unless Puma::HAS_NATIVE_IO_WAIT
7
4
 
8
5
  require 'open3'
9
6
  # need for Puma::MiniSSL::OPENSSL constants used in `HAS_TLS1_3`
@@ -24,7 +21,6 @@ module Puma
24
21
  @socket = socket
25
22
  @engine = engine
26
23
  @peercert = nil
27
- @reuse = nil
28
24
  @write_buffer = +''
29
25
  @write_owed = 0
30
26
  @write_owed_data = nil
@@ -84,13 +80,15 @@ module Puma
84
80
  output
85
81
  end
86
82
 
87
- def read_nonblock(size, *_)
88
- # *_ is to deal with keyword args that were added
89
- # at some point (and being used in the wild)
83
+ def read_nonblock(size, *_, exception: true)
84
+ # *_ absorbs the optional positional outbuf argument passed by IO callers
90
85
  output = engine_read_all
91
86
  return output if output
92
87
 
93
- raise IO::EAGAINWaitReadable unless flush_write_buffer_nonblock
88
+ unless flush_write_buffer_nonblock
89
+ raise IO::EAGAINWaitReadable if exception
90
+ return :wait_readable
91
+ end
94
92
 
95
93
  data = @socket.read_nonblock(size, exception: false)
96
94
  if data == :wait_readable || data == :wait_writable
@@ -103,7 +101,8 @@ module Puma
103
101
  # In the meantime, we can emulate the correct behavior by
104
102
  # capturing :wait_readable & :wait_writable and raising EAGAIN
105
103
  # ourselves.
106
- raise IO::EAGAINWaitReadable
104
+ raise IO::EAGAINWaitReadable if exception
105
+ return :wait_readable
107
106
  elsif data.nil?
108
107
  raise SSLError.exception "HTTP connection?" if bad_tlsv1_3?
109
108
  return nil
@@ -115,7 +114,8 @@ module Puma
115
114
 
116
115
  queue_ciphertext
117
116
  flush_write_buffer_nonblock
118
- raise IO::EAGAINWaitReadable
117
+ raise IO::EAGAINWaitReadable if exception
118
+ :wait_readable
119
119
  end
120
120
 
121
121
  def write_pending?
@@ -237,15 +237,11 @@ module Puma
237
237
  raw = @engine.peercert
238
238
  return nil unless raw
239
239
 
240
+ require 'openssl' unless defined?(OpenSSL::X509::Certificate)
240
241
  @peercert = OpenSSL::X509::Certificate.new raw
241
242
  end
242
243
  end
243
244
 
244
- if IS_JRUBY
245
- OPENSSL_NO_SSL3 = false
246
- OPENSSL_NO_TLS1 = false
247
- end
248
-
249
245
  class Context
250
246
  attr_accessor :verify_mode
251
247
  attr_reader :no_tlsv1, :no_tlsv1_1
@@ -13,9 +13,54 @@ Puma::Plugin.create do
13
13
 
14
14
  launcher.log_writer.log "* Enabling systemd notification integration"
15
15
 
16
+ extend_timeout_usec = Puma::SdNotify.extend_timeout_usec
17
+ if extend_timeout_usec.positive?
18
+ extend_timeout_max_usec = Puma::SdNotify.extend_timeout_max_usec
19
+ extend_sleep_time = extend_timeout_usec / usec_per_sec / 2
20
+ extend_deadline = monotonic_time + extend_timeout_max_usec / usec_per_sec if extend_timeout_max_usec.positive?
21
+
22
+ pipe_r, @extend_stop = IO.pipe
23
+
24
+ launcher.log_writer.log "Extending systemd startup timeout every #{extend_sleep_time.round(1)} sec"
25
+
26
+ @extend_thread = Thread.new do
27
+ Puma.set_thread_name "systemd extend"
28
+ begin
29
+ loop do
30
+ break if IO.select([pipe_r], nil, nil, 0)
31
+
32
+ usec = extend_timeout_usec
33
+ if extend_deadline
34
+ remaining_usec = ((extend_deadline - monotonic_time) * usec_per_sec).ceil
35
+ break unless remaining_usec.positive?
36
+
37
+ usec = [usec, remaining_usec].min
38
+ end
39
+
40
+ begin
41
+ Puma::SdNotify.extend_timeout(usec)
42
+ rescue Puma::SdNotify::NotifyError
43
+ break
44
+ end
45
+ break if usec < extend_timeout_usec
46
+
47
+ break if IO.select([pipe_r], nil, nil, extend_sleep_time)
48
+ end
49
+ ensure
50
+ pipe_r.close
51
+ end
52
+ end
53
+ end
54
+
16
55
  # hook_events
17
- launcher.events.after_booted { Puma::SdNotify.ready }
18
- launcher.events.after_stopped { Puma::SdNotify.stopping }
56
+ launcher.events.after_booted do
57
+ stop_extending
58
+ Puma::SdNotify.ready
59
+ end
60
+ launcher.events.after_stopped do
61
+ stop_extending
62
+ Puma::SdNotify.stopping
63
+ end
19
64
  launcher.events.before_restart { Puma::SdNotify.reloading }
20
65
 
21
66
  # start watchdog
@@ -59,10 +104,31 @@ Puma::Plugin.create do
59
104
 
60
105
  private
61
106
 
107
+ def usec_per_sec
108
+ 1_000_000.0
109
+ end
110
+
111
+ def stop_extending
112
+ stop = @extend_stop
113
+ thread = @extend_thread
114
+ @extend_stop = nil
115
+ @extend_thread = nil
116
+
117
+ begin
118
+ stop&.close
119
+ rescue IOError
120
+ end
121
+ thread&.join
122
+ end
123
+
124
+ def monotonic_time
125
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
126
+ end
127
+
62
128
  def watchdog_sleep_time
63
129
  usec = Integer(ENV["WATCHDOG_USEC"])
64
130
 
65
- sec_f = usec / 1_000_000.0
131
+ sec_f = usec / usec_per_sec
66
132
  # "It is recommended that a daemon sends a keep-alive notification message
67
133
  # to the service manager every half of the time returned here."
68
134
  sec_f / 2
@@ -14,8 +14,8 @@ module Puma::Rack
14
14
  # first, since they are most specific.
15
15
 
16
16
  class URLMap
17
- NEGATIVE_INFINITY = -1.0 / 0.0
18
- INFINITY = 1.0 / 0.0
17
+ NEGATIVE_INFINITY = -Float::INFINITY
18
+ INFINITY = Float::INFINITY
19
19
 
20
20
  def initialize(map = {})
21
21
  remap(map)