preplay_unicorn 4.3.1.2.gc450

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. data/.CHANGELOG.old +25 -0
  2. data/.document +29 -0
  3. data/.gitignore +24 -0
  4. data/.mailmap +26 -0
  5. data/.manifest +258 -0
  6. data/.wrongdoc.yml +10 -0
  7. data/Application_Timeouts +77 -0
  8. data/CONTRIBUTORS +35 -0
  9. data/COPYING +674 -0
  10. data/ChangeLog +3839 -0
  11. data/DESIGN +97 -0
  12. data/Documentation/.gitignore +5 -0
  13. data/Documentation/GNUmakefile +30 -0
  14. data/Documentation/unicorn.1.txt +174 -0
  15. data/Documentation/unicorn_rails.1.txt +175 -0
  16. data/FAQ +53 -0
  17. data/GIT-VERSION-FILE +1 -0
  18. data/GIT-VERSION-GEN +40 -0
  19. data/GNUmakefile +294 -0
  20. data/HACKING +134 -0
  21. data/ISSUES +36 -0
  22. data/KNOWN_ISSUES +79 -0
  23. data/LATEST +15 -0
  24. data/LICENSE +64 -0
  25. data/Links +56 -0
  26. data/NEWS +1886 -0
  27. data/PHILOSOPHY +145 -0
  28. data/README +149 -0
  29. data/Rakefile +97 -0
  30. data/SIGNALS +114 -0
  31. data/Sandbox +96 -0
  32. data/TODO +5 -0
  33. data/TUNING +98 -0
  34. data/bin/unicorn +121 -0
  35. data/bin/unicorn_rails +209 -0
  36. data/examples/big_app_gc.rb +2 -0
  37. data/examples/echo.ru +27 -0
  38. data/examples/git.ru +13 -0
  39. data/examples/init.sh +74 -0
  40. data/examples/logger_mp_safe.rb +25 -0
  41. data/examples/logrotate.conf +29 -0
  42. data/examples/nginx.conf +156 -0
  43. data/examples/unicorn.conf.minimal.rb +13 -0
  44. data/examples/unicorn.conf.rb +94 -0
  45. data/ext/unicorn_http/CFLAGS +13 -0
  46. data/ext/unicorn_http/c_util.h +124 -0
  47. data/ext/unicorn_http/common_field_optimization.h +111 -0
  48. data/ext/unicorn_http/ext_help.h +86 -0
  49. data/ext/unicorn_http/extconf.rb +10 -0
  50. data/ext/unicorn_http/global_variables.h +97 -0
  51. data/ext/unicorn_http/httpdate.c +82 -0
  52. data/ext/unicorn_http/unicorn_http.c +4031 -0
  53. data/ext/unicorn_http/unicorn_http.rl +1036 -0
  54. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  55. data/lib/unicorn/app/exec_cgi.rb +154 -0
  56. data/lib/unicorn/app/inetd.rb +109 -0
  57. data/lib/unicorn/app/old_rails/static.rb +59 -0
  58. data/lib/unicorn/app/old_rails.rb +35 -0
  59. data/lib/unicorn/cgi_wrapper.rb +147 -0
  60. data/lib/unicorn/configurator.rb +641 -0
  61. data/lib/unicorn/const.rb +40 -0
  62. data/lib/unicorn/http_request.rb +77 -0
  63. data/lib/unicorn/http_response.rb +45 -0
  64. data/lib/unicorn/http_server.rb +748 -0
  65. data/lib/unicorn/launcher.rb +62 -0
  66. data/lib/unicorn/oob_gc.rb +71 -0
  67. data/lib/unicorn/preread_input.rb +33 -0
  68. data/lib/unicorn/socket_helper.rb +208 -0
  69. data/lib/unicorn/ssl_client.rb +11 -0
  70. data/lib/unicorn/ssl_configurator.rb +104 -0
  71. data/lib/unicorn/ssl_server.rb +42 -0
  72. data/lib/unicorn/stream_input.rb +149 -0
  73. data/lib/unicorn/tee_input.rb +126 -0
  74. data/lib/unicorn/tmpio.rb +29 -0
  75. data/lib/unicorn/util.rb +68 -0
  76. data/lib/unicorn/worker.rb +88 -0
  77. data/lib/unicorn.rb +107 -0
  78. data/local.mk.sample +59 -0
  79. data/man/man1/unicorn.1 +201 -0
  80. data/man/man1/unicorn_rails.1 +208 -0
  81. data/preplay_unicorn.gemspec +44 -0
  82. data/script/isolate_for_tests +50 -0
  83. data/setup.rb +1586 -0
  84. data/t/.gitignore +5 -0
  85. data/t/GNUmakefile +82 -0
  86. data/t/README +42 -0
  87. data/t/bin/content-md5-put +36 -0
  88. data/t/bin/sha1sum.rb +17 -0
  89. data/t/bin/unused_listen +40 -0
  90. data/t/bin/utee +12 -0
  91. data/t/broken-app.ru +12 -0
  92. data/t/detach.ru +11 -0
  93. data/t/env.ru +3 -0
  94. data/t/heartbeat-timeout.ru +12 -0
  95. data/t/my-tap-lib.sh +201 -0
  96. data/t/oob_gc.ru +21 -0
  97. data/t/oob_gc_path.ru +21 -0
  98. data/t/pid.ru +3 -0
  99. data/t/preread_input.ru +17 -0
  100. data/t/rack-input-tests.ru +21 -0
  101. data/t/rails3-app/.gitignore +4 -0
  102. data/t/rails3-app/Gemfile +26 -0
  103. data/t/rails3-app/Rakefile +10 -0
  104. data/t/rails3-app/app/controllers/application_controller.rb +4 -0
  105. data/t/rails3-app/app/helpers/application_helper.rb +2 -0
  106. data/t/rails3-app/app/views/layouts/application.html.erb +14 -0
  107. data/t/rails3-app/config/application.rb +46 -0
  108. data/t/rails3-app/config/boot.rb +6 -0
  109. data/t/rails3-app/config/database.yml +22 -0
  110. data/t/rails3-app/config/environment.rb +5 -0
  111. data/t/rails3-app/config/environments/development.rb +19 -0
  112. data/t/rails3-app/config/environments/production.rb +42 -0
  113. data/t/rails3-app/config/environments/test.rb +32 -0
  114. data/t/rails3-app/config/initializers/backtrace_silencers.rb +7 -0
  115. data/t/rails3-app/config/initializers/inflections.rb +10 -0
  116. data/t/rails3-app/config/initializers/mime_types.rb +5 -0
  117. data/t/rails3-app/config/initializers/secret_token.rb +7 -0
  118. data/t/rails3-app/config/initializers/session_store.rb +8 -0
  119. data/t/rails3-app/config/locales/en.yml +5 -0
  120. data/t/rails3-app/config/routes.rb +58 -0
  121. data/t/rails3-app/config.ru +4 -0
  122. data/t/rails3-app/db/seeds.rb +7 -0
  123. data/t/rails3-app/doc/README_FOR_APP +2 -0
  124. data/t/rails3-app/lib/tasks/.gitkeep +0 -0
  125. data/t/rails3-app/public/404.html +1 -0
  126. data/t/rails3-app/public/500.html +1 -0
  127. data/t/rails3-app/public/x.txt +1 -0
  128. data/t/rails3-app/script/rails +9 -0
  129. data/t/rails3-app/test/performance/browsing_test.rb +9 -0
  130. data/t/rails3-app/test/test_helper.rb +13 -0
  131. data/t/rails3-app/vendor/plugins/.gitkeep +0 -0
  132. data/t/sslgen.sh +71 -0
  133. data/t/t0000-http-basic.sh +50 -0
  134. data/t/t0001-reload-bad-config.sh +53 -0
  135. data/t/t0002-config-conflict.sh +49 -0
  136. data/t/t0002-parser-error.sh +94 -0
  137. data/t/t0003-working_directory.sh +51 -0
  138. data/t/t0004-heartbeat-timeout.sh +69 -0
  139. data/t/t0004-working_directory_broken.sh +24 -0
  140. data/t/t0005-working_directory_app.rb.sh +37 -0
  141. data/t/t0006-reopen-logs.sh +83 -0
  142. data/t/t0006.ru +13 -0
  143. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  144. data/t/t0008-back_out_of_upgrade.sh +110 -0
  145. data/t/t0009-broken-app.sh +56 -0
  146. data/t/t0009-winch_ttin.sh +59 -0
  147. data/t/t0010-reap-logging.sh +55 -0
  148. data/t/t0011-active-unix-socket.sh +79 -0
  149. data/t/t0012-reload-empty-config.sh +85 -0
  150. data/t/t0013-rewindable-input-false.sh +24 -0
  151. data/t/t0013.ru +12 -0
  152. data/t/t0014-rewindable-input-true.sh +24 -0
  153. data/t/t0014.ru +12 -0
  154. data/t/t0015-configurator-internals.sh +25 -0
  155. data/t/t0016-trust-x-forwarded-false.sh +30 -0
  156. data/t/t0017-trust-x-forwarded-true.sh +30 -0
  157. data/t/t0018-write-on-close.sh +23 -0
  158. data/t/t0019-max_header_len.sh +49 -0
  159. data/t/t0020-at_exit-handler.sh +49 -0
  160. data/t/t0021-process_detach.sh +29 -0
  161. data/t/t0100-rack-input-tests.sh +124 -0
  162. data/t/t0116-client_body_buffer_size.sh +80 -0
  163. data/t/t0116.ru +16 -0
  164. data/t/t0300-rails3-basic.sh +28 -0
  165. data/t/t0301-rails3-missing-config-ru.sh +33 -0
  166. data/t/t0302-rails3-alt-working_directory.sh +32 -0
  167. data/t/t0303-rails3-alt-working_directory_config.ru.sh +56 -0
  168. data/t/t0304-rails3-alt-working_directory_no_embed_cli.sh +52 -0
  169. data/t/t0600-https-server-basic.sh +48 -0
  170. data/t/t9000-preread-input.sh +48 -0
  171. data/t/t9001-oob_gc.sh +47 -0
  172. data/t/t9002-oob_gc-path.sh +75 -0
  173. data/t/test-lib.sh +113 -0
  174. data/t/test-rails3.sh +27 -0
  175. data/t/write-on-close.ru +11 -0
  176. data/test/aggregate.rb +15 -0
  177. data/test/benchmark/README +50 -0
  178. data/test/benchmark/dd.ru +18 -0
  179. data/test/benchmark/stack.ru +8 -0
  180. data/test/exec/README +5 -0
  181. data/test/exec/test_exec.rb +1055 -0
  182. data/test/rails/app-1.2.3/.gitignore +2 -0
  183. data/test/rails/app-1.2.3/Rakefile +7 -0
  184. data/test/rails/app-1.2.3/app/controllers/application.rb +6 -0
  185. data/test/rails/app-1.2.3/app/controllers/foo_controller.rb +36 -0
  186. data/test/rails/app-1.2.3/app/helpers/application_helper.rb +4 -0
  187. data/test/rails/app-1.2.3/config/boot.rb +11 -0
  188. data/test/rails/app-1.2.3/config/database.yml +12 -0
  189. data/test/rails/app-1.2.3/config/environment.rb +13 -0
  190. data/test/rails/app-1.2.3/config/environments/development.rb +9 -0
  191. data/test/rails/app-1.2.3/config/environments/production.rb +5 -0
  192. data/test/rails/app-1.2.3/config/routes.rb +6 -0
  193. data/test/rails/app-1.2.3/db/.gitignore +0 -0
  194. data/test/rails/app-1.2.3/log/.gitignore +1 -0
  195. data/test/rails/app-1.2.3/public/404.html +1 -0
  196. data/test/rails/app-1.2.3/public/500.html +1 -0
  197. data/test/rails/app-2.0.2/.gitignore +2 -0
  198. data/test/rails/app-2.0.2/Rakefile +7 -0
  199. data/test/rails/app-2.0.2/app/controllers/application.rb +4 -0
  200. data/test/rails/app-2.0.2/app/controllers/foo_controller.rb +36 -0
  201. data/test/rails/app-2.0.2/app/helpers/application_helper.rb +4 -0
  202. data/test/rails/app-2.0.2/config/boot.rb +11 -0
  203. data/test/rails/app-2.0.2/config/database.yml +12 -0
  204. data/test/rails/app-2.0.2/config/environment.rb +17 -0
  205. data/test/rails/app-2.0.2/config/environments/development.rb +8 -0
  206. data/test/rails/app-2.0.2/config/environments/production.rb +5 -0
  207. data/test/rails/app-2.0.2/config/routes.rb +6 -0
  208. data/test/rails/app-2.0.2/db/.gitignore +0 -0
  209. data/test/rails/app-2.0.2/log/.gitignore +1 -0
  210. data/test/rails/app-2.0.2/public/404.html +1 -0
  211. data/test/rails/app-2.0.2/public/500.html +1 -0
  212. data/test/rails/app-2.1.2/.gitignore +2 -0
  213. data/test/rails/app-2.1.2/Rakefile +7 -0
  214. data/test/rails/app-2.1.2/app/controllers/application.rb +4 -0
  215. data/test/rails/app-2.1.2/app/controllers/foo_controller.rb +36 -0
  216. data/test/rails/app-2.1.2/app/helpers/application_helper.rb +4 -0
  217. data/test/rails/app-2.1.2/config/boot.rb +111 -0
  218. data/test/rails/app-2.1.2/config/database.yml +12 -0
  219. data/test/rails/app-2.1.2/config/environment.rb +17 -0
  220. data/test/rails/app-2.1.2/config/environments/development.rb +7 -0
  221. data/test/rails/app-2.1.2/config/environments/production.rb +5 -0
  222. data/test/rails/app-2.1.2/config/routes.rb +6 -0
  223. data/test/rails/app-2.1.2/db/.gitignore +0 -0
  224. data/test/rails/app-2.1.2/log/.gitignore +1 -0
  225. data/test/rails/app-2.1.2/public/404.html +1 -0
  226. data/test/rails/app-2.1.2/public/500.html +1 -0
  227. data/test/rails/app-2.2.2/.gitignore +2 -0
  228. data/test/rails/app-2.2.2/Rakefile +7 -0
  229. data/test/rails/app-2.2.2/app/controllers/application.rb +4 -0
  230. data/test/rails/app-2.2.2/app/controllers/foo_controller.rb +36 -0
  231. data/test/rails/app-2.2.2/app/helpers/application_helper.rb +4 -0
  232. data/test/rails/app-2.2.2/config/boot.rb +111 -0
  233. data/test/rails/app-2.2.2/config/database.yml +12 -0
  234. data/test/rails/app-2.2.2/config/environment.rb +17 -0
  235. data/test/rails/app-2.2.2/config/environments/development.rb +7 -0
  236. data/test/rails/app-2.2.2/config/environments/production.rb +5 -0
  237. data/test/rails/app-2.2.2/config/routes.rb +6 -0
  238. data/test/rails/app-2.2.2/db/.gitignore +0 -0
  239. data/test/rails/app-2.2.2/log/.gitignore +1 -0
  240. data/test/rails/app-2.2.2/public/404.html +1 -0
  241. data/test/rails/app-2.2.2/public/500.html +1 -0
  242. data/test/rails/test_rails.rb +287 -0
  243. data/test/test_helper.rb +300 -0
  244. data/test/unit/test_configurator.rb +158 -0
  245. data/test/unit/test_droplet.rb +28 -0
  246. data/test/unit/test_http_parser.rb +860 -0
  247. data/test/unit/test_http_parser_ng.rb +716 -0
  248. data/test/unit/test_http_parser_xftrust.rb +38 -0
  249. data/test/unit/test_request.rb +197 -0
  250. data/test/unit/test_response.rb +99 -0
  251. data/test/unit/test_server.rb +289 -0
  252. data/test/unit/test_signals.rb +207 -0
  253. data/test/unit/test_sni_hostnames.rb +47 -0
  254. data/test/unit/test_socket_helper.rb +192 -0
  255. data/test/unit/test_stream_input.rb +204 -0
  256. data/test/unit/test_tee_input.rb +296 -0
  257. data/test/unit/test_upload.rb +306 -0
  258. data/test/unit/test_util.rb +100 -0
  259. metadata +428 -0
@@ -0,0 +1,641 @@
1
+ # -*- encoding: binary -*-
2
+ require 'logger'
3
+ require 'unicorn/ssl_configurator'
4
+
5
+ # Implements a simple DSL for configuring a \Unicorn server.
6
+ #
7
+ # See http://unicorn.bogomips.org/examples/unicorn.conf.rb and
8
+ # http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
9
+ # example configuration files. An example config file for use with
10
+ # nginx is also available at
11
+ # http://unicorn.bogomips.org/examples/nginx.conf
12
+ #
13
+ # See the link:/TUNING.html document for more information on tuning unicorn.
14
+ class Unicorn::Configurator
15
+ include Unicorn
16
+ include Unicorn::SSLConfigurator
17
+
18
+ # :stopdoc:
19
+ attr_accessor :set, :config_file, :after_reload
20
+
21
+ # used to stash stuff for deferred processing of cli options in
22
+ # config.ru after "working_directory" is bound. Do not rely on
23
+ # this being around later on...
24
+ RACKUP = {
25
+ :daemonize => false,
26
+ :host => Unicorn::Const::DEFAULT_HOST,
27
+ :port => Unicorn::Const::DEFAULT_PORT,
28
+ :set_listener => false,
29
+ :options => { :listeners => [] }
30
+ }
31
+
32
+ # Default settings for Unicorn
33
+ DEFAULTS = {
34
+ :timeout => 60,
35
+ :logger => Logger.new($stderr),
36
+ :worker_processes => 1,
37
+ :after_fork => lambda { |server, worker|
38
+ server.logger.info("worker=#{worker.nr} spawned pid=#{$$}")
39
+ },
40
+ :before_fork => lambda { |server, worker|
41
+ server.logger.info("worker=#{worker.nr} spawning...")
42
+ },
43
+ :after_worker_ready => lambda { |server|
44
+ server.logger.info("worker is ready...")
45
+ },
46
+ :before_exec => lambda { |server|
47
+ server.logger.info("forked child re-executing...")
48
+ },
49
+ :pid => nil,
50
+ :preload_app => false,
51
+ :rewindable_input => true, # for Rack 2.x: (Rack::VERSION[0] <= 1),
52
+ :client_body_buffer_size => Unicorn::Const::MAX_BODY,
53
+ :trust_x_forwarded => true,
54
+ }
55
+ #:startdoc:
56
+
57
+ def initialize(defaults = {}) #:nodoc:
58
+ self.set = Hash.new(:unset)
59
+ @use_defaults = defaults.delete(:use_defaults)
60
+ self.config_file = defaults.delete(:config_file)
61
+
62
+ # after_reload is only used by unicorn_rails, unsupported otherwise
63
+ self.after_reload = defaults.delete(:after_reload)
64
+
65
+ set.merge!(DEFAULTS) if @use_defaults
66
+ defaults.each { |key, value| self.__send__(key, value) }
67
+ Hash === set[:listener_opts] or
68
+ set[:listener_opts] = Hash.new { |hash,key| hash[key] = {} }
69
+ Array === set[:listeners] or set[:listeners] = []
70
+ reload(false)
71
+ end
72
+
73
+ def reload(merge_defaults = true) #:nodoc:
74
+ if merge_defaults && @use_defaults
75
+ set.merge!(DEFAULTS) if @use_defaults
76
+ end
77
+ instance_eval(File.read(config_file), config_file) if config_file
78
+
79
+ parse_rackup_file
80
+
81
+ RACKUP[:set_listener] and
82
+ set[:listeners] << "#{RACKUP[:host]}:#{RACKUP[:port]}"
83
+
84
+ # unicorn_rails creates dirs here after working_directory is bound
85
+ after_reload.call if after_reload
86
+
87
+ # working_directory binds immediately (easier error checking that way),
88
+ # now ensure any paths we changed are correctly set.
89
+ [ :pid, :stderr_path, :stdout_path ].each do |var|
90
+ String === (path = set[var]) or next
91
+ path = File.expand_path(path)
92
+ File.writable?(path) || File.writable?(File.dirname(path)) or \
93
+ raise ArgumentError, "directory for #{var}=#{path} not writable"
94
+ end
95
+ end
96
+
97
+ def commit!(server, options = {}) #:nodoc:
98
+ skip = options[:skip] || []
99
+ if ready_pipe = RACKUP.delete(:ready_pipe)
100
+ server.ready_pipe = ready_pipe
101
+ end
102
+ set.each do |key, value|
103
+ value == :unset and next
104
+ skip.include?(key) and next
105
+ server.__send__("#{key}=", value)
106
+ end
107
+ end
108
+
109
+ def [](key) # :nodoc:
110
+ set[key]
111
+ end
112
+
113
+ # sets object to the +obj+ Logger-like object. The new Logger-like
114
+ # object must respond to the following methods:
115
+ # * debug
116
+ # * info
117
+ # * warn
118
+ # * error
119
+ # * fatal
120
+ # The default Logger will log its output to the path specified
121
+ # by +stderr_path+. If you're running Unicorn daemonized, then
122
+ # you must specify a path to prevent error messages from going
123
+ # to /dev/null.
124
+ def logger(obj)
125
+ %w(debug info warn error fatal).each do |m|
126
+ obj.respond_to?(m) and next
127
+ raise ArgumentError, "logger=#{obj} does not respond to method=#{m}"
128
+ end
129
+
130
+ set[:logger] = obj
131
+ end
132
+
133
+ # sets after_fork hook to a given block. This block will be called by
134
+ # the worker after forking. The following is an example hook which adds
135
+ # a per-process listener to every worker:
136
+ #
137
+ # after_fork do |server,worker|
138
+ # # per-process listener ports for debugging/admin:
139
+ # addr = "127.0.0.1:#{9293 + worker.nr}"
140
+ #
141
+ # # the negative :tries parameter indicates we will retry forever
142
+ # # waiting on the existing process to exit with a 5 second :delay
143
+ # # Existing options for Unicorn::Configurator#listen such as
144
+ # # :backlog, :rcvbuf, :sndbuf are available here as well.
145
+ # server.listen(addr, :tries => -1, :delay => 5, :backlog => 128)
146
+ # end
147
+ def after_fork(*args, &block)
148
+ set_hook(:after_fork, block_given? ? block : args[0])
149
+ end
150
+
151
+ # sets before_fork got be a given Proc object. This Proc
152
+ # object will be called by the master process before forking
153
+ # each worker.
154
+ def before_fork(*args, &block)
155
+ set_hook(:before_fork, block_given? ? block : args[0])
156
+ end
157
+
158
+ # sets after_worker_ready got be a given Proc object. This Proc
159
+ # object will be called by the master process after a worker
160
+ # has booted the app (useful when preload_app is set to false).
161
+ def after_worker_ready(*args, &block)
162
+ set_hook(:after_worker_ready, block_given? ? block : args[0], 1)
163
+ end
164
+
165
+
166
+ # sets the before_exec hook to a given Proc object. This
167
+ # Proc object will be called by the master process right
168
+ # before exec()-ing the new unicorn binary. This is useful
169
+ # for freeing certain OS resources that you do NOT wish to
170
+ # share with the reexeced child process.
171
+ # There is no corresponding after_exec hook (for obvious reasons).
172
+ def before_exec(*args, &block)
173
+ set_hook(:before_exec, block_given? ? block : args[0], 1)
174
+ end
175
+
176
+ # sets the timeout of worker processes to +seconds+. Workers
177
+ # handling the request/app.call/response cycle taking longer than
178
+ # this time period will be forcibly killed (via SIGKILL). This
179
+ # timeout is enforced by the master process itself and not subject
180
+ # to the scheduling limitations by the worker process. Due the
181
+ # low-complexity, low-overhead implementation, timeouts of less
182
+ # than 3.0 seconds can be considered inaccurate and unsafe.
183
+ #
184
+ # For running Unicorn behind nginx, it is recommended to set
185
+ # "fail_timeout=0" for in your nginx configuration like this
186
+ # to have nginx always retry backends that may have had workers
187
+ # SIGKILL-ed due to timeouts.
188
+ #
189
+ # # See http://wiki.nginx.org/NginxHttpUpstreamModule for more details
190
+ # # on nginx upstream configuration:
191
+ # upstream unicorn_backend {
192
+ # # for UNIX domain socket setups:
193
+ # server unix:/path/to/unicorn.sock fail_timeout=0;
194
+ #
195
+ # # for TCP setups
196
+ # server 192.168.0.7:8080 fail_timeout=0;
197
+ # server 192.168.0.8:8080 fail_timeout=0;
198
+ # server 192.168.0.9:8080 fail_timeout=0;
199
+ # }
200
+ def timeout(seconds)
201
+ set_int(:timeout, seconds, 3)
202
+ # POSIX says 31 days is the smallest allowed maximum timeout for select()
203
+ max = 30 * 60 * 60 * 24
204
+ set[:timeout] = seconds > max ? max : seconds
205
+ end
206
+
207
+ # sets the current number of worker_processes to +nr+. Each worker
208
+ # process will serve exactly one client at a time. You can
209
+ # increment or decrement this value at runtime by sending SIGTTIN
210
+ # or SIGTTOU respectively to the master process without reloading
211
+ # the rest of your Unicorn configuration. See the SIGNALS document
212
+ # for more information.
213
+ def worker_processes(nr)
214
+ set_int(:worker_processes, nr, 1)
215
+ end
216
+
217
+ # sets listeners to the given +addresses+, replacing or augmenting the
218
+ # current set. This is for the global listener pool shared by all
219
+ # worker processes. For per-worker listeners, see the after_fork example
220
+ # This is for internal API use only, do not use it in your Unicorn
221
+ # config file. Use listen instead.
222
+ def listeners(addresses) # :nodoc:
223
+ Array === addresses or addresses = Array(addresses)
224
+ addresses.map! { |addr| expand_addr(addr) }
225
+ set[:listeners] = addresses
226
+ end
227
+
228
+ # Adds an +address+ to the existing listener set. May be specified more
229
+ # than once. +address+ may be an Integer port number for a TCP port, an
230
+ # "IP_ADDRESS:PORT" for TCP listeners or a pathname for UNIX domain sockets.
231
+ #
232
+ # listen 3000 # listen to port 3000 on all TCP interfaces
233
+ # listen "127.0.0.1:3000" # listen to port 3000 on the loopback interface
234
+ # listen "/tmp/.unicorn.sock" # listen on the given Unix domain socket
235
+ # listen "[::1]:3000" # listen to port 3000 on the IPv6 loopback interface
236
+ #
237
+ # The following options may be specified (but are generally not needed):
238
+ #
239
+ # [:backlog => number of clients]
240
+ #
241
+ # This is the backlog of the listen() syscall.
242
+ #
243
+ # Some operating systems allow negative values here to specify the
244
+ # maximum allowable value. In most cases, this number is only
245
+ # recommendation and there are other OS-specific tunables and
246
+ # variables that can affect this number. See the listen(2)
247
+ # syscall documentation of your OS for the exact semantics of
248
+ # this.
249
+ #
250
+ # If you are running unicorn on multiple machines, lowering this number
251
+ # can help your load balancer detect when a machine is overloaded
252
+ # and give requests to a different machine.
253
+ #
254
+ # Default: 1024
255
+ #
256
+ # [:rcvbuf => bytes, :sndbuf => bytes]
257
+ #
258
+ # Maximum receive and send buffer sizes (in bytes) of sockets.
259
+ #
260
+ # These correspond to the SO_RCVBUF and SO_SNDBUF settings which
261
+ # can be set via the setsockopt(2) syscall. Some kernels
262
+ # (e.g. Linux 2.4+) have intelligent auto-tuning mechanisms and
263
+ # there is no need (and it is sometimes detrimental) to specify them.
264
+ #
265
+ # See the socket API documentation of your operating system
266
+ # to determine the exact semantics of these settings and
267
+ # other operating system-specific knobs where they can be
268
+ # specified.
269
+ #
270
+ # Defaults: operating system defaults
271
+ #
272
+ # [:tcp_nodelay => true or false]
273
+ #
274
+ # Disables Nagle's algorithm on TCP sockets if +true+.
275
+ #
276
+ # Setting this to +true+ can make streaming responses in Rails 3.1
277
+ # appear more quickly at the cost of slightly higher bandwidth usage.
278
+ # The effect of this option is most visible if nginx is not used,
279
+ # but nginx remains highly recommended with \Unicorn.
280
+ #
281
+ # This has no effect on UNIX sockets.
282
+ #
283
+ # Default: +true+ (Nagle's algorithm disabled) in \Unicorn,
284
+ # +true+ in Rainbows! This defaulted to +false+ in \Unicorn
285
+ # 3.x
286
+ #
287
+ # [:tcp_nopush => true or false]
288
+ #
289
+ # Enables/disables TCP_CORK in Linux or TCP_NOPUSH in FreeBSD
290
+ #
291
+ # This prevents partial TCP frames from being sent out and reduces
292
+ # wakeups in nginx if it is on a different machine. Since \Unicorn
293
+ # is only designed for applications that send the response body
294
+ # quickly without keepalive, sockets will always be flushed on close
295
+ # to prevent delays.
296
+ #
297
+ # This has no effect on UNIX sockets.
298
+ #
299
+ # Default: +false+
300
+ # This defaulted to +true+ in \Unicorn 3.4 - 3.7
301
+ #
302
+ # [:ipv6only => true or false]
303
+ #
304
+ # This option makes IPv6-capable TCP listeners IPv6-only and unable
305
+ # to receive IPv4 queries on dual-stack systems. A separate IPv4-only
306
+ # listener is required if this is true.
307
+ #
308
+ # This option is only available for Ruby 1.9.2 and later.
309
+ #
310
+ # Enabling this option for the IPv6-only listener and having a
311
+ # separate IPv4 listener is recommended if you wish to support IPv6
312
+ # on the same TCP port. Otherwise, the value of \env[\"REMOTE_ADDR\"]
313
+ # will appear as an ugly IPv4-mapped-IPv6 address for IPv4 clients
314
+ # (e.g ":ffff:10.0.0.1" instead of just "10.0.0.1").
315
+ #
316
+ # Default: Operating-system dependent
317
+ #
318
+ # [:tries => Integer]
319
+ #
320
+ # Times to retry binding a socket if it is already in use
321
+ #
322
+ # A negative number indicates we will retry indefinitely, this is
323
+ # useful for migrations and upgrades when individual workers
324
+ # are binding to different ports.
325
+ #
326
+ # Default: 5
327
+ #
328
+ # [:delay => seconds]
329
+ #
330
+ # Seconds to wait between successive +tries+
331
+ #
332
+ # Default: 0.5 seconds
333
+ #
334
+ # [:umask => mode]
335
+ #
336
+ # Sets the file mode creation mask for UNIX sockets. If specified,
337
+ # this is usually in octal notation.
338
+ #
339
+ # Typically UNIX domain sockets are created with more liberal
340
+ # file permissions than the rest of the application. By default,
341
+ # we create UNIX domain sockets to be readable and writable by
342
+ # all local users to give them the same accessibility as
343
+ # locally-bound TCP listeners.
344
+ #
345
+ # This has no effect on TCP listeners.
346
+ #
347
+ # Default: 0000 (world-read/writable)
348
+ #
349
+ # [:tcp_defer_accept => Integer]
350
+ #
351
+ # Defer accept() until data is ready (Linux-only)
352
+ #
353
+ # For Linux 2.6.32 and later, this is the number of retransmits to
354
+ # defer an accept() for if no data arrives, but the client will
355
+ # eventually be accepted after the specified number of retransmits
356
+ # regardless of whether data is ready.
357
+ #
358
+ # For Linux before 2.6.32, this is a boolean option, and
359
+ # accepts are _always_ deferred indefinitely if no data arrives.
360
+ # This is similar to <code>:accept_filter => "dataready"</code>
361
+ # under FreeBSD.
362
+ #
363
+ # Specifying +true+ is synonymous for the default value(s) below,
364
+ # and +false+ or +nil+ is synonymous for a value of zero.
365
+ #
366
+ # A value of +1+ is a good optimization for local networks
367
+ # and trusted clients. For Rainbows! and Zbatery users, a higher
368
+ # value (e.g. +60+) provides more protection against some
369
+ # denial-of-service attacks. There is no good reason to ever
370
+ # disable this with a +zero+ value when serving HTTP.
371
+ #
372
+ # Default: 1 retransmit for \Unicorn, 60 for Rainbows! 0.95.0\+
373
+ #
374
+ # [:accept_filter => String]
375
+ #
376
+ # defer accept() until data is ready (FreeBSD-only)
377
+ #
378
+ # This enables either the "dataready" or (default) "httpready"
379
+ # accept() filter under FreeBSD. This is intended as an
380
+ # optimization to reduce context switches with common GET/HEAD
381
+ # requests. For Rainbows! and Zbatery users, this provides
382
+ # some protection against certain denial-of-service attacks, too.
383
+ #
384
+ # There is no good reason to change from the default.
385
+ #
386
+ # Default: "httpready"
387
+ def listen(address, options = {})
388
+ address = expand_addr(address)
389
+ if String === address
390
+ [ :umask, :backlog, :sndbuf, :rcvbuf, :tries ].each do |key|
391
+ value = options[key] or next
392
+ Integer === value or
393
+ raise ArgumentError, "not an integer: #{key}=#{value.inspect}"
394
+ end
395
+ [ :tcp_nodelay, :tcp_nopush, :ipv6only ].each do |key|
396
+ (value = options[key]).nil? and next
397
+ TrueClass === value || FalseClass === value or
398
+ raise ArgumentError, "not boolean: #{key}=#{value.inspect}"
399
+ end
400
+ unless (value = options[:delay]).nil?
401
+ Numeric === value or
402
+ raise ArgumentError, "not numeric: delay=#{value.inspect}"
403
+ end
404
+ set[:listener_opts][address].merge!(options)
405
+ end
406
+
407
+ set[:listeners] << address
408
+ end
409
+
410
+ # sets the +path+ for the PID file of the unicorn master process
411
+ def pid(path); set_path(:pid, path); end
412
+
413
+ # Enabling this preloads an application before forking worker
414
+ # processes. This allows memory savings when using a
415
+ # copy-on-write-friendly GC but can cause bad things to happen when
416
+ # resources like sockets are opened at load time by the master
417
+ # process and shared by multiple children. People enabling this are
418
+ # highly encouraged to look at the before_fork/after_fork hooks to
419
+ # properly close/reopen sockets. Files opened for logging do not
420
+ # have to be reopened as (unbuffered-in-userspace) files opened with
421
+ # the File::APPEND flag are written to atomically on UNIX.
422
+ #
423
+ # In addition to reloading the unicorn-specific config settings,
424
+ # SIGHUP will reload application code in the working
425
+ # directory/symlink when workers are gracefully restarted when
426
+ # preload_app=false (the default). As reloading the application
427
+ # sometimes requires RubyGems updates, +Gem.refresh+ is always
428
+ # called before the application is loaded (for RubyGems users).
429
+ #
430
+ # During deployments, care should _always_ be taken to ensure your
431
+ # applications are properly deployed and running. Using
432
+ # preload_app=false (the default) means you _must_ check if
433
+ # your application is responding properly after a deployment.
434
+ # Improperly deployed applications can go into a spawn loop
435
+ # if the application fails to load. While your children are
436
+ # in a spawn loop, it is is possible to fix an application
437
+ # by properly deploying all required code and dependencies.
438
+ # Using preload_app=true means any application load error will
439
+ # cause the master process to exit with an error.
440
+
441
+ def preload_app(bool)
442
+ set_bool(:preload_app, bool)
443
+ end
444
+
445
+ # Toggles making \env[\"rack.input\"] rewindable.
446
+ # Disabling rewindability can improve performance by lowering
447
+ # I/O and memory usage for applications that accept uploads.
448
+ # Keep in mind that the Rack 1.x spec requires
449
+ # \env[\"rack.input\"] to be rewindable, so this allows
450
+ # intentionally violating the current Rack 1.x spec.
451
+ #
452
+ # +rewindable_input+ defaults to +true+ when used with Rack 1.x for
453
+ # Rack conformance. When Rack 2.x is finalized, this will most
454
+ # likely default to +false+ while still conforming to the newer
455
+ # (less demanding) spec.
456
+ def rewindable_input(bool)
457
+ set_bool(:rewindable_input, bool)
458
+ end
459
+
460
+ # The maximum size (in +bytes+) to buffer in memory before
461
+ # resorting to a temporary file. Default is 112 kilobytes.
462
+ # This option has no effect if "rewindable_input" is set to
463
+ # +false+.
464
+ def client_body_buffer_size(bytes)
465
+ set_int(:client_body_buffer_size, bytes, 0)
466
+ end
467
+
468
+ # Allow redirecting $stderr to a given path. Unlike doing this from
469
+ # the shell, this allows the unicorn process to know the path its
470
+ # writing to and rotate the file if it is used for logging. The
471
+ # file will be opened with the File::APPEND flag and writes
472
+ # synchronized to the kernel (but not necessarily to _disk_) so
473
+ # multiple processes can safely append to it.
474
+ #
475
+ # If you are daemonizing and using the default +logger+, it is important
476
+ # to specify this as errors will otherwise be lost to /dev/null.
477
+ # Some applications/libraries may also triggering warnings that go to
478
+ # stderr, and they will end up here.
479
+ def stderr_path(path)
480
+ set_path(:stderr_path, path)
481
+ end
482
+
483
+ # Same as stderr_path, except for $stdout. Not many Rack applications
484
+ # write to $stdout, but any that do will have their output written here.
485
+ # It is safe to point this to the same location a stderr_path.
486
+ # Like stderr_path, this defaults to /dev/null when daemonized.
487
+ def stdout_path(path)
488
+ set_path(:stdout_path, path)
489
+ end
490
+
491
+ # sets the working directory for Unicorn. This ensures SIGUSR2 will
492
+ # start a new instance of Unicorn in this directory. This may be
493
+ # a symlink, a common scenario for Capistrano users. Unlike
494
+ # all other Unicorn configuration directives, this binds immediately
495
+ # for error checking and cannot be undone by unsetting it in the
496
+ # configuration file and reloading.
497
+ def working_directory(path)
498
+ # just let chdir raise errors
499
+ path = File.expand_path(path)
500
+ if config_file &&
501
+ config_file[0] != ?/ &&
502
+ ! File.readable?("#{path}/#{config_file}")
503
+ raise ArgumentError,
504
+ "config_file=#{config_file} would not be accessible in" \
505
+ " working_directory=#{path}"
506
+ end
507
+ Dir.chdir(path)
508
+ Unicorn::HttpServer::START_CTX[:cwd] = ENV["PWD"] = path
509
+ end
510
+
511
+ # Runs worker processes as the specified +user+ and +group+.
512
+ # The master process always stays running as the user who started it.
513
+ # This switch will occur after calling the after_fork hook, and only
514
+ # if the Worker#user method is not called in the after_fork hook
515
+ # +group+ is optional and will not change if unspecified.
516
+ def user(user, group = nil)
517
+ # raises ArgumentError on invalid user/group
518
+ Etc.getpwnam(user)
519
+ Etc.getgrnam(group) if group
520
+ set[:user] = [ user, group ]
521
+ end
522
+
523
+ # Sets whether or not the parser will trust X-Forwarded-Proto and
524
+ # X-Forwarded-SSL headers and set "rack.url_scheme" to "https" accordingly.
525
+ # Rainbows!/Zbatery installations facing untrusted clients directly
526
+ # should set this to +false+. This is +true+ by default as Unicorn
527
+ # is designed to only sit behind trusted nginx proxies.
528
+ #
529
+ # This has never been publically documented and is subject to removal
530
+ # in future releases.
531
+ def trust_x_forwarded(bool) # :nodoc:
532
+ set_bool(:trust_x_forwarded, bool)
533
+ end
534
+
535
+ # expands "unix:path/to/foo" to a socket relative to the current path
536
+ # expands pathnames of sockets if relative to "~" or "~username"
537
+ # expands "*:port and ":port" to "0.0.0.0:port"
538
+ def expand_addr(address) #:nodoc:
539
+ return "0.0.0.0:#{address}" if Integer === address
540
+ return address unless String === address
541
+
542
+ case address
543
+ when %r{\Aunix:(.*)\z}
544
+ File.expand_path($1)
545
+ when %r{\A~}
546
+ File.expand_path(address)
547
+ when %r{\A(?:\*:)?(\d+)\z}
548
+ "0.0.0.0:#$1"
549
+ when %r{\A\[([a-fA-F0-9:]+)\]\z}, %r/\A((?:\d+\.){3}\d+)\z/
550
+ canonicalize_tcp($1, 80)
551
+ when %r{\A\[([a-fA-F0-9:]+)\]:(\d+)\z}, %r{\A(.*):(\d+)\z}
552
+ canonicalize_tcp($1, $2.to_i)
553
+ else
554
+ address
555
+ end
556
+ end
557
+
558
+ private
559
+ def set_int(var, n, min) #:nodoc:
560
+ Integer === n or raise ArgumentError, "not an integer: #{var}=#{n.inspect}"
561
+ n >= min or raise ArgumentError, "too low (< #{min}): #{var}=#{n.inspect}"
562
+ set[var] = n
563
+ end
564
+
565
+ def canonicalize_tcp(addr, port)
566
+ packed = Socket.pack_sockaddr_in(port, addr)
567
+ port, addr = Socket.unpack_sockaddr_in(packed)
568
+ /:/ =~ addr ? "[#{addr}]:#{port}" : "#{addr}:#{port}"
569
+ end
570
+
571
+ def set_path(var, path) #:nodoc:
572
+ case path
573
+ when NilClass, String
574
+ set[var] = path
575
+ else
576
+ raise ArgumentError
577
+ end
578
+ end
579
+
580
+ def check_bool(var, bool) # :nodoc:
581
+ case bool
582
+ when true, false
583
+ return bool
584
+ end
585
+ raise ArgumentError, "#{var}=#{bool.inspect} not a boolean"
586
+ end
587
+
588
+ def set_bool(var, bool) #:nodoc:
589
+ set[var] = check_bool(var, bool)
590
+ end
591
+
592
+ def set_hook(var, my_proc, req_arity = 2) #:nodoc:
593
+ case my_proc
594
+ when Proc
595
+ arity = my_proc.arity
596
+ (arity == req_arity) or \
597
+ raise ArgumentError,
598
+ "#{var}=#{my_proc.inspect} has invalid arity: " \
599
+ "#{arity} (need #{req_arity})"
600
+ when NilClass
601
+ my_proc = DEFAULTS[var]
602
+ else
603
+ raise ArgumentError, "invalid type: #{var}=#{my_proc.inspect}"
604
+ end
605
+ set[var] = my_proc
606
+ end
607
+
608
+ # this is called _after_ working_directory is bound. This only
609
+ # parses the embedded switches in .ru files
610
+ # (for "rackup" compatibility)
611
+ def parse_rackup_file # :nodoc:
612
+ ru = RACKUP[:file] or return # we only return here in unit tests
613
+
614
+ # :rails means use (old) Rails autodetect
615
+ if ru == :rails
616
+ File.readable?('config.ru') or return
617
+ ru = 'config.ru'
618
+ end
619
+
620
+ File.readable?(ru) or
621
+ raise ArgumentError, "rackup file (#{ru}) not readable"
622
+
623
+ # it could be a .rb file, too, we don't parse those manually
624
+ ru =~ /\.ru\z/ or return
625
+
626
+ /^#\\(.*)/ =~ File.read(ru) or return
627
+ RACKUP[:optparse].parse!($1.split(/\s+/))
628
+
629
+ if RACKUP[:daemonize]
630
+ # unicorn_rails wants a default pid path, (not plain 'unicorn')
631
+ if after_reload
632
+ spid = set[:pid]
633
+ pid('tmp/pids/unicorn.pid') if spid.nil? || spid == :unset
634
+ end
635
+ unless RACKUP[:daemonized]
636
+ Unicorn::Launcher.daemonize!(RACKUP[:options])
637
+ RACKUP[:ready_pipe] = RACKUP[:options].delete(:ready_pipe)
638
+ end
639
+ end
640
+ end
641
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # :enddoc:
4
+ # Frequently used constants when constructing requests or responses.
5
+ # Many times the constant just refers to a string with the same
6
+ # contents. Using these constants gave about a 3% to 10% performance
7
+ # improvement over using the strings directly. Symbols did not really
8
+ # improve things much compared to constants.
9
+ module Unicorn::Const
10
+
11
+ UNICORN_VERSION = "4.3.1"
12
+
13
+ # default TCP listen host address (0.0.0.0, all interfaces)
14
+ DEFAULT_HOST = "0.0.0.0"
15
+
16
+ # default TCP listen port (8080)
17
+ DEFAULT_PORT = 8080
18
+
19
+ # default TCP listen address and port (0.0.0.0:8080)
20
+ DEFAULT_LISTEN = "#{DEFAULT_HOST}:#{DEFAULT_PORT}"
21
+
22
+ # The basic request body size we'll try to read at once (16 kilobytes).
23
+ CHUNK_SIZE = 16 * 1024
24
+
25
+ # Maximum request body size before it is moved out of memory and into a
26
+ # temporary file for reading (112 kilobytes). This is the default
27
+ # value of client_body_buffer_size.
28
+ MAX_BODY = 1024 * 112
29
+
30
+ # :stopdoc:
31
+ # common errors we'll send back
32
+ ERROR_400_RESPONSE = "HTTP/1.1 400 Bad Request\r\n\r\n"
33
+ ERROR_414_RESPONSE = "HTTP/1.1 414 Request-URI Too Long\r\n\r\n"
34
+ ERROR_413_RESPONSE = "HTTP/1.1 413 Request Entity Too Large\r\n\r\n"
35
+ ERROR_500_RESPONSE = "HTTP/1.1 500 Internal Server Error\r\n\r\n"
36
+ EXPECT_100_RESPONSE = "HTTP/1.1 100 Continue\r\n\r\n"
37
+
38
+ HTTP_EXPECT = "HTTP_EXPECT"
39
+ # :startdoc:
40
+ end