tiny-fast-gem 0.0.1

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 (99) hide show
  1. checksums.yaml +7 -0
  2. data/puma-8.0.2/History.md +3334 -0
  3. data/puma-8.0.2/LICENSE +29 -0
  4. data/puma-8.0.2/README.md +484 -0
  5. data/puma-8.0.2/bin/puma +10 -0
  6. data/puma-8.0.2/bin/puma-wild +25 -0
  7. data/puma-8.0.2/bin/pumactl +12 -0
  8. data/puma-8.0.2/docs/5.0-Upgrade.md +98 -0
  9. data/puma-8.0.2/docs/6.0-Upgrade.md +56 -0
  10. data/puma-8.0.2/docs/7.0-Upgrade.md +52 -0
  11. data/puma-8.0.2/docs/8.0-Upgrade.md +100 -0
  12. data/puma-8.0.2/docs/architecture.md +74 -0
  13. data/puma-8.0.2/docs/compile_options.md +55 -0
  14. data/puma-8.0.2/docs/deployment.md +137 -0
  15. data/puma-8.0.2/docs/fork_worker.md +41 -0
  16. data/puma-8.0.2/docs/grpc.md +62 -0
  17. data/puma-8.0.2/docs/images/favicon.svg +1 -0
  18. data/puma-8.0.2/docs/images/puma-connection-flow-no-reactor.png +0 -0
  19. data/puma-8.0.2/docs/images/puma-connection-flow.png +0 -0
  20. data/puma-8.0.2/docs/images/puma-general-arch.png +0 -0
  21. data/puma-8.0.2/docs/images/running-puma.svg +1 -0
  22. data/puma-8.0.2/docs/images/standard-logo.svg +1 -0
  23. data/puma-8.0.2/docs/java_options.md +54 -0
  24. data/puma-8.0.2/docs/jungle/README.md +9 -0
  25. data/puma-8.0.2/docs/jungle/rc.d/README.md +74 -0
  26. data/puma-8.0.2/docs/jungle/rc.d/puma +61 -0
  27. data/puma-8.0.2/docs/jungle/rc.d/puma.conf +10 -0
  28. data/puma-8.0.2/docs/kubernetes.md +73 -0
  29. data/puma-8.0.2/docs/nginx.md +80 -0
  30. data/puma-8.0.2/docs/plugins.md +42 -0
  31. data/puma-8.0.2/docs/rails_dev_mode.md +28 -0
  32. data/puma-8.0.2/docs/restart.md +65 -0
  33. data/puma-8.0.2/docs/signals.md +98 -0
  34. data/puma-8.0.2/docs/stats.md +148 -0
  35. data/puma-8.0.2/docs/systemd.md +253 -0
  36. data/puma-8.0.2/docs/testing_benchmarks_local_files.md +150 -0
  37. data/puma-8.0.2/docs/testing_test_rackup_ci_files.md +36 -0
  38. data/puma-8.0.2/ext/puma_http11/PumaHttp11Service.java +17 -0
  39. data/puma-8.0.2/ext/puma_http11/extconf.rb +65 -0
  40. data/puma-8.0.2/ext/puma_http11/http11_parser.c +1057 -0
  41. data/puma-8.0.2/ext/puma_http11/http11_parser.h +65 -0
  42. data/puma-8.0.2/ext/puma_http11/http11_parser.java.rl +131 -0
  43. data/puma-8.0.2/ext/puma_http11/http11_parser.rl +149 -0
  44. data/puma-8.0.2/ext/puma_http11/http11_parser_common.rl +54 -0
  45. data/puma-8.0.2/ext/puma_http11/mini_ssl.c +852 -0
  46. data/puma-8.0.2/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  47. data/puma-8.0.2/ext/puma_http11/org/jruby/puma/EnvKey.java +241 -0
  48. data/puma-8.0.2/ext/puma_http11/org/jruby/puma/Http11.java +321 -0
  49. data/puma-8.0.2/ext/puma_http11/org/jruby/puma/Http11Parser.java +441 -0
  50. data/puma-8.0.2/ext/puma_http11/org/jruby/puma/MiniSSL.java +509 -0
  51. data/puma-8.0.2/ext/puma_http11/puma_http11.c +499 -0
  52. data/puma-8.0.2/lib/puma/app/status.rb +104 -0
  53. data/puma-8.0.2/lib/puma/binder.rb +511 -0
  54. data/puma-8.0.2/lib/puma/cli.rb +245 -0
  55. data/puma-8.0.2/lib/puma/client.rb +756 -0
  56. data/puma-8.0.2/lib/puma/client_env.rb +171 -0
  57. data/puma-8.0.2/lib/puma/cluster/worker.rb +183 -0
  58. data/puma-8.0.2/lib/puma/cluster/worker_handle.rb +127 -0
  59. data/puma-8.0.2/lib/puma/cluster.rb +634 -0
  60. data/puma-8.0.2/lib/puma/cluster_accept_loop_delay.rb +91 -0
  61. data/puma-8.0.2/lib/puma/commonlogger.rb +115 -0
  62. data/puma-8.0.2/lib/puma/configuration.rb +522 -0
  63. data/puma-8.0.2/lib/puma/const.rb +308 -0
  64. data/puma-8.0.2/lib/puma/control_cli.rb +320 -0
  65. data/puma-8.0.2/lib/puma/detect.rb +58 -0
  66. data/puma-8.0.2/lib/puma/dsl.rb +1562 -0
  67. data/puma-8.0.2/lib/puma/error_logger.rb +115 -0
  68. data/puma-8.0.2/lib/puma/events.rb +72 -0
  69. data/puma-8.0.2/lib/puma/io_buffer.rb +50 -0
  70. data/puma-8.0.2/lib/puma/jruby_restart.rb +11 -0
  71. data/puma-8.0.2/lib/puma/json_serialization.rb +96 -0
  72. data/puma-8.0.2/lib/puma/launcher/bundle_pruner.rb +102 -0
  73. data/puma-8.0.2/lib/puma/launcher.rb +501 -0
  74. data/puma-8.0.2/lib/puma/log_writer.rb +153 -0
  75. data/puma-8.0.2/lib/puma/minissl/context_builder.rb +96 -0
  76. data/puma-8.0.2/lib/puma/minissl.rb +458 -0
  77. data/puma-8.0.2/lib/puma/null_io.rb +101 -0
  78. data/puma-8.0.2/lib/puma/plugin/systemd.rb +90 -0
  79. data/puma-8.0.2/lib/puma/plugin/tmp_restart.rb +36 -0
  80. data/puma-8.0.2/lib/puma/plugin.rb +111 -0
  81. data/puma-8.0.2/lib/puma/rack/builder.rb +297 -0
  82. data/puma-8.0.2/lib/puma/rack/urlmap.rb +93 -0
  83. data/puma-8.0.2/lib/puma/rack_default.rb +24 -0
  84. data/puma-8.0.2/lib/puma/reactor.rb +131 -0
  85. data/puma-8.0.2/lib/puma/response.rb +532 -0
  86. data/puma-8.0.2/lib/puma/runner.rb +211 -0
  87. data/puma-8.0.2/lib/puma/sd_notify.rb +146 -0
  88. data/puma-8.0.2/lib/puma/server.rb +773 -0
  89. data/puma-8.0.2/lib/puma/server_plugin_control.rb +32 -0
  90. data/puma-8.0.2/lib/puma/single.rb +72 -0
  91. data/puma-8.0.2/lib/puma/state_file.rb +69 -0
  92. data/puma-8.0.2/lib/puma/thread_pool.rb +517 -0
  93. data/puma-8.0.2/lib/puma/util.rb +134 -0
  94. data/puma-8.0.2/lib/puma.rb +88 -0
  95. data/puma-8.0.2/lib/rack/handler/puma.rb +144 -0
  96. data/puma-8.0.2/tools/Dockerfile +26 -0
  97. data/puma-8.0.2/tools/trickletest.rb +44 -0
  98. data/tiny-fast-gem.gemspec +12 -0
  99. metadata +138 -0
@@ -0,0 +1,1562 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'const'
4
+ require_relative 'util'
5
+
6
+ module Puma
7
+ # The methods that are available for use inside the configuration file.
8
+ # These same methods are used in Puma cli and the rack handler
9
+ # internally.
10
+ #
11
+ # Used manually (via CLI class):
12
+ #
13
+ # config = Configuration.new({}) do |user_config|
14
+ # user_config.port 3001
15
+ # end
16
+ # config.clamp
17
+ #
18
+ # puts config.options[:binds] # => "tcp://127.0.0.1:3001"
19
+ #
20
+ # Used to load file:
21
+ #
22
+ # $ cat puma_config.rb
23
+ # port 3002
24
+ #
25
+ # Resulting configuration:
26
+ #
27
+ # config = Configuration.new(config_file: "puma_config.rb")
28
+ # config.clamp
29
+ #
30
+ # puts config.options[:binds] # => "tcp://127.0.0.1:3002"
31
+ #
32
+ # You can also find many examples being used by the test suite in
33
+ # +test/config+.
34
+ #
35
+ # Puma v6 adds the option to specify a key name (String or Symbol) to the
36
+ # hooks that run inside the forked workers. All the hooks run inside the
37
+ # {Puma::Cluster::Worker#run} method.
38
+ #
39
+ # Previously, the worker index and the LogWriter instance were passed to the
40
+ # hook blocks/procs. If a key name is specified, a hash is passed as the last
41
+ # parameter. This allows storage of data, typically objects that are created
42
+ # before the worker that need to be passed to the hook when the worker is shutdown.
43
+ #
44
+ # The following hooks have been updated:
45
+ #
46
+ # | DSL Method | Options Key | Fork Block Location |
47
+ # | before_worker_boot | :before_worker_boot | inside, before |
48
+ # | before_worker_shutdown | :before_worker_shutdown | inside, after |
49
+ # | before_refork | :before_refork | inside |
50
+ # | after_refork | :after_refork | inside |
51
+ #
52
+ class DSL
53
+ ON_WORKER_KEY = [String, Symbol].freeze
54
+
55
+ # Convenience method so logic can be used in CI.
56
+ #
57
+ # @see ssl_bind
58
+ #
59
+ def self.ssl_bind_str(host, port, opts)
60
+ verify = opts.fetch(:verify_mode, 'none').to_s
61
+
62
+ tls_str =
63
+ if opts[:no_tlsv1_1] then '&no_tlsv1_1=true'
64
+ elsif opts[:no_tlsv1] then '&no_tlsv1=true'
65
+ else ''
66
+ end
67
+
68
+ ca_additions = "&ca=#{Puma::Util.escape(opts[:ca])}" if ['peer', 'force_peer'].include?(verify)
69
+
70
+ low_latency_str = opts.key?(:low_latency) ? "&low_latency=#{opts[:low_latency]}" : ''
71
+ backlog_str = opts[:backlog] ? "&backlog=#{Integer(opts[:backlog])}" : ''
72
+
73
+ if defined?(JRUBY_VERSION)
74
+ cipher_suites = opts[:ssl_cipher_list] ? "&ssl_cipher_list=#{opts[:ssl_cipher_list]}" : nil # old name
75
+ cipher_suites = "#{cipher_suites}&cipher_suites=#{opts[:cipher_suites]}" if opts[:cipher_suites]
76
+ protocols = opts[:protocols] ? "&protocols=#{opts[:protocols]}" : nil
77
+
78
+ keystore_additions = "keystore=#{opts[:keystore]}&keystore-pass=#{opts[:keystore_pass]}"
79
+ keystore_additions = "#{keystore_additions}&keystore-type=#{opts[:keystore_type]}" if opts[:keystore_type]
80
+ if opts[:truststore]
81
+ truststore_additions = "&truststore=#{opts[:truststore]}"
82
+ truststore_additions = "#{truststore_additions}&truststore-pass=#{opts[:truststore_pass]}" if opts[:truststore_pass]
83
+ truststore_additions = "#{truststore_additions}&truststore-type=#{opts[:truststore_type]}" if opts[:truststore_type]
84
+ end
85
+
86
+ "ssl://#{host}:#{port}?#{keystore_additions}#{truststore_additions}#{cipher_suites}#{protocols}" \
87
+ "&verify_mode=#{verify}#{tls_str}#{ca_additions}#{backlog_str}"
88
+ else
89
+ ssl_cipher_filter = opts[:ssl_cipher_filter] ? "&ssl_cipher_filter=#{opts[:ssl_cipher_filter]}" : nil
90
+ ssl_ciphersuites = opts[:ssl_ciphersuites] ? "&ssl_ciphersuites=#{opts[:ssl_ciphersuites]}" : nil
91
+ v_flags = (ary = opts[:verification_flags]) ? "&verification_flags=#{Array(ary).join ','}" : nil
92
+
93
+ cert_flags = (cert = opts[:cert]) ? "cert=#{Puma::Util.escape(cert)}" : nil
94
+ key_flags = (key = opts[:key]) ? "&key=#{Puma::Util.escape(key)}" : nil
95
+ password_flags = (password_command = opts[:key_password_command]) ? "&key_password_command=#{Puma::Util.escape(password_command)}" : nil
96
+
97
+ reuse_flag =
98
+ if (reuse = opts[:reuse])
99
+ if reuse == true
100
+ '&reuse=dflt'
101
+ elsif reuse.is_a?(Hash) && (reuse.key?(:size) || reuse.key?(:timeout))
102
+ val = +''
103
+ if (size = reuse[:size]) && Integer === size
104
+ val << size.to_s
105
+ end
106
+ if (timeout = reuse[:timeout]) && Integer === timeout
107
+ val << ",#{timeout}"
108
+ end
109
+ if val.empty?
110
+ nil
111
+ else
112
+ "&reuse=#{val}"
113
+ end
114
+ else
115
+ nil
116
+ end
117
+ else
118
+ nil
119
+ end
120
+
121
+ "ssl://#{host}:#{port}?#{cert_flags}#{key_flags}#{password_flags}#{ssl_cipher_filter}#{ssl_ciphersuites}" \
122
+ "#{reuse_flag}&verify_mode=#{verify}#{tls_str}#{ca_additions}#{v_flags}#{backlog_str}#{low_latency_str}"
123
+ end
124
+ end
125
+
126
+ def initialize(options, config)
127
+ @config = config
128
+ @options = options
129
+
130
+ @plugins = []
131
+ end
132
+
133
+ def _load_from(path)
134
+ if path
135
+ @path = path
136
+ instance_eval(File.read(path), path, 1)
137
+ end
138
+ ensure
139
+ _offer_plugins
140
+ end
141
+
142
+ def _offer_plugins
143
+ @plugins.each do |o|
144
+ if o.respond_to? :config
145
+ @options.shift
146
+ o.config self
147
+ end
148
+ end
149
+
150
+ @plugins.clear
151
+ end
152
+
153
+ def set_default_host(host)
154
+ @options[:default_host] = host
155
+ end
156
+
157
+ def default_host
158
+ @options[:default_host] || Configuration.default_tcp_host
159
+ end
160
+
161
+ def inject(&blk)
162
+ instance_eval(&blk)
163
+ end
164
+
165
+ def get(key,default=nil)
166
+ @options[key.to_sym] || default
167
+ end
168
+
169
+ # Load the named plugin for use by this configuration.
170
+ #
171
+ # @example
172
+ # plugin :tmp_restart
173
+ #
174
+ def plugin(name)
175
+ @plugins << @config.load_plugin(name)
176
+ end
177
+
178
+ # Use an object or block as the rack application. This allows the
179
+ # configuration file to be the application itself.
180
+ #
181
+ # @example
182
+ # app do |env|
183
+ # body = 'Hello, World!'
184
+ #
185
+ # [
186
+ # 200,
187
+ # {
188
+ # 'Content-Type' => 'text/plain',
189
+ # 'Content-Length' => body.length.to_s
190
+ # },
191
+ # [body]
192
+ # ]
193
+ # end
194
+ #
195
+ # @see Puma::Configuration#app
196
+ #
197
+ def app(obj=nil, &block)
198
+ obj ||= block
199
+
200
+ raise "Provide either a #call'able or a block" unless obj
201
+
202
+ @options[:app] = obj
203
+ end
204
+
205
+ # Start the Puma control rack application on +url+. This application can
206
+ # be communicated with to control the main server. Additionally, you can
207
+ # provide an authentication token, so all requests to the control server
208
+ # will need to include that token as a query parameter. This allows for
209
+ # simple authentication.
210
+ #
211
+ # Check out {Puma::App::Status} to see what the app has available.
212
+ #
213
+ # @example
214
+ # activate_control_app 'unix:///var/run/pumactl.sock'
215
+ # @example
216
+ # activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345' }
217
+ # @example
218
+ # activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true }
219
+ # @example
220
+ # activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true, data_only: true}
221
+ #
222
+ def activate_control_app(url="auto", opts={})
223
+ if url == "auto"
224
+ path = Configuration.temp_path
225
+ @options[:control_url] = "unix://#{path}"
226
+ @options[:control_url_temp] = path
227
+ else
228
+ @options[:control_url] = url
229
+ end
230
+
231
+ if opts[:no_token]
232
+ # We need to use 'none' rather than :none because this value will be
233
+ # passed on to an instance of OptionParser, which doesn't support
234
+ # symbols as option values.
235
+ #
236
+ # See: https://github.com/puma/puma/issues/1193#issuecomment-305995488
237
+ auth_token = 'none'
238
+ else
239
+ auth_token = opts[:auth_token]
240
+ auth_token ||= Configuration.random_token
241
+ end
242
+
243
+ @options[:control_auth_token] = auth_token
244
+ @options[:control_url_umask] = opts[:umask] if opts[:umask]
245
+ @options[:control_data_only] = opts[:data_only] if opts[:data_only]
246
+ end
247
+
248
+ # Load additional configuration from a file.
249
+ # Files get loaded later via Configuration#load.
250
+ #
251
+ # @example
252
+ # load 'config/puma/production.rb'
253
+ #
254
+ def load(file)
255
+ @options[:config_files] ||= []
256
+ @options[:config_files] << file
257
+ end
258
+
259
+ # Bind the server to +url+. "tcp://", "unix://" and "ssl://" are the only
260
+ # accepted protocols. Multiple urls can be bound to, calling +bind+ does
261
+ # not overwrite previous bindings.
262
+ #
263
+ # The default is "tcp://[::]:9292" when IPv6 interfaces are available,
264
+ # otherwise "tcp://0.0.0.0:9292".
265
+ #
266
+ # You can use query parameters within the url to specify options:
267
+ #
268
+ # * Set the socket backlog depth with +backlog+, default is 1024.
269
+ # * Set up an SSL certificate with +key+ & +cert+.
270
+ # * Set up an SSL certificate for mTLS with +key+, +cert+, +ca+ and +verify_mode+.
271
+ # * Set whether to optimize for low latency instead of throughput with
272
+ # +low_latency+, default is to not optimize for low latency. This is done
273
+ # via +Socket::TCP_NODELAY+.
274
+ # * Set socket permissions with +umask+.
275
+ #
276
+ # @example Backlog depth
277
+ # bind 'unix:///var/run/puma.sock?backlog=512'
278
+ # @example SSL cert
279
+ # bind 'ssl://127.0.0.1:9292?key=key.key&cert=cert.pem'
280
+ # @example SSL cert for mutual TLS (mTLS)
281
+ # bind 'ssl://127.0.0.1:9292?key=key.key&cert=cert.pem&ca=ca.pem&verify_mode=force_peer'
282
+ # @example Disable optimization for low latency
283
+ # bind 'tcp://[::]:9292?low_latency=false'
284
+ # @example Socket permissions
285
+ # bind 'unix:///var/run/puma.sock?umask=0111'
286
+ #
287
+ # @see Puma::Runner#load_and_bind
288
+ # @see Puma::Cluster#run
289
+ #
290
+ def bind(url)
291
+ @options[:binds] ||= []
292
+ @options[:binds] << url
293
+ end
294
+
295
+ def clear_binds!
296
+ @options[:binds] = []
297
+ end
298
+
299
+ # Bind to (systemd) activated sockets, regardless of configured binds.
300
+ #
301
+ # Systemd can present sockets as file descriptors that are already opened.
302
+ # By default Puma will use these but only if it was explicitly told to bind
303
+ # to the socket. If not, it will close the activated sockets. This means
304
+ # all configuration is duplicated.
305
+ #
306
+ # Binds can contain additional configuration, but only SSL config is really
307
+ # relevant since the unix and TCP socket options are ignored.
308
+ #
309
+ # This means there is a lot of duplicated configuration for no additional
310
+ # value in most setups. This method tells the launcher to bind to all
311
+ # activated sockets, regardless of existing bind.
312
+ #
313
+ # To clear configured binds, the value only can be passed. This will clear
314
+ # out any binds that may have been configured.
315
+ #
316
+ # @example Use any systemd activated sockets as well as configured binds
317
+ # bind_to_activated_sockets
318
+ #
319
+ # @example Only bind to systemd activated sockets, ignoring other binds
320
+ # bind_to_activated_sockets 'only'
321
+ #
322
+ def bind_to_activated_sockets(bind=true)
323
+ @options[:bind_to_activated_sockets] = bind
324
+ end
325
+
326
+ # Define the TCP port to bind to. Use `bind` for more advanced options.
327
+ #
328
+ # The default is +9292+.
329
+ #
330
+ # @example
331
+ # port 3000
332
+ #
333
+ def port(port, host=nil)
334
+ host ||= default_host
335
+ bind URI::Generic.build(scheme: 'tcp', host: host, port: Integer(port)).to_s
336
+ end
337
+
338
+ # Define how long the tcp socket stays open, if no data has been received.
339
+ #
340
+ # The default is 30 seconds.
341
+ #
342
+ # @example
343
+ # first_data_timeout 40
344
+ #
345
+ # @see Puma::Server.new
346
+ #
347
+ def first_data_timeout(seconds)
348
+ @options[:first_data_timeout] = Integer(seconds)
349
+ end
350
+
351
+ # Define how long persistent connections can be idle before Puma closes them.
352
+ #
353
+ # The default is 65 seconds.
354
+ #
355
+ # @example
356
+ # persistent_timeout 30
357
+ #
358
+ # @see Puma::Server.new
359
+ #
360
+ def persistent_timeout(seconds)
361
+ @options[:persistent_timeout] = Integer(seconds)
362
+ end
363
+
364
+ # If a new request is not received within this number of seconds, begin shutting down.
365
+ #
366
+ # The default is +nil+.
367
+ #
368
+ # @example
369
+ # idle_timeout 60
370
+ #
371
+ # @see Puma::Server.new
372
+ #
373
+ def idle_timeout(seconds)
374
+ @options[:idle_timeout] = Integer(seconds)
375
+ end
376
+
377
+ # Use a clean fiber per request which ensures a clean slate for fiber
378
+ # locals and fiber storage. Also provides a cleaner backtrace with less
379
+ # Puma internal stack frames.
380
+ #
381
+ # The default is +false+.
382
+ #
383
+ # @example
384
+ # fiber_per_request
385
+ #
386
+ def fiber_per_request(which=true)
387
+ @options[:fiber_per_request] = which
388
+ end
389
+
390
+ alias clean_thread_locals fiber_per_request
391
+
392
+ # When shutting down, drain the accept socket of pending connections and
393
+ # process them. This loops over the accept socket until there are no more
394
+ # read events and then stops looking and waits for the requests to finish.
395
+ #
396
+ # @see Puma::Server#graceful_shutdown
397
+ #
398
+ def drain_on_shutdown(which=true)
399
+ @options[:drain_on_shutdown] = which
400
+ end
401
+
402
+ # Set the environment in which the rack's app will run. The value must be
403
+ # a string.
404
+ #
405
+ # The default is "development".
406
+ #
407
+ # @example
408
+ # environment 'production'
409
+ #
410
+ def environment(environment)
411
+ @options[:environment] = environment
412
+ end
413
+
414
+ # How long to wait for threads to stop when shutting them down.
415
+ # Specifying :immediately will cause Puma to kill the threads immediately.
416
+ # Otherwise the value is the number of seconds to wait.
417
+ #
418
+ # Puma always waits a few seconds after killing a thread for it to try
419
+ # to finish up it's work, even in :immediately mode.
420
+ #
421
+ # The default is +:forever+.
422
+ #
423
+ # @see Puma::Server#graceful_shutdown
424
+ #
425
+ def force_shutdown_after(val=:forever)
426
+ i = case val
427
+ when :forever
428
+ -1
429
+ when :immediately
430
+ 0
431
+ else
432
+ Float(val)
433
+ end
434
+
435
+ @options[:force_shutdown_after] = i
436
+ end
437
+
438
+ # Code to run before doing a restart. This code should
439
+ # close log files, database connections, etc.
440
+ #
441
+ # This can be called multiple times to add code each time.
442
+ #
443
+ # @example
444
+ # before_restart do
445
+ # puts 'On restart...'
446
+ # end
447
+ #
448
+ def before_restart(&block)
449
+ Puma.deprecate_method_change :on_restart, __callee__, __method__
450
+
451
+ process_hook :before_restart, nil, block
452
+ end
453
+
454
+ alias_method :on_restart, :before_restart
455
+
456
+ # Command to use to restart Puma. This should be just how to
457
+ # load Puma itself (ie. 'ruby -Ilib bin/puma'), not the arguments
458
+ # to Puma, as those are the same as the original process.
459
+ #
460
+ # @example
461
+ # restart_command '/u/app/lolcat/bin/restart_puma'
462
+ #
463
+ def restart_command(cmd)
464
+ @options[:restart_cmd] = cmd.to_s
465
+ end
466
+
467
+ # Store the pid of the server in the file at "path".
468
+ #
469
+ # @example
470
+ # pidfile '/u/apps/lolcat/tmp/pids/puma.pid'
471
+ #
472
+ def pidfile(path)
473
+ @options[:pidfile] = path.to_s
474
+ end
475
+
476
+ # Disable request logging, the inverse of `log_requests`.
477
+ #
478
+ # The default is +true+.
479
+ #
480
+ # @example
481
+ # quiet
482
+ #
483
+ def quiet(which=true)
484
+ @options[:log_requests] = !which
485
+ end
486
+
487
+ # Enable request logging, the inverse of `quiet`.
488
+ #
489
+ # The default is +false+.
490
+ #
491
+ # @example
492
+ # log_requests
493
+ #
494
+ def log_requests(which=true)
495
+ @options[:log_requests] = which
496
+ end
497
+
498
+ # Pass in a custom logging class instance
499
+ #
500
+ # @example
501
+ # custom_logger Logger.new('t.log')
502
+ #
503
+ def custom_logger(custom_logger)
504
+ @options[:custom_logger] = custom_logger
505
+ end
506
+
507
+ # Show debugging info
508
+ #
509
+ # The default is +false+.
510
+ #
511
+ # @example
512
+ # debug
513
+ #
514
+ def debug
515
+ @options[:debug] = true
516
+ end
517
+
518
+ # Load +path+ as a rackup file.
519
+ #
520
+ # The default is "config.ru".
521
+ #
522
+ # @example
523
+ # rackup '/u/apps/lolcat/config.ru'
524
+ #
525
+ def rackup(path)
526
+ @options[:rackup] ||= path.to_s
527
+ end
528
+
529
+ # Allows setting `env['rack.url_scheme']`.
530
+ # Only necessary if X-Forwarded-Proto is not being set by your proxy
531
+ # Normal values are 'http' or 'https'.
532
+ #
533
+ def rack_url_scheme(scheme=nil)
534
+ @options[:rack_url_scheme] = scheme
535
+ end
536
+
537
+ # Enable HTTP 103 Early Hints responses.
538
+ #
539
+ # The default is +nil+.
540
+ #
541
+ # @example
542
+ # early_hints
543
+ #
544
+ def early_hints(answer=true)
545
+ @options[:early_hints] = answer
546
+ end
547
+
548
+ # Redirect +STDOUT+ and +STDERR+ to files specified. The +append+ parameter
549
+ # specifies whether the output is appended.
550
+ #
551
+ # The default is +false+.
552
+ #
553
+ # @example
554
+ # stdout_redirect '/app/lolcat/log/stdout', '/app/lolcat/log/stderr'
555
+ # @example
556
+ # stdout_redirect '/app/lolcat/log/stdout', '/app/lolcat/log/stderr', true
557
+ #
558
+ def stdout_redirect(stdout=nil, stderr=nil, append=false)
559
+ @options[:redirect_stdout] = stdout
560
+ @options[:redirect_stderr] = stderr
561
+ @options[:redirect_append] = append
562
+ end
563
+
564
+ def log_formatter(&block)
565
+ @options[:log_formatter] = block
566
+ end
567
+
568
+ # Configure the number of threads to use to answer requests.
569
+ #
570
+ # It can be a single fixed number, or a +min+ and a +max+.
571
+ #
572
+ # The default is the environment variables +PUMA_MIN_THREADS+ / +PUMA_MAX_THREADS+
573
+ # (or +MIN_THREADS+ / +MAX_THREADS+ if the +PUMA_+ variables aren't set).
574
+ #
575
+ # If these environment variables aren't set, the default is "0, 5" in MRI or "0, 16" for other interpreters.
576
+ #
577
+ # @example
578
+ # threads 5
579
+ # @example
580
+ # threads 0, 16
581
+ # @example
582
+ # threads 5, 5
583
+ #
584
+ def threads(min, max = min)
585
+ min = Integer(min)
586
+ max = Integer(max)
587
+ if min > max
588
+ raise "The minimum (#{min}) number of threads must be less than or equal to the max (#{max})"
589
+ end
590
+
591
+ if max < 1
592
+ raise "The maximum number of threads (#{max}) must be greater than 0"
593
+ end
594
+
595
+ @options[:min_threads] = min
596
+ @options[:max_threads] = max
597
+ end
598
+
599
+ # Configure the max number of IO threads.
600
+ #
601
+ # When request handlers know the current requests will no longer use a significant amount
602
+ # of CPU, they can mark the current request as IO bound using <tt>env["puma.mark_as_io_bound"]</tt>.
603
+ #
604
+ # Threads marked as IO bound are allowed to go over the max thread limit.
605
+ #
606
+ # @example
607
+ # threads 5
608
+ # max_io_threads 5
609
+ #
610
+ # The above example allows for 5 regular threads and 5 IO threads to process requests concurrently.
611
+ # Any IO thread over the limit is counted as a regular thread, hence the above configuration also
612
+ # allows for 3 regular threads and 7 IO threads for example.
613
+ def max_io_threads(max)
614
+ max = Integer(max)
615
+ if max < 0
616
+ raise "The maximum number of IO threads (#{max}) must be a positive number"
617
+ end
618
+
619
+ @options[:max_io_threads] = max
620
+ end
621
+
622
+ # Instead of using +bind+ and manually constructing a URI like:
623
+ #
624
+ # bind 'ssl://127.0.0.1:9292?key=key_path&cert=cert_path'
625
+ #
626
+ # you can use the this method.
627
+ #
628
+ # When binding on localhost you don't need to specify +cert+ and +key+,
629
+ # Puma will assume you are using the +localhost+ gem and try to load the
630
+ # appropriate files.
631
+ #
632
+ # When using the options hash parameter, the `reuse:` value is either
633
+ # `true`, which sets reuse 'on' with default values, or a hash, with `:size`
634
+ # and/or `:timeout` keys, each with integer values.
635
+ #
636
+ # The `cert:` options hash parameter can be the path to a certificate
637
+ # file including all intermediate certificates in PEM format.
638
+ #
639
+ # The `cert_pem:` options hash parameter can be String containing the
640
+ # cerificate and all intermediate certificates in PEM format.
641
+ #
642
+ # @example
643
+ # ssl_bind '127.0.0.1', '9292', {
644
+ # cert: path_to_cert,
645
+ # key: path_to_key,
646
+ # ssl_cipher_filter: cipher_filter, # optional
647
+ # ssl_ciphersuites: ciphersuites, # optional
648
+ # verify_mode: verify_mode, # default 'none'
649
+ # verification_flags: flags, # optional, not supported by JRuby
650
+ # reuse: true # optional
651
+ # }
652
+ #
653
+ # @example Using self-signed certificate with the +localhost+ gem:
654
+ # ssl_bind '127.0.0.1', '9292'
655
+ #
656
+ # @example Alternatively, you can provide +cert_pem+ and +key_pem+:
657
+ # ssl_bind '127.0.0.1', '9292', {
658
+ # cert_pem: File.read(path_to_cert),
659
+ # key_pem: File.read(path_to_key),
660
+ # reuse: {size: 2_000, timeout: 20} # optional
661
+ # }
662
+ #
663
+ # @example For JRuby, two keys are required: +keystore+ & +keystore_pass+
664
+ # ssl_bind '127.0.0.1', '9292', {
665
+ # keystore: path_to_keystore,
666
+ # keystore_pass: password,
667
+ # ssl_cipher_list: cipher_list, # optional
668
+ # verify_mode: verify_mode # default 'none'
669
+ # }
670
+ #
671
+ def ssl_bind(host, port, opts = {})
672
+ add_pem_values_to_options_store(opts)
673
+ bind self.class.ssl_bind_str(host, port, opts)
674
+ end
675
+
676
+ # Use +path+ as the file to store the server info state. This is
677
+ # used by +pumactl+ to query and control the server.
678
+ #
679
+ # @example
680
+ # state_path '/u/apps/lolcat/tmp/pids/puma.state'
681
+ #
682
+ def state_path(path)
683
+ @options[:state] = path.to_s
684
+ end
685
+
686
+ # Use +permission+ to restrict permissions for the state file. By convention,
687
+ # +permission+ is an octal number (e.g. `0640` or `0o640`).
688
+ #
689
+ # @example
690
+ # state_permission 0600
691
+ #
692
+ def state_permission(permission)
693
+ @options[:state_permission] = permission
694
+ end
695
+
696
+ # How many worker processes to run. Typically this is set to the number of
697
+ # available cores.
698
+ #
699
+ # The default is the value of the environment variable +WEB_CONCURRENCY+ if
700
+ # set, otherwise 0. Passing +:auto+ will set the value to
701
+ # +Concurrent.available_processor_count+ (requires the concurrent-ruby gem).
702
+ # On some platforms (e.g. under CPU quotas) this may be fractional, and Puma
703
+ # will round down. If it rounds down to 0, Puma will run in single mode and
704
+ # cluster-only hooks like +before_worker_boot+ will not execute.
705
+ # If you rely on cluster-only hooks, set an explicit worker count.
706
+ #
707
+ # A value of 0 or nil means run in single mode.
708
+ #
709
+ # @example
710
+ # workers 2
711
+ # workers :auto
712
+ #
713
+ # @see Puma::Cluster
714
+ #
715
+ def workers(count)
716
+ @options[:workers] = count.nil? ? 0 : @config.send(:parse_workers, count)
717
+ end
718
+
719
+ # Disable warning message when running in cluster mode with a single worker.
720
+ #
721
+ # Cluster mode has some overhead of running an additional 'control' process
722
+ # in order to manage the cluster. If only running a single worker it is
723
+ # likely not worth paying that overhead vs running in single mode with
724
+ # additional threads instead.
725
+ #
726
+ # There are some scenarios where running cluster mode with a single worker
727
+ # may still be warranted and valid under certain deployment scenarios, see
728
+ # https://github.com/puma/puma/issues/2534
729
+ #
730
+ # Moving from workers = 1 to workers = 0 will save 10-30% of memory use.
731
+ #
732
+ # The default is +false+.
733
+ #
734
+ # @note Cluster mode only.
735
+ #
736
+ # @example
737
+ # silence_single_worker_warning
738
+ #
739
+ def silence_single_worker_warning
740
+ @options[:silence_single_worker_warning] = true
741
+ end
742
+
743
+ # Disable warning message when running single mode with callback hook defined.
744
+ #
745
+ # The default is +false+.
746
+ #
747
+ # @example
748
+ # silence_fork_callback_warning
749
+ #
750
+ def silence_fork_callback_warning
751
+ @options[:silence_fork_callback_warning] = true
752
+ end
753
+
754
+ # Code to run only in single mode.
755
+ # Runs after all config files are loaded.
756
+ #
757
+ # This can be called multiple times.
758
+ #
759
+ # @note Single mode only.
760
+ #
761
+ # @example
762
+ # single do
763
+ # silence_fork_callback_warning
764
+ # end
765
+ #
766
+ def single(&block)
767
+ raise ArgumentError, "A block must be provided to `single`" unless block
768
+
769
+ @options[:single] ||= []
770
+ @options[:single] << block
771
+ end
772
+
773
+ # Code to run only in cluster mode.
774
+ # Runs after all config files are loaded.
775
+ #
776
+ # This can be called multiple times.
777
+ #
778
+ # @note Cluster mode only.
779
+ #
780
+ # @example
781
+ # cluster do
782
+ # prune_bundler
783
+ # end
784
+ #
785
+ def cluster(&block)
786
+ raise ArgumentError, "A block must be provided to `cluster`" unless block
787
+
788
+ @options[:cluster] ||= []
789
+ @options[:cluster] << block
790
+ end
791
+
792
+ # Code to run immediately before master process
793
+ # forks workers (once on boot). These hooks can block if necessary
794
+ # to wait for background operations unknown to Puma to finish before
795
+ # the process terminates.
796
+ # This can be used to close any connections to remote servers (database,
797
+ # Redis, ...) that were opened when preloading the code.
798
+ #
799
+ # This can be called multiple times to add several hooks.
800
+ #
801
+ # @note Cluster mode only.
802
+ #
803
+ # @example
804
+ # before_fork do
805
+ # puts "Starting workers..."
806
+ # end
807
+ #
808
+ def before_fork(&block)
809
+ process_hook :before_fork, nil, block, cluster_only: true
810
+ end
811
+
812
+ # Code to run in a worker when it boots to setup
813
+ # the process before booting the app.
814
+ #
815
+ # This can be called multiple times to add several hooks.
816
+ #
817
+ # @note Cluster mode only.
818
+ #
819
+ # @example
820
+ # before_worker_boot do
821
+ # puts 'Before worker boot...'
822
+ # end
823
+ #
824
+ def before_worker_boot(key = nil, &block)
825
+ Puma.deprecate_method_change :on_worker_boot, __callee__, __method__
826
+
827
+ process_hook :before_worker_boot, key, block, cluster_only: true
828
+ end
829
+
830
+ alias_method :on_worker_boot, :before_worker_boot
831
+
832
+ # Code to run immediately before a worker shuts
833
+ # down (after it has finished processing HTTP requests). The worker's
834
+ # index is passed as an argument. These hooks
835
+ # can block if necessary to wait for background operations unknown
836
+ # to Puma to finish before the process terminates.
837
+ #
838
+ # This can be called multiple times to add several hooks.
839
+ #
840
+ # @note Cluster mode only.
841
+ #
842
+ # @example
843
+ # before_worker_shutdown do
844
+ # puts 'On worker shutdown...'
845
+ # end
846
+ #
847
+ def before_worker_shutdown(key = nil, &block)
848
+ Puma.deprecate_method_change :on_worker_shutdown, __callee__, __method__
849
+
850
+ process_hook :before_worker_shutdown, key, block, cluster_only: true
851
+ end
852
+
853
+ alias_method :on_worker_shutdown, :before_worker_shutdown
854
+
855
+ # Code to run in the master right before a worker is started. The worker's
856
+ # index is passed as an argument.
857
+ #
858
+ # This can be called multiple times to add several hooks.
859
+ #
860
+ # @note Cluster mode only.
861
+ #
862
+ # @example
863
+ # before_worker_fork do
864
+ # puts 'Before worker fork...'
865
+ # end
866
+ #
867
+ def before_worker_fork(&block)
868
+ Puma.deprecate_method_change :on_worker_fork, __callee__, __method__
869
+
870
+ process_hook :before_worker_fork, nil, block, cluster_only: true
871
+ end
872
+
873
+ alias_method :on_worker_fork, :before_worker_fork
874
+
875
+ # Code to run in the master after a worker has been started. The worker's
876
+ # index is passed as an argument.
877
+ #
878
+ # This is called everytime a worker is to be started.
879
+ #
880
+ # @note Cluster mode only.
881
+ #
882
+ # @example
883
+ # after_worker_fork do
884
+ # puts 'After worker fork...'
885
+ # end
886
+ #
887
+ def after_worker_fork(&block)
888
+ process_hook :after_worker_fork, nil, block, cluster_only: true
889
+ end
890
+
891
+ alias_method :after_worker_boot, :after_worker_fork
892
+
893
+ # Code to run in the master right after a worker has stopped. The worker's
894
+ # index and Process::Status are passed as arguments.
895
+ #
896
+ # @note Cluster mode only.
897
+ #
898
+ # @example
899
+ # after_worker_shutdown do |worker_handle|
900
+ # puts 'Worker crashed' unless worker_handle.process_status.success?
901
+ # end
902
+ #
903
+ def after_worker_shutdown(&block)
904
+ process_hook :after_worker_shutdown, nil, block, cluster_only: true
905
+ end
906
+
907
+ # Code to run after puma is booted (works for both single and cluster modes).
908
+ #
909
+ # @example
910
+ # after_booted do
911
+ # puts 'After booting...'
912
+ # end
913
+ #
914
+ def after_booted(&block)
915
+ Puma.deprecate_method_change :on_booted, __callee__, __method__
916
+
917
+ @config.events.after_booted(&block)
918
+ end
919
+
920
+ alias_method :on_booted, :after_booted
921
+
922
+ # Code to run after puma is stopped (works for both: single and clustered)
923
+ #
924
+ # @example
925
+ # after_stopped do
926
+ # puts 'After stopping...'
927
+ # end
928
+ #
929
+ def after_stopped(&block)
930
+ Puma.deprecate_method_change :on_stopped, __callee__, __method__
931
+
932
+ @config.events.after_stopped(&block)
933
+ end
934
+ alias_method :on_stopped, :after_stopped
935
+
936
+ # When `fork_worker` is enabled, code to run in Worker 0
937
+ # before all other workers are re-forked from this process,
938
+ # after the server has temporarily stopped serving requests
939
+ # (once per complete refork cycle).
940
+ #
941
+ # This can be used to trigger extra garbage-collection to maximize
942
+ # copy-on-write efficiency, or close any connections to remote servers
943
+ # (database, Redis, ...) that were opened while the server was running.
944
+ #
945
+ # This can be called multiple times to add several hooks.
946
+ #
947
+ # @note Cluster mode with `fork_worker` enabled only.
948
+ #
949
+ # @example
950
+ # before_refork do
951
+ # 3.times {GC.start}
952
+ # end
953
+ #
954
+ # @version 5.0.0
955
+ #
956
+ def before_refork(key = nil, &block)
957
+ Puma.deprecate_method_change :on_refork, __callee__, __method__
958
+
959
+ process_hook :before_refork, key, block, cluster_only: true
960
+ end
961
+
962
+ alias_method :on_refork, :before_refork
963
+
964
+ # When `fork_worker` is enabled, code to run in Worker 0
965
+ # after all other workers are re-forked from this process,
966
+ # after the server has temporarily stopped serving requests
967
+ # (once per complete refork cycle).
968
+ #
969
+ # This can be used to re-open any connections to remote servers
970
+ # (database, Redis, ...) that were closed via before_refork.
971
+ #
972
+ # This can be called multiple times to add several hooks.
973
+ #
974
+ # @note Cluster mode with `fork_worker` enabled only.
975
+ #
976
+ # @example
977
+ # after_refork do
978
+ # puts 'After refork...'
979
+ # end
980
+ #
981
+ def after_refork(key = nil, &block)
982
+ process_hook :after_refork, key, block
983
+ end
984
+
985
+ # Provide a block to be executed just before a thread is added to the thread
986
+ # pool. Be careful: while the block executes, thread creation is delayed, and
987
+ # probably a request will have to wait too! The new thread will not be added to
988
+ # the threadpool until the provided block returns.
989
+ #
990
+ # Return values are ignored.
991
+ # Raising an exception will log a warning.
992
+ #
993
+ # This hook is useful for doing something when the thread pool grows.
994
+ #
995
+ # This can be called multiple times to add several hooks.
996
+ #
997
+ # @example
998
+ # before_thread_start do
999
+ # puts 'On thread start...'
1000
+ # end
1001
+ #
1002
+ def before_thread_start(&block)
1003
+ Puma.deprecate_method_change :on_thread_start, __callee__, __method__
1004
+
1005
+ process_hook :before_thread_start, nil, block
1006
+ end
1007
+
1008
+ alias_method :on_thread_start, :before_thread_start
1009
+
1010
+ # Provide a block to be executed after a thread is trimmed from the thread
1011
+ # pool. Be careful: while this block executes, Puma's main loop is
1012
+ # blocked, so no new requests will be picked up.
1013
+ #
1014
+ # This hook only runs when a thread in the threadpool is trimmed by Puma.
1015
+ # It does not run when a thread dies due to exceptions or any other cause.
1016
+ #
1017
+ # Return values are ignored.
1018
+ # Raising an exception will log a warning.
1019
+ #
1020
+ # This hook is useful for cleaning up thread local resources when a thread
1021
+ # is trimmed.
1022
+ #
1023
+ # This can be called multiple times to add several hooks.
1024
+ #
1025
+ # @example
1026
+ # before_thread_exit do
1027
+ # puts 'On thread exit...'
1028
+ # end
1029
+ #
1030
+ def before_thread_exit(&block)
1031
+ Puma.deprecate_method_change :on_thread_exit, __callee__, __method__
1032
+
1033
+ process_hook :before_thread_exit, nil, block
1034
+ end
1035
+
1036
+ alias_method :on_thread_exit, :before_thread_exit
1037
+
1038
+ # Code to run out-of-band when the worker is idle.
1039
+ # These hooks run immediately after a request has finished
1040
+ # processing and there are no busy threads on the worker.
1041
+ # The worker doesn't accept new requests until this code finishes.
1042
+ #
1043
+ # This hook is useful for running out-of-band garbage collection
1044
+ # or scheduling asynchronous tasks to execute after a response.
1045
+ #
1046
+ # This can be called multiple times to add several hooks.
1047
+ #
1048
+ def out_of_band(&block)
1049
+ process_hook :out_of_band, nil, block
1050
+ end
1051
+
1052
+ # The directory to operate out of.
1053
+ #
1054
+ # The default is the current directory.
1055
+ #
1056
+ # @example
1057
+ # directory '/u/apps/lolcat'
1058
+ #
1059
+ def directory(dir)
1060
+ @options[:directory] = dir.to_s
1061
+ end
1062
+
1063
+ # Preload the application before forking the workers; this conflicts with
1064
+ # the phased restart feature.
1065
+ #
1066
+ # The default is +true+ if your app uses more than 1 worker.
1067
+ #
1068
+ # @note Cluster mode only.
1069
+ # @note When using `fork_worker`, this only applies to worker 0.
1070
+ #
1071
+ # @example
1072
+ # preload_app!
1073
+ #
1074
+ def preload_app!(answer=true)
1075
+ @options[:preload_app] = answer
1076
+ end
1077
+
1078
+ # Use +obj+ or +block+ as the low level error handler. This allows the
1079
+ # configuration file to change the default error on the server.
1080
+ #
1081
+ # @example
1082
+ # lowlevel_error_handler do |err|
1083
+ # [200, {}, ["error page"]]
1084
+ # end
1085
+ #
1086
+ def lowlevel_error_handler(obj=nil, &block)
1087
+ obj ||= block
1088
+ raise "Provide either a #call'able or a block" unless obj
1089
+ @options[:lowlevel_error_handler] = obj
1090
+ end
1091
+
1092
+ # This option is used to allow your app and its gems to be
1093
+ # properly reloaded when not using preload.
1094
+ #
1095
+ # When set, if Puma detects that it's been invoked in the
1096
+ # context of Bundler, it will cleanup the environment and
1097
+ # re-run itself outside the Bundler environment, but directly
1098
+ # using the files that Bundler has setup.
1099
+ #
1100
+ # This means that Puma is now decoupled from your Bundler
1101
+ # context and when each worker loads, it will be loading a
1102
+ # new Bundler context and thus can float around as the release
1103
+ # dictates.
1104
+ #
1105
+ # @note Cluster mode only.
1106
+ # @note This is incompatible with +preload_app!+.
1107
+ # @note This is only supported for RubyGems 2.2+
1108
+ #
1109
+ # @see extra_runtime_dependencies
1110
+ #
1111
+ def prune_bundler(answer=true)
1112
+ @options[:prune_bundler] = answer
1113
+ end
1114
+
1115
+ # Raises a SignalException when SIGTERM is received. In environments where
1116
+ # SIGTERM is something expected, you can suppress these with this option.
1117
+ #
1118
+ # This can be useful for example in Kubernetes, where rolling restart is
1119
+ # guaranteed usually on the infrastructure level.
1120
+ #
1121
+ # The default is +true+.
1122
+ #
1123
+ # @example
1124
+ # raise_exception_on_sigterm false
1125
+ #
1126
+ # @see Puma::Launcher#setup_signals
1127
+ # @see Puma::Cluster#setup_signals
1128
+ #
1129
+ def raise_exception_on_sigterm(answer=true)
1130
+ @options[:raise_exception_on_sigterm] = answer
1131
+ end
1132
+
1133
+ # When using prune_bundler, if extra runtime dependencies need to be loaded to
1134
+ # initialize your app, then this setting can be used. This includes any Puma plugins.
1135
+ #
1136
+ # Before bundler is pruned, the gem names supplied will be looked up in the bundler
1137
+ # context and then loaded again after bundler is pruned.
1138
+ # Only applies if prune_bundler is used.
1139
+ #
1140
+ # @example
1141
+ # extra_runtime_dependencies ['gem_name_1', 'gem_name_2']
1142
+ # @example
1143
+ # extra_runtime_dependencies ['puma_worker_killer', 'puma-heroku']
1144
+ #
1145
+ # @see Puma::Launcher#extra_runtime_deps_directories
1146
+ #
1147
+ def extra_runtime_dependencies(answer = [])
1148
+ @options[:extra_runtime_dependencies] = Array(answer)
1149
+ end
1150
+
1151
+ # Additional text to display in process listing.
1152
+ #
1153
+ # If you do not specify a tag, Puma will infer it. If you do not want Puma
1154
+ # to add a tag, use an empty string.
1155
+ #
1156
+ # The default is the current file or directory base name.
1157
+ #
1158
+ # @example
1159
+ # tag 'app name'
1160
+ # @example
1161
+ # tag ''
1162
+ #
1163
+ def tag(string)
1164
+ @options[:tag] = string.to_s
1165
+ end
1166
+
1167
+ # Change the default interval for checking workers.
1168
+ #
1169
+ # The default is 5 seconds.
1170
+ #
1171
+ # @note Cluster mode only.
1172
+ #
1173
+ # @example
1174
+ # worker_check_interval 10
1175
+ #
1176
+ # @see Puma::Cluster#check_workers
1177
+ #
1178
+ def worker_check_interval(interval)
1179
+ @options[:worker_check_interval] = Integer(interval)
1180
+ end
1181
+
1182
+ # Verifies that all workers have checked in to the master process within
1183
+ # the given timeout. If not the worker process will be restarted. This is
1184
+ # not a request timeout, it is to protect against a hung or dead process.
1185
+ # Setting this value will not protect against slow requests.
1186
+ #
1187
+ # This value must be greater than worker_check_interval.
1188
+ #
1189
+ # The default is 60 seconds.
1190
+ #
1191
+ # @note Cluster mode only.
1192
+ #
1193
+ # @example
1194
+ # worker_timeout 60
1195
+ #
1196
+ # @see Puma::Cluster::Worker#ping_timeout
1197
+ #
1198
+ def worker_timeout(timeout)
1199
+ timeout = Integer(timeout)
1200
+ min = @options.fetch(:worker_check_interval, Configuration::DEFAULTS[:worker_check_interval])
1201
+
1202
+ if timeout <= min
1203
+ raise "The minimum worker_timeout must be greater than the worker reporting interval (#{min})"
1204
+ end
1205
+
1206
+ @options[:worker_timeout] = timeout
1207
+ end
1208
+
1209
+ # Change the default worker timeout for booting.
1210
+ #
1211
+ # The default is 60 seconds.
1212
+ #
1213
+ # @note Cluster mode only.
1214
+ #
1215
+ # @example
1216
+ # worker_boot_timeout 60
1217
+ #
1218
+ # @see Puma::Cluster::Worker#ping_timeout
1219
+ #
1220
+ def worker_boot_timeout(timeout)
1221
+ @options[:worker_boot_timeout] = Integer(timeout)
1222
+ end
1223
+
1224
+ # Set the timeout for worker shutdown.
1225
+ #
1226
+ # The default is 30 seconds.
1227
+ #
1228
+ # @note Cluster mode only.
1229
+ #
1230
+ # @example
1231
+ # worker_shutdown_timeout 90
1232
+ #
1233
+ # @see Puma::Cluster::Worker#term
1234
+ #
1235
+ def worker_shutdown_timeout(timeout)
1236
+ @options[:worker_shutdown_timeout] = Integer(timeout)
1237
+ end
1238
+
1239
+ # Set the strategy for worker culling.
1240
+ #
1241
+ # There are two possible values:
1242
+ #
1243
+ # 1. **:youngest** - the youngest workers (i.e. the workers that were
1244
+ # the most recently started) will be culled.
1245
+ # 2. **:oldest** - the oldest workers (i.e. the workers that were started
1246
+ # the longest time ago) will be culled.
1247
+ #
1248
+ # The default is +:youngest+.
1249
+ #
1250
+ # @note Cluster mode only.
1251
+ #
1252
+ # @example
1253
+ # worker_culling_strategy :oldest
1254
+ #
1255
+ # @see Puma::Cluster#cull_workers
1256
+ #
1257
+ def worker_culling_strategy(strategy)
1258
+ strategy = strategy.to_sym
1259
+
1260
+ if ![:youngest, :oldest].include?(strategy)
1261
+ raise "Invalid value for worker_culling_strategy - #{strategy}"
1262
+ end
1263
+
1264
+ @options[:worker_culling_strategy] = strategy
1265
+ end
1266
+
1267
+ # When set to true, workers accept all requests
1268
+ # and queue them before passing them to the handlers.
1269
+ # When set to false, each worker process accepts exactly as
1270
+ # many requests as it is configured to simultaneously handle.
1271
+ #
1272
+ # Queueing requests generally improves performance. In some
1273
+ # cases, such as a single threaded application, it may be
1274
+ # better to ensure requests get balanced across workers.
1275
+ #
1276
+ # Note that setting this to false disables HTTP keepalive and
1277
+ # slow clients will occupy a handler thread while the request
1278
+ # is being sent. A reverse proxy, such as nginx, can handle
1279
+ # slow clients and queue requests before they reach Puma.
1280
+ #
1281
+ # The default is +true+.
1282
+ #
1283
+ # @see Puma::Server
1284
+ #
1285
+ def queue_requests(answer=true)
1286
+ @options[:queue_requests] = answer
1287
+ end
1288
+
1289
+ # When a shutdown is requested, the backtraces of all the
1290
+ # threads will be written to $stdout. This can help figure
1291
+ # out why shutdown is hanging.
1292
+ #
1293
+ # If `on_force` is true, the backtraces will be written only
1294
+ # when the shutdown is forced i.e. not graceful.
1295
+ #
1296
+ # @see force_shutdown_after
1297
+ def shutdown_debug(val = true, on_force: false)
1298
+ @options[:shutdown_debug] = val && on_force ? :on_force : val
1299
+ end
1300
+
1301
+ # Maximum delay of worker accept loop.
1302
+ #
1303
+ # Attempts to route traffic to less-busy workers by causing a busy worker to delay
1304
+ # listening on the socket, allowing workers which are not processing as many
1305
+ # requests to pick up new requests first.
1306
+ #
1307
+ # The default is 0.005 seconds.
1308
+ #
1309
+ # To turn off this feature, set the value to 0.
1310
+ #
1311
+ # @note Cluster mode with >= 2 workers only.
1312
+ #
1313
+ # @note Interpreters with forking support only.
1314
+ #
1315
+ # @see Puma::Server#handle_servers
1316
+ # @see Puma::ThreadPool#wait_for_less_busy_worker
1317
+ #
1318
+ def wait_for_less_busy_worker(val=0.005)
1319
+ @options[:wait_for_less_busy_worker] = val.to_f
1320
+ end
1321
+
1322
+ # Control how the remote address of the connection is set. This
1323
+ # is configurable because to calculate the true socket peer address
1324
+ # a kernel syscall is required which for very fast rack handlers
1325
+ # slows down the handling significantly.
1326
+ #
1327
+ # There are 5 possible values:
1328
+ #
1329
+ # 1. **:socket** - read the peername from the socket using the
1330
+ # syscall. This is the normal behavior. If this fails for any reason (e.g.,
1331
+ # if the peer disconnects between the connection being accepted and the getpeername
1332
+ # system call), Puma will return "0.0.0.0"
1333
+ # 2. **:localhost** - set the remote address to "127.0.0.1"
1334
+ # 3. **header: <http_header>**- set the remote address to the value of the
1335
+ # provided http header. For instance:
1336
+ # `set_remote_address header: "X-Real-IP"`.
1337
+ # Only the first word (as separated by spaces or comma) is used, allowing
1338
+ # headers such as X-Forwarded-For to be used as well. If this header is absent,
1339
+ # Puma will fall back to the behavior of :socket
1340
+ # 4. **proxy_protocol: :v1**- set the remote address to the value read from the
1341
+ # HAproxy PROXY protocol, version 1. If the request does not have the PROXY
1342
+ # protocol attached to it, will fall back to :socket
1343
+ # 5. **\<Any string\>** - this allows you to hardcode remote address to any value
1344
+ # you wish. Because Puma never uses this field anyway, it's format is
1345
+ # entirely in your hands.
1346
+ #
1347
+ # The default is +:socket+.
1348
+ #
1349
+ # @example
1350
+ # set_remote_address :localhost
1351
+ #
1352
+ def set_remote_address(val=:socket)
1353
+ case val
1354
+ when :socket
1355
+ @options[:remote_address] = val
1356
+ when :localhost
1357
+ @options[:remote_address] = :value
1358
+ @options[:remote_address_value] = "127.0.0.1".freeze
1359
+ when String
1360
+ @options[:remote_address] = :value
1361
+ @options[:remote_address_value] = val
1362
+ when Hash
1363
+ if hdr = val[:header]
1364
+ @options[:remote_address] = :header
1365
+ @options[:remote_address_header] = "HTTP_" + hdr.upcase.tr("-", "_")
1366
+ elsif protocol_version = val[:proxy_protocol]
1367
+ @options[:remote_address] = :proxy_protocol
1368
+ protocol_version = protocol_version.downcase.to_sym
1369
+ unless [:v1].include?(protocol_version)
1370
+ raise "Invalid value for proxy_protocol - #{protocol_version.inspect}"
1371
+ end
1372
+ @options[:remote_address_proxy_protocol] = protocol_version
1373
+ else
1374
+ raise "Invalid value for set_remote_address - #{val.inspect}"
1375
+ end
1376
+ else
1377
+ raise "Invalid value for set_remote_address - #{val}"
1378
+ end
1379
+ end
1380
+
1381
+ # When enabled, workers will be forked from worker 0 instead of from the master process.
1382
+ # This option is similar to `preload_app` because the app is preloaded before forking,
1383
+ # but it is compatible with phased restart.
1384
+ #
1385
+ # This option also enables the `refork` command (SIGURG), which optimizes copy-on-write performance
1386
+ # in a running app.
1387
+ #
1388
+ # A refork will automatically trigger once after the specified number of requests
1389
+ # (default 1000), or pass 0 to disable auto refork.
1390
+ #
1391
+ # @note This is experimental.
1392
+ # @note Cluster mode only.
1393
+ #
1394
+ def fork_worker(after_requests=1000)
1395
+ @options[:fork_worker] = Integer(after_requests)
1396
+ end
1397
+
1398
+ # @deprecated Use {#max_keep_alive} instead.
1399
+ #
1400
+ def max_fast_inline(num_of_requests)
1401
+ Puma.deprecate_method_change :max_fast_inline, __method__, :max_keep_alive
1402
+ @options[:max_keep_alive] ||= Float(num_of_requests) unless num_of_requests.nil?
1403
+ end
1404
+
1405
+ # The number of requests a keep-alive client can submit before being closed.
1406
+ # Note that some applications (server to server) may benefit from a very high
1407
+ # number or Float::INFINITY.
1408
+ #
1409
+ # The default is 999.
1410
+ #
1411
+ # @example
1412
+ # max_keep_alive 20
1413
+ #
1414
+ def max_keep_alive(num_of_requests)
1415
+ @options[:max_keep_alive] = Float(num_of_requests) unless num_of_requests.nil?
1416
+ end
1417
+
1418
+ # When `true`, keep-alive connections are maintained on inbound requests.
1419
+ # Enabling this setting reduces the number of TCP operations, reducing response
1420
+ # times for connections that can send multiple requests in a single connection.
1421
+ #
1422
+ # When Puma receives more incoming connections than available Puma threads,
1423
+ # enabling the keep-alive behavior may result in processing requests out-of-order,
1424
+ # increasing overall response time variance. Increased response time variance
1425
+ # means that the overall average of response times might not change, but more
1426
+ # outliers will exist. Those long-tail outliers may significantly affect response
1427
+ # times for some processed requests.
1428
+ #
1429
+ # When `false`, Puma closes the connection after each request, requiring the
1430
+ # client to open a new request. Disabling this setting guarantees that requests
1431
+ # will be processed in the order they are fully received, decreasing response
1432
+ # variance and eliminating long-tail outliers caused by keep-alive behavior.
1433
+ # The trade-off is that the number of TCP operations required will increase.
1434
+ #
1435
+ # The default is +true+.
1436
+ #
1437
+ # @example
1438
+ # enable_keep_alives false
1439
+ #
1440
+ def enable_keep_alives(enabled=true)
1441
+ @options[:enable_keep_alives] = enabled
1442
+ end
1443
+
1444
+ # Specify the backend for the IO selector.
1445
+ #
1446
+ # Provided values will be passed directly to +NIO::Selector.new+, with the
1447
+ # exception of +:auto+ which will let nio4r choose the backend.
1448
+ #
1449
+ # Check the documentation of +NIO::Selector.backends+ for the list of valid
1450
+ # options. Note that the available options on your system will depend on the
1451
+ # operating system. If you want to use the pure Ruby backend (not
1452
+ # recommended due to its comparatively low performance), set environment
1453
+ # variable +NIO4R_PURE+ to +true+.
1454
+ #
1455
+ # The default is +:auto+.
1456
+ #
1457
+ # @see https://github.com/socketry/nio4r/blob/main/lib/nio/selector.rb
1458
+ #
1459
+ def io_selector_backend(backend)
1460
+ @options[:io_selector_backend] = backend.to_sym
1461
+ end
1462
+
1463
+ # Ensures +STDOUT+ and +STDERR+ is immediately flushed to the underlying
1464
+ # operating system and is not buffered internally
1465
+ #
1466
+ # The default is +true+.
1467
+ #
1468
+ # @example
1469
+ # mutate_stdout_and_stderr_to_sync_on_write false
1470
+ #
1471
+ def mutate_stdout_and_stderr_to_sync_on_write(enabled=true)
1472
+ @options[:mutate_stdout_and_stderr_to_sync_on_write] = enabled
1473
+ end
1474
+
1475
+ # Specify how big the request payload should be, in bytes.
1476
+ # This limit is compared against Content-Length HTTP header.
1477
+ # If the payload size (CONTENT_LENGTH) is larger than http_content_length_limit,
1478
+ # HTTP 413 status code is returned.
1479
+ #
1480
+ # When no Content-Length http header is present, it is compared against the
1481
+ # size of the body of the request.
1482
+ #
1483
+ # The default is +nil+.
1484
+ #
1485
+ # @example
1486
+ # http_content_length_limit 2_000_000_000
1487
+ #
1488
+ def http_content_length_limit(limit)
1489
+ @options[:http_content_length_limit] = limit
1490
+ end
1491
+
1492
+ # Supported http methods, which will replace `Puma::Const::SUPPORTED_HTTP_METHODS`.
1493
+ # The value of `:any` will allows all methods, otherwise, the value must be
1494
+ # an array of strings. Note that methods are all uppercase.
1495
+ #
1496
+ # `Puma::Const::SUPPORTED_HTTP_METHODS` is conservative, if you want a
1497
+ # complete set of methods, the methods defined by the
1498
+ # [IANA Method Registry](https://www.iana.org/assignments/http-methods/http-methods.xhtml)
1499
+ # are pre-defined as the constant `Puma::Const::IANA_HTTP_METHODS`.
1500
+ #
1501
+ # @note If the `methods` value is `:any`, no method check with be performed,
1502
+ # similar to Puma v5 and earlier.
1503
+ #
1504
+ # @example Adds 'PROPFIND' to existing supported methods
1505
+ # supported_http_methods(Puma::Const::SUPPORTED_HTTP_METHODS + ['PROPFIND'])
1506
+ # @example Restricts methods to the array elements
1507
+ # supported_http_methods %w[HEAD GET POST PUT DELETE OPTIONS PROPFIND]
1508
+ # @example Restricts methods to the methods in the IANA Registry
1509
+ # supported_http_methods Puma::Const::IANA_HTTP_METHODS
1510
+ # @example Allows any method
1511
+ # supported_http_methods :any
1512
+ #
1513
+ def supported_http_methods(methods)
1514
+ if methods == :any
1515
+ @options[:supported_http_methods] = :any
1516
+ elsif Array === methods && methods == (ary = methods.grep(String).uniq) &&
1517
+ !ary.empty?
1518
+ @options[:supported_http_methods] = ary
1519
+ else
1520
+ raise "supported_http_methods must be ':any' or a unique array of strings"
1521
+ end
1522
+ end
1523
+
1524
+ private
1525
+
1526
+ # To avoid adding cert_pem and key_pem as URI params, we store them on the
1527
+ # options[:store] from where Puma binder knows how to find and extract them.
1528
+ #
1529
+ def add_pem_values_to_options_store(opts)
1530
+ return if defined?(JRUBY_VERSION)
1531
+
1532
+ @options[:store] ||= []
1533
+
1534
+ # Store cert_pem and key_pem to options[:store] if present
1535
+ [:cert, :key].each do |v|
1536
+ opt_key = :"#{v}_pem"
1537
+ if opts[opt_key]
1538
+ index = @options[:store].length
1539
+ @options[:store] << opts[opt_key]
1540
+ opts[v] = "store:#{index}"
1541
+ end
1542
+ end
1543
+ end
1544
+
1545
+ def process_hook(options_key, key, block, cluster_only: false)
1546
+ raise ArgumentError, "expected #{options_key} to be given a block" unless block
1547
+
1548
+ @config.hooks[options_key] = true
1549
+
1550
+ @options[options_key] ||= []
1551
+ hook_options = { block: block, cluster_only: cluster_only }
1552
+ hook_options[:id] = if ON_WORKER_KEY.include?(key.class)
1553
+ key.to_sym
1554
+ elsif key.nil?
1555
+ nil
1556
+ else
1557
+ raise "'#{options_key}' key must be String or Symbol"
1558
+ end
1559
+ @options[options_key] << hook_options
1560
+ end
1561
+ end
1562
+ end