puma 4.3.12 → 5.0.0

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 (82) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +79 -32
  3. data/LICENSE +23 -20
  4. data/README.md +26 -13
  5. data/bin/puma-wild +0 -0
  6. data/docs/architecture.md +3 -3
  7. data/docs/deployment.md +9 -3
  8. data/docs/fork_worker.md +31 -0
  9. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  10. data/docs/images/puma-connection-flow.png +0 -0
  11. data/docs/images/puma-general-arch.png +0 -0
  12. data/docs/jungle/README.md +13 -0
  13. data/{tools → docs}/jungle/rc.d/README.md +0 -0
  14. data/{tools → docs}/jungle/rc.d/puma.conf +0 -0
  15. data/{tools → docs}/jungle/upstart/README.md +0 -0
  16. data/{tools → docs}/jungle/upstart/puma-manager.conf +0 -0
  17. data/{tools → docs}/jungle/upstart/puma.conf +0 -0
  18. data/docs/nginx.md +0 -0
  19. data/docs/plugins.md +0 -0
  20. data/docs/restart.md +0 -0
  21. data/docs/signals.md +7 -6
  22. data/docs/systemd.md +1 -63
  23. data/ext/puma_http11/PumaHttp11Service.java +2 -4
  24. data/ext/puma_http11/ext_help.h +0 -0
  25. data/ext/puma_http11/extconf.rb +3 -10
  26. data/ext/puma_http11/http11_parser.c +10 -23
  27. data/ext/puma_http11/http11_parser.h +0 -0
  28. data/ext/puma_http11/http11_parser.java.rl +0 -0
  29. data/ext/puma_http11/http11_parser.rl +0 -0
  30. data/ext/puma_http11/http11_parser_common.rl +1 -1
  31. data/ext/puma_http11/mini_ssl.c +62 -84
  32. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  33. data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
  34. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +46 -48
  35. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +77 -18
  36. data/ext/puma_http11/puma_http11.c +6 -38
  37. data/lib/puma/accept_nonblock.rb +0 -0
  38. data/lib/puma/app/status.rb +14 -1
  39. data/lib/puma/binder.rb +88 -68
  40. data/lib/puma/cli.rb +7 -15
  41. data/lib/puma/client.rb +49 -59
  42. data/lib/puma/cluster.rb +191 -74
  43. data/lib/puma/commonlogger.rb +2 -2
  44. data/lib/puma/configuration.rb +31 -42
  45. data/lib/puma/const.rb +6 -8
  46. data/lib/puma/control_cli.rb +29 -17
  47. data/lib/puma/detect.rb +17 -0
  48. data/lib/puma/dsl.rb +144 -70
  49. data/lib/puma/error_logger.rb +97 -0
  50. data/lib/puma/events.rb +35 -31
  51. data/lib/puma/io_buffer.rb +9 -2
  52. data/lib/puma/jruby_restart.rb +0 -58
  53. data/lib/puma/launcher.rb +49 -31
  54. data/lib/puma/minissl/context_builder.rb +0 -3
  55. data/lib/puma/minissl.rb +60 -18
  56. data/lib/puma/null_io.rb +1 -1
  57. data/lib/puma/plugin/tmp_restart.rb +0 -0
  58. data/lib/puma/plugin.rb +1 -10
  59. data/lib/puma/rack/builder.rb +0 -4
  60. data/lib/puma/rack/urlmap.rb +0 -0
  61. data/lib/puma/rack_default.rb +0 -0
  62. data/lib/puma/reactor.rb +9 -4
  63. data/lib/puma/runner.rb +8 -36
  64. data/lib/puma/server.rb +122 -215
  65. data/lib/puma/single.rb +7 -64
  66. data/lib/puma/state_file.rb +6 -3
  67. data/lib/puma/thread_pool.rb +94 -49
  68. data/lib/puma/util.rb +0 -0
  69. data/lib/puma.rb +17 -0
  70. data/lib/rack/handler/puma.rb +1 -3
  71. data/tools/{docker/Dockerfile → Dockerfile} +0 -0
  72. data/tools/trickletest.rb +0 -0
  73. metadata +18 -20
  74. data/docs/tcp_mode.md +0 -96
  75. data/ext/puma_http11/io_buffer.c +0 -155
  76. data/ext/puma_http11/org/jruby/puma/IOBuffer.java +0 -72
  77. data/lib/puma/tcp_logger.rb +0 -41
  78. data/tools/jungle/README.md +0 -19
  79. data/tools/jungle/init.d/README.md +0 -61
  80. data/tools/jungle/init.d/puma +0 -421
  81. data/tools/jungle/init.d/run-puma +0 -18
  82. /data/{tools → docs}/jungle/rc.d/puma +0 -0
data/lib/puma/dsl.rb CHANGED
@@ -14,22 +14,23 @@ module Puma
14
14
  # end
15
15
  # config.load
16
16
  #
17
- # puts config.options[:binds]
18
- # "tcp://127.0.0.1:3001"
17
+ # puts config.options[:binds] # => "tcp://127.0.0.1:3001"
19
18
  #
20
19
  # Used to load file:
21
20
  #
22
21
  # $ cat puma_config.rb
23
- # port 3002
22
+ # port 3002
23
+ #
24
+ # Resulting configuration:
24
25
  #
25
26
  # config = Configuration.new(config_file: "puma_config.rb")
26
27
  # config.load
27
28
  #
28
- # puts config.options[:binds]
29
- # # => "tcp://127.0.0.1:3002"
29
+ # puts config.options[:binds] # => "tcp://127.0.0.1:3002"
30
30
  #
31
31
  # You can also find many examples being used by the test suite in
32
32
  # +test/config+.
33
+ #
33
34
  class DSL
34
35
  include ConfigDefault
35
36
 
@@ -98,6 +99,9 @@ module Puma
98
99
  # [body]
99
100
  # ]
100
101
  # end
102
+ #
103
+ # @see Puma::Configuration#app
104
+ #
101
105
  def app(obj=nil, &block)
102
106
  obj ||= block
103
107
 
@@ -160,12 +164,12 @@ module Puma
160
164
  #
161
165
  # You can use query parameters within the url to specify options:
162
166
  #
163
- # - Set the socket backlog depth with +backlog+, default is 1024.
164
- # - Set up an SSL certificate with +key+ & +cert+.
165
- # - Set whether to optimize for low latency instead of throughput with
166
- # +low_latency+, default is to optimize for low latency. This is done
167
- # via +Socket::TCP_NODELAY+.
168
- # - Set socket permissions with +umask+.
167
+ # * Set the socket backlog depth with +backlog+, default is 1024.
168
+ # * Set up an SSL certificate with +key+ & +cert+.
169
+ # * Set whether to optimize for low latency instead of throughput with
170
+ # +low_latency+, default is to optimize for low latency. This is done
171
+ # via +Socket::TCP_NODELAY+.
172
+ # * Set socket permissions with +umask+.
169
173
  #
170
174
  # @example Backlog depth
171
175
  # bind 'unix:///var/run/puma.sock?backlog=512'
@@ -175,6 +179,9 @@ module Puma
175
179
  # bind 'tcp://0.0.0.0:9292?low_latency=false'
176
180
  # @example Socket permissions
177
181
  # bind 'unix:///var/run/puma.sock?umask=0111'
182
+ # @see Puma::Runner#load_and_bind
183
+ # @see Puma::Cluster#run
184
+ #
178
185
  def bind(url)
179
186
  @options[:binds] ||= []
180
187
  @options[:binds] << url
@@ -193,13 +200,14 @@ module Puma
193
200
  bind "tcp://#{host}:#{port}"
194
201
  end
195
202
 
196
- # Define how long persistent connections can be idle before Puma closes
197
- # them.
203
+ # Define how long persistent connections can be idle before Puma closes them.
204
+ # @see Puma::Server.new
198
205
  def persistent_timeout(seconds)
199
206
  @options[:persistent_timeout] = Integer(seconds)
200
207
  end
201
208
 
202
209
  # Define how long the tcp socket stays open, if no data has been received.
210
+ # @see Puma::Server.new
203
211
  def first_data_timeout(seconds)
204
212
  @options[:first_data_timeout] = Integer(seconds)
205
213
  end
@@ -210,24 +218,11 @@ module Puma
210
218
  @options[:clean_thread_locals] = which
211
219
  end
212
220
 
213
- # Daemonize the server into the background. It's highly recommended to
214
- # use this in combination with +pidfile+ and +stdout_redirect+.
215
- #
216
- # The default is "false".
217
- #
218
- # @example
219
- # daemonize
221
+ # When shutting down, drain the accept socket of pending connections and
222
+ # process them. This loops over the accept socket until there are no more
223
+ # read events and then stops looking and waits for the requests to finish.
224
+ # @see Puma::Server#graceful_shutdown
220
225
  #
221
- # @example
222
- # daemonize false
223
- def daemonize(which=true)
224
- @options[:daemon] = which
225
- end
226
-
227
- # When shutting down, drain the accept socket of pending
228
- # connections and process them. This loops over the accept
229
- # socket until there are no more read events and then stops
230
- # looking and waits for the requests to finish.
231
226
  def drain_on_shutdown(which=true)
232
227
  @options[:drain_on_shutdown] = which
233
228
  end
@@ -250,6 +245,7 @@ module Puma
250
245
  #
251
246
  # Puma always waits a few seconds after killing a thread for it to try
252
247
  # to finish up it's work, even in :immediately mode.
248
+ # @see Puma::Server#graceful_shutdown
253
249
  def force_shutdown_after(val=:forever)
254
250
  i = case val
255
251
  when :forever
@@ -257,7 +253,7 @@ module Puma
257
253
  when :immediately
258
254
  0
259
255
  else
260
- Integer(val)
256
+ Float(val)
261
257
  end
262
258
 
263
259
  @options[:force_shutdown_after] = i
@@ -322,20 +318,14 @@ module Puma
322
318
  # @example
323
319
  # rackup '/u/apps/lolcat/config.ru'
324
320
  def rackup(path)
325
- @options[:rackup] = path.to_s
326
- end
327
-
328
- # Run Puma in TCP mode
329
- #
330
- def tcp_mode!
331
- @options[:mode] = :tcp
321
+ @options[:rackup] ||= path.to_s
332
322
  end
333
323
 
334
324
  def early_hints(answer=true)
335
325
  @options[:early_hints] = answer
336
326
  end
337
327
 
338
- # Redirect STDOUT and STDERR to files specified. The +append+ parameter
328
+ # Redirect +STDOUT+ and +STDERR+ to files specified. The +append+ parameter
339
329
  # specifies whether the output is appended, the default is +false+.
340
330
  #
341
331
  # @example
@@ -376,8 +366,8 @@ module Puma
376
366
  @options[:max_threads] = max
377
367
  end
378
368
 
379
- # Instead of "bind 'ssl://127.0.0.1:9292?key=key_path&cert=cert_path'" you
380
- # can also use the "ssl_bind" option.
369
+ # Instead of `bind 'ssl://127.0.0.1:9292?key=key_path&cert=cert_path'` you
370
+ # can also use the this method.
381
371
  #
382
372
  # @example
383
373
  # ssl_bind '127.0.0.1', '9292', {
@@ -419,12 +409,23 @@ module Puma
419
409
  @options[:state] = path.to_s
420
410
  end
421
411
 
412
+ # Use +permission+ to restrict permissions for the state file.
413
+ #
414
+ # @example
415
+ # state_permission 0600
416
+ # @version 5.0.0
417
+ #
418
+ def state_permission(permission)
419
+ @options[:state_permission] = permission
420
+ end
421
+
422
422
  # How many worker processes to run. Typically this is set to
423
- # to the number of available cores.
423
+ # the number of available cores.
424
424
  #
425
425
  # The default is 0.
426
426
  #
427
427
  # @note Cluster mode only.
428
+ # @see Puma::Cluster
428
429
  def workers(count)
429
430
  @options[:workers] = count.to_i
430
431
  end
@@ -455,8 +456,8 @@ module Puma
455
456
  #
456
457
  # @note Cluster mode only.
457
458
  # @example
458
- # on_worker_fork do
459
- # puts 'Before worker fork...'
459
+ # on_worker_boot do
460
+ # puts 'Before worker boot...'
460
461
  # end
461
462
  def on_worker_boot(&block)
462
463
  @options[:before_worker_boot] ||= []
@@ -512,6 +513,29 @@ module Puma
512
513
 
513
514
  alias_method :after_worker_boot, :after_worker_fork
514
515
 
516
+ # When `fork_worker` is enabled, code to run in Worker 0
517
+ # before all other workers are re-forked from this process,
518
+ # after the server has temporarily stopped serving requests
519
+ # (once per complete refork cycle).
520
+ #
521
+ # This can be used to trigger extra garbage-collection to maximize
522
+ # copy-on-write efficiency, or close any connections to remote servers
523
+ # (database, Redis, ...) that were opened while the server was running.
524
+ #
525
+ # This can be called multiple times to add several hooks.
526
+ #
527
+ # @note Cluster mode with `fork_worker` enabled only.
528
+ # @example
529
+ # on_refork do
530
+ # 3.times {GC.start}
531
+ # end
532
+ # @version 5.0.0
533
+ #
534
+ def on_refork(&block)
535
+ @options[:before_refork] ||= []
536
+ @options[:before_refork] << block
537
+ end
538
+
515
539
  # Code to run out-of-band when the worker is idle.
516
540
  # These hooks run immediately after a request has finished
517
541
  # processing and there are no busy threads on the worker.
@@ -536,17 +560,6 @@ module Puma
536
560
  @options[:directory] = dir.to_s
537
561
  end
538
562
 
539
- # DEPRECATED: The directory to operate out of.
540
- def worker_directory(dir)
541
- $stderr.puts "worker_directory is deprecated. Please use `directory`"
542
- directory dir
543
- end
544
-
545
- # Run the app as a raw TCP app instead of an HTTP rack app.
546
- def tcp_mode
547
- @options[:mode] = :tcp
548
- end
549
-
550
563
  # Preload the application before starting the workers; this conflicts with
551
564
  # phased restart feature. This is off by default.
552
565
  #
@@ -583,7 +596,7 @@ module Puma
583
596
  # new Bundler context and thus can float around as the release
584
597
  # dictates.
585
598
  #
586
- # See also: extra_runtime_dependencies
599
+ # @see extra_runtime_dependencies
587
600
  #
588
601
  # @note This is incompatible with +preload_app!+.
589
602
  # @note This is only supported for RubyGems 2.2+
@@ -600,6 +613,9 @@ module Puma
600
613
  #
601
614
  # @example
602
615
  # raise_exception_on_sigterm false
616
+ # @see Puma::Launcher#setup_signals
617
+ # @see Puma::Cluster#setup_signals
618
+ #
603
619
  def raise_exception_on_sigterm(answer=true)
604
620
  @options[:raise_exception_on_sigterm] = answer
605
621
  end
@@ -615,6 +631,8 @@ module Puma
615
631
  # extra_runtime_dependencies ['gem_name_1', 'gem_name_2']
616
632
  # @example
617
633
  # extra_runtime_dependencies ['puma_worker_killer', 'puma-heroku']
634
+ # @see Puma::Launcher#extra_runtime_deps_directories
635
+ #
618
636
  def extra_runtime_dependencies(answer = [])
619
637
  @options[:extra_runtime_dependencies] = Array(answer)
620
638
  end
@@ -642,6 +660,8 @@ module Puma
642
660
  # @note Cluster mode only.
643
661
  # @example
644
662
  # worker_timeout 60
663
+ # @see Puma::Cluster::Worker#ping_timeout
664
+ #
645
665
  def worker_timeout(timeout)
646
666
  timeout = Integer(timeout)
647
667
  min = Const::WORKER_CHECK_INTERVAL
@@ -658,15 +678,20 @@ module Puma
658
678
  # If unspecified, this defaults to the value of worker_timeout.
659
679
  #
660
680
  # @note Cluster mode only.
661
- # @example:
681
+ #
682
+ # @example
662
683
  # worker_boot_timeout 60
684
+ # @see Puma::Cluster::Worker#ping_timeout
685
+ #
663
686
  def worker_boot_timeout(timeout)
664
687
  @options[:worker_boot_timeout] = Integer(timeout)
665
688
  end
666
689
 
667
- # Set the timeout for worker shutdown
690
+ # Set the timeout for worker shutdown.
668
691
  #
669
692
  # @note Cluster mode only.
693
+ # @see Puma::Cluster::Worker#term
694
+ #
670
695
  def worker_shutdown_timeout(timeout)
671
696
  @options[:worker_shutdown_timeout] = Integer(timeout)
672
697
  end
@@ -684,6 +709,7 @@ module Puma
684
709
  # slow clients will occupy a handler thread while the request
685
710
  # is being sent. A reverse proxy, such as nginx, can handle
686
711
  # slow clients and queue requests before they reach Puma.
712
+ # @see Puma::Server
687
713
  def queue_requests(answer=true)
688
714
  @options[:queue_requests] = answer
689
715
  end
@@ -691,10 +717,25 @@ module Puma
691
717
  # When a shutdown is requested, the backtraces of all the
692
718
  # threads will be written to $stdout. This can help figure
693
719
  # out why shutdown is hanging.
720
+ #
694
721
  def shutdown_debug(val=true)
695
722
  @options[:shutdown_debug] = val
696
723
  end
697
724
 
725
+
726
+ # Attempts to route traffic to less-busy workers by causing them to delay
727
+ # listening on the socket, allowing workers which are not processing any
728
+ # requests to pick up new requests first.
729
+ #
730
+ # Only works on MRI. For all other interpreters, this setting does nothing.
731
+ # @see Puma::Server#handle_servers
732
+ # @see Puma::ThreadPool#wait_for_less_busy_worker
733
+ # @version 5.0.0
734
+ #
735
+ def wait_for_less_busy_worker(val=0.005)
736
+ @options[:wait_for_less_busy_worker] = val.to_f
737
+ end
738
+
698
739
  # Control how the remote address of the connection is set. This
699
740
  # is configurable because to calculate the true socket peer address
700
741
  # a kernel syscall is required which for very fast rack handlers
@@ -702,18 +743,18 @@ module Puma
702
743
  #
703
744
  # There are 4 possible values:
704
745
  #
705
- # * :socket (the default) - read the peername from the socket using the
706
- # syscall. This is the normal behavior.
707
- # * :localhost - set the remote address to "127.0.0.1"
708
- # * header: http_header - set the remote address to the value of the
709
- # provided http header. For instance:
710
- # `set_remote_address header: "X-Real-IP"`.
711
- # Only the first word (as separated by spaces or comma)
712
- # is used, allowing headers such as X-Forwarded-For
713
- # to be used as well.
714
- # * Any string - this allows you to hardcode remote address to any value
715
- # you wish. Because Puma never uses this field anyway, it's
716
- # format is entirely in your hands.
746
+ # 1. **:socket** (the default) - read the peername from the socket using the
747
+ # syscall. This is the normal behavior.
748
+ # 2. **:localhost** - set the remote address to "127.0.0.1"
749
+ # 3. **header: <http_header>**- set the remote address to the value of the
750
+ # provided http header. For instance:
751
+ # `set_remote_address header: "X-Real-IP"`.
752
+ # Only the first word (as separated by spaces or comma) is used, allowing
753
+ # headers such as X-Forwarded-For to be used as well.
754
+ # 4. **\<Any string\>** - this allows you to hardcode remote address to any value
755
+ # you wish. Because Puma never uses this field anyway, it's format is
756
+ # entirely in your hands.
757
+ #
717
758
  def set_remote_address(val=:socket)
718
759
  case val
719
760
  when :socket
@@ -736,5 +777,38 @@ module Puma
736
777
  end
737
778
  end
738
779
 
780
+ # When enabled, workers will be forked from worker 0 instead of from the master process.
781
+ # This option is similar to `preload_app` because the app is preloaded before forking,
782
+ # but it is compatible with phased restart.
783
+ #
784
+ # This option also enables the `refork` command (SIGURG), which optimizes copy-on-write performance
785
+ # in a running app.
786
+ #
787
+ # A refork will automatically trigger once after the specified number of requests
788
+ # (default 1000), or pass 0 to disable auto refork.
789
+ #
790
+ # @note Cluster mode only.
791
+ # @version 5.0.0
792
+ #
793
+ def fork_worker(after_requests=1000)
794
+ @options[:fork_worker] = Integer(after_requests)
795
+ end
796
+
797
+ # When enabled, Puma will GC 4 times before forking workers.
798
+ # If available (Ruby 2.7+), we will also call GC.compact.
799
+ # Not recommended for non-MRI Rubies.
800
+ #
801
+ # Based on the work of Koichi Sasada and Aaron Patterson, this option may
802
+ # decrease memory utilization of preload-enabled cluster-mode Pumas. It will
803
+ # also increase time to boot and fork. See your logs for details on how much
804
+ # time this adds to your boot process. For most apps, it will be less than one
805
+ # second.
806
+ #
807
+ # @see Puma::Cluster#nakayoshi_gc
808
+ # @version 5.0.0
809
+ #
810
+ def nakayoshi_fork(enabled=true)
811
+ @options[:nakayoshi_fork] = enabled
812
+ end
739
813
  end
740
814
  end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'puma/const'
4
+
5
+ module Puma
6
+ # The implementation of a detailed error logging.
7
+ # @version 5.0.0
8
+ #
9
+ class ErrorLogger
10
+ include Const
11
+
12
+ attr_reader :ioerr
13
+
14
+ REQUEST_FORMAT = %{"%s %s%s" - (%s)}
15
+
16
+ def initialize(ioerr)
17
+ @ioerr = ioerr
18
+ @ioerr.sync = true
19
+
20
+ @debug = ENV.key? 'PUMA_DEBUG'
21
+ end
22
+
23
+ def self.stdio
24
+ new $stderr
25
+ end
26
+
27
+ # Print occured error details.
28
+ # +options+ hash with additional options:
29
+ # - +error+ is an exception object
30
+ # - +req+ the http request
31
+ # - +text+ (default nil) custom string to print in title
32
+ # and before all remaining info.
33
+ #
34
+ def info(options={})
35
+ ioerr.puts title(options)
36
+ end
37
+
38
+ # Print occured error details only if
39
+ # environment variable PUMA_DEBUG is defined.
40
+ # +options+ hash with additional options:
41
+ # - +error+ is an exception object
42
+ # - +req+ the http request
43
+ # - +text+ (default nil) custom string to print in title
44
+ # and before all remaining info.
45
+ #
46
+ def debug(options={})
47
+ return unless @debug
48
+
49
+ error = options[:error]
50
+ req = options[:req]
51
+
52
+ string_block = []
53
+ string_block << title(options)
54
+ string_block << request_dump(req) if req
55
+ string_block << error_backtrace(options) if error
56
+
57
+ ioerr.puts string_block.join("\n")
58
+ end
59
+
60
+ def title(options={})
61
+ text = options[:text]
62
+ req = options[:req]
63
+ error = options[:error]
64
+
65
+ string_block = ["#{Time.now}"]
66
+ string_block << " #{text}" if text
67
+ string_block << " (#{request_title(req)})" if request_parsed?(req)
68
+ string_block << ": #{error.inspect}" if error
69
+ string_block.join('')
70
+ end
71
+
72
+ def request_dump(req)
73
+ "Headers: #{request_headers(req)}\n" \
74
+ "Body: #{req.body}"
75
+ end
76
+
77
+ def request_title(req)
78
+ env = req.env
79
+
80
+ REQUEST_FORMAT % [
81
+ env[REQUEST_METHOD],
82
+ env[REQUEST_PATH] || env[PATH_INFO],
83
+ env[QUERY_STRING] || "",
84
+ env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR] || "-"
85
+ ]
86
+ end
87
+
88
+ def request_headers(req)
89
+ headers = req.env.select { |key, _| key.start_with?('HTTP_') }
90
+ headers.map { |key, value| [key[5..-1], value] }.to_h.inspect
91
+ end
92
+
93
+ def request_parsed?(req)
94
+ req && req.env[REQUEST_METHOD]
95
+ end
96
+ end
97
+ end
data/lib/puma/events.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'puma/const'
4
3
  require "puma/null_io"
4
+ require 'puma/error_logger'
5
5
  require 'stringio'
6
6
 
7
7
  module Puma
@@ -23,8 +23,6 @@ module Puma
23
23
  end
24
24
  end
25
25
 
26
- include Const
27
-
28
26
  # Create an Events object that prints to +stdout+ and +stderr+.
29
27
  #
30
28
  def initialize(stdout, stderr)
@@ -36,6 +34,7 @@ module Puma
36
34
  @stderr.sync = true
37
35
 
38
36
  @debug = ENV.key? 'PUMA_DEBUG'
37
+ @error_logger = ErrorLogger.new(@stderr)
39
38
 
40
39
  @hooks = Hash.new { |h,k| h[k] = [] }
41
40
  end
@@ -66,7 +65,8 @@ module Puma
66
65
  # Write +str+ to +@stdout+
67
66
  #
68
67
  def log(str)
69
- @stdout.puts format(str)
68
+ @stdout.puts format(str) if @stdout.respond_to? :puts
69
+ rescue Errno::EPIPE
70
70
  end
71
71
 
72
72
  def write(str)
@@ -80,7 +80,7 @@ module Puma
80
80
  # Write +str+ to +@stderr+
81
81
  #
82
82
  def error(str)
83
- @stderr.puts format("ERROR: #{str}")
83
+ @error_logger.info(text: format("ERROR: #{str}"))
84
84
  exit 1
85
85
  end
86
86
 
@@ -88,43 +88,47 @@ module Puma
88
88
  formatter.call(str)
89
89
  end
90
90
 
91
+ # An HTTP connection error has occurred.
92
+ # +error+ a connection exception, +req+ the request,
93
+ # and +text+ additional info
94
+ # @version 5.0.0
95
+ #
96
+ def connection_error(error, req, text="HTTP connection error")
97
+ @error_logger.info(error: error, req: req, text: text)
98
+ end
99
+
91
100
  # An HTTP parse error has occurred.
92
- # +server+ is the Server object, +env+ the request, and +error+ a
93
- # parsing exception.
101
+ # +error+ a parsing exception,
102
+ # and +req+ the request.
94
103
  #
95
- def parse_error(server, env, error)
96
- @stderr.puts "#{Time.now}: HTTP parse error, malformed request " \
97
- "(#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]}#{env[REQUEST_PATH]}): " \
98
- "#{error.inspect}" \
99
- "\n---\n"
104
+ def parse_error(error, req)
105
+ @error_logger.info(error: error, req: req, text: 'HTTP parse error, malformed request')
100
106
  end
101
107
 
102
108
  # An SSL error has occurred.
103
- # +server+ is the Server object, +peeraddr+ peer address, +peercert+
104
- # any peer certificate (if present), and +error+ an exception object.
109
+ # +error+ an exception object, +peeraddr+ peer address,
110
+ # and +peercert+ any peer certificate (if present).
105
111
  #
106
- def ssl_error(server, peeraddr, peercert, error)
112
+ def ssl_error(error, peeraddr, peercert)
107
113
  subject = peercert ? peercert.subject : nil
108
- @stderr.puts "#{Time.now}: SSL error, peer: #{peeraddr}, peer cert: #{subject}, #{error.inspect}"
114
+ @error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}")
109
115
  end
110
116
 
111
117
  # An unknown error has occurred.
112
- # +server+ is the Server object, +error+ an exception object,
113
- # +kind+ some additional info, and +env+ the request.
118
+ # +error+ an exception object, +req+ the request,
119
+ # and +text+ additional info
114
120
  #
115
- def unknown_error(server, error, kind="Unknown", env=nil)
116
- if error.respond_to? :render
117
- error.render "#{Time.now}: #{kind} error", @stderr
118
- else
119
- if env
120
- string_block = [ "#{Time.now}: #{kind} error handling request { #{env['REQUEST_METHOD']} #{env['PATH_INFO']} }" ]
121
- string_block << error.inspect
122
- else
123
- string_block = [ "#{Time.now}: #{kind} error: #{error.inspect}" ]
124
- end
125
- string_block << error.backtrace
126
- @stderr.puts string_block.join("\n")
127
- end
121
+ def unknown_error(error, req=nil, text="Unknown error")
122
+ @error_logger.info(error: error, req: req, text: text)
123
+ end
124
+
125
+ # Log occurred error debug dump.
126
+ # +error+ an exception object, +req+ the request,
127
+ # and +text+ additional info
128
+ # @version 5.0.0
129
+ #
130
+ def debug_error(error, req=nil, text="")
131
+ @error_logger.debug(error: error, req: req, text: text)
128
132
  end
129
133
 
130
134
  def on_booted(&block)
@@ -1,4 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'puma/detect'
4
- require 'puma/puma_http11'
3
+ module Puma
4
+ class IOBuffer < String
5
+ def append(*args)
6
+ args.each { |a| concat(a) }
7
+ end
8
+
9
+ alias reset clear
10
+ end
11
+ end
@@ -22,63 +22,5 @@ module Puma
22
22
  execlp(cmd, *argv)
23
23
  raise SystemCallError.new(FFI.errno)
24
24
  end
25
-
26
- PermKey = 'PUMA_DAEMON_PERM'
27
- RestartKey = 'PUMA_DAEMON_RESTART'
28
-
29
- # Called to tell things "Your now always in daemon mode,
30
- # don't try to reenter it."
31
- #
32
- def self.perm_daemonize
33
- ENV[PermKey] = "1"
34
- end
35
-
36
- def self.daemon?
37
- ENV.key?(PermKey) || ENV.key?(RestartKey)
38
- end
39
-
40
- def self.daemon_init
41
- return true if ENV.key?(PermKey)
42
-
43
- return false unless ENV.key? RestartKey
44
-
45
- master = ENV[RestartKey]
46
-
47
- # In case the master disappears early
48
- begin
49
- Process.kill "SIGUSR2", master.to_i
50
- rescue SystemCallError => e
51
- end
52
-
53
- ENV[RestartKey] = ""
54
-
55
- setsid
56
-
57
- null = File.open "/dev/null", "w+"
58
- STDIN.reopen null
59
- STDOUT.reopen null
60
- STDERR.reopen null
61
-
62
- true
63
- end
64
-
65
- def self.daemon_start(dir, argv)
66
- ENV[RestartKey] = Process.pid.to_s
67
-
68
- if k = ENV['PUMA_JRUBY_DAEMON_OPTS']
69
- ENV['JRUBY_OPTS'] = k
70
- end
71
-
72
- cmd = argv.first
73
- argv = ([:string] * argv.size).zip(argv).flatten
74
- argv << :string
75
- argv << nil
76
-
77
- chdir(dir)
78
- ret = fork
79
- return ret if ret != 0
80
- execlp(cmd, *argv)
81
- raise SystemCallError.new(FFI.errno)
82
- end
83
25
  end
84
26
  end