macournoyer-thin 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +233 -0
- data/COPYING +18 -0
- data/README +77 -0
- data/Rakefile +13 -0
- data/benchmark/abc +51 -0
- data/benchmark/benchmarker.rb +80 -0
- data/benchmark/runner +79 -0
- data/bin/thin +6 -0
- data/example/adapter.rb +32 -0
- data/example/config.ru +23 -0
- data/example/monit_sockets +20 -0
- data/example/monit_unixsock +20 -0
- data/example/myapp.rb +1 -0
- data/example/ramaze.ru +12 -0
- data/example/thin.god +80 -0
- data/example/thin_solaris_smf.erb +36 -0
- data/example/thin_solaris_smf.readme.txt +150 -0
- data/example/vlad.rake +64 -0
- data/ext/thin_parser/common.rl +55 -0
- data/ext/thin_parser/ext_help.h +14 -0
- data/ext/thin_parser/extconf.rb +6 -0
- data/ext/thin_parser/parser.c +452 -0
- data/ext/thin_parser/parser.h +49 -0
- data/ext/thin_parser/parser.rl +157 -0
- data/ext/thin_parser/thin.c +433 -0
- data/lib/rack/adapter/loader.rb +79 -0
- data/lib/rack/adapter/rails.rb +173 -0
- data/lib/rack/handler/thin.rb +18 -0
- data/lib/thin.rb +50 -0
- data/lib/thin/backends/base.rb +141 -0
- data/lib/thin/backends/swiftiply_client.rb +56 -0
- data/lib/thin/backends/tcp_server.rb +29 -0
- data/lib/thin/backends/unix_server.rb +51 -0
- data/lib/thin/command.rb +52 -0
- data/lib/thin/connection.rb +186 -0
- data/lib/thin/controllers/cluster.rb +127 -0
- data/lib/thin/controllers/controller.rb +183 -0
- data/lib/thin/controllers/service.rb +75 -0
- data/lib/thin/controllers/service.sh.erb +39 -0
- data/lib/thin/daemonizing.rb +163 -0
- data/lib/thin/headers.rb +39 -0
- data/lib/thin/logging.rb +54 -0
- data/lib/thin/request.rb +147 -0
- data/lib/thin/response.rb +99 -0
- data/lib/thin/runner.rb +208 -0
- data/lib/thin/server.rb +241 -0
- data/lib/thin/stats.html.erb +216 -0
- data/lib/thin/stats.rb +52 -0
- data/lib/thin/statuses.rb +43 -0
- data/lib/thin/version.rb +32 -0
- data/spec/backends/swiftiply_client_spec.rb +66 -0
- data/spec/backends/tcp_server_spec.rb +33 -0
- data/spec/backends/unix_server_spec.rb +37 -0
- data/spec/command_spec.rb +20 -0
- data/spec/configs/cluster.yml +9 -0
- data/spec/configs/single.yml +9 -0
- data/spec/connection_spec.rb +105 -0
- data/spec/controllers/cluster_spec.rb +212 -0
- data/spec/controllers/controller_spec.rb +129 -0
- data/spec/controllers/service_spec.rb +50 -0
- data/spec/daemonizing_spec.rb +192 -0
- data/spec/headers_spec.rb +40 -0
- data/spec/logging_spec.rb +46 -0
- data/spec/perf/request_perf_spec.rb +50 -0
- data/spec/perf/response_perf_spec.rb +19 -0
- data/spec/perf/server_perf_spec.rb +39 -0
- data/spec/rack/loader_spec.rb +29 -0
- data/spec/rack/rails_adapter_spec.rb +106 -0
- data/spec/rails_app/app/controllers/application.rb +10 -0
- data/spec/rails_app/app/controllers/simple_controller.rb +19 -0
- data/spec/rails_app/app/helpers/application_helper.rb +3 -0
- data/spec/rails_app/app/views/simple/index.html.erb +15 -0
- data/spec/rails_app/config/boot.rb +109 -0
- data/spec/rails_app/config/environment.rb +64 -0
- data/spec/rails_app/config/environments/development.rb +18 -0
- data/spec/rails_app/config/environments/production.rb +19 -0
- data/spec/rails_app/config/environments/test.rb +22 -0
- data/spec/rails_app/config/initializers/inflections.rb +10 -0
- data/spec/rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails_app/config/routes.rb +35 -0
- data/spec/rails_app/public/404.html +30 -0
- data/spec/rails_app/public/422.html +30 -0
- data/spec/rails_app/public/500.html +30 -0
- data/spec/rails_app/public/dispatch.cgi +10 -0
- data/spec/rails_app/public/dispatch.fcgi +24 -0
- data/spec/rails_app/public/dispatch.rb +10 -0
- data/spec/rails_app/public/favicon.ico +0 -0
- data/spec/rails_app/public/images/rails.png +0 -0
- data/spec/rails_app/public/index.html +277 -0
- data/spec/rails_app/public/javascripts/application.js +2 -0
- data/spec/rails_app/public/javascripts/controls.js +963 -0
- data/spec/rails_app/public/javascripts/dragdrop.js +972 -0
- data/spec/rails_app/public/javascripts/effects.js +1120 -0
- data/spec/rails_app/public/javascripts/prototype.js +4225 -0
- data/spec/rails_app/public/robots.txt +5 -0
- data/spec/rails_app/script/about +3 -0
- data/spec/rails_app/script/console +3 -0
- data/spec/rails_app/script/destroy +3 -0
- data/spec/rails_app/script/generate +3 -0
- data/spec/rails_app/script/performance/benchmarker +3 -0
- data/spec/rails_app/script/performance/profiler +3 -0
- data/spec/rails_app/script/performance/request +3 -0
- data/spec/rails_app/script/plugin +3 -0
- data/spec/rails_app/script/process/inspector +3 -0
- data/spec/rails_app/script/process/reaper +3 -0
- data/spec/rails_app/script/process/spawner +3 -0
- data/spec/rails_app/script/runner +3 -0
- data/spec/rails_app/script/server +3 -0
- data/spec/request/mongrel_spec.rb +39 -0
- data/spec/request/parser_spec.rb +215 -0
- data/spec/request/persistent_spec.rb +35 -0
- data/spec/request/processing_spec.rb +45 -0
- data/spec/response_spec.rb +83 -0
- data/spec/runner_spec.rb +167 -0
- data/spec/server/builder_spec.rb +44 -0
- data/spec/server/pipelining_spec.rb +109 -0
- data/spec/server/robustness_spec.rb +34 -0
- data/spec/server/stopping_spec.rb +45 -0
- data/spec/server/swiftiply.yml +6 -0
- data/spec/server/swiftiply_spec.rb +32 -0
- data/spec/server/tcp_spec.rb +57 -0
- data/spec/server/threaded_spec.rb +27 -0
- data/spec/server/unix_socket_spec.rb +26 -0
- data/spec/server_spec.rb +96 -0
- data/spec/spec_helper.rb +219 -0
- data/tasks/announce.rake +22 -0
- data/tasks/deploy.rake +16 -0
- data/tasks/email.erb +30 -0
- data/tasks/ext.rake +42 -0
- data/tasks/gem.rake +108 -0
- data/tasks/rdoc.rake +25 -0
- data/tasks/site.rake +15 -0
- data/tasks/spec.rake +48 -0
- data/tasks/stats.rake +28 -0
- metadata +248 -0
@@ -0,0 +1,79 @@
|
|
1
|
+
module Rack
|
2
|
+
class AdapterNotFound < RuntimeError; end
|
3
|
+
|
4
|
+
# Mapping used to guess which adapter to use in <tt>Adapter.for</tt>.
|
5
|
+
# Framework <name> => <file unique to this framework> in order they will
|
6
|
+
# be tested.
|
7
|
+
# +nil+ for value to never guess.
|
8
|
+
# NOTE: If a framework has a file that is not unique, make sure to place
|
9
|
+
# it at the end.
|
10
|
+
ADAPTERS = [
|
11
|
+
[:rails, 'config/environment.rb'],
|
12
|
+
[:ramaze, 'start.rb'],
|
13
|
+
[:halcyon, 'runner.ru'],
|
14
|
+
[:merb, 'config/init.rb'],
|
15
|
+
[:mack, 'config/app_config/default.yml'],
|
16
|
+
[:mack, 'config/configatron/default.rb'],
|
17
|
+
[:file, nil]
|
18
|
+
]
|
19
|
+
|
20
|
+
module Adapter
|
21
|
+
# Guess which adapter to use based on the directory structure
|
22
|
+
# or file content.
|
23
|
+
# Returns a symbol representing the name of the adapter to use
|
24
|
+
# to load the application under <tt>dir/</tt>.
|
25
|
+
def self.guess(dir)
|
26
|
+
ADAPTERS.each do |adapter, file|
|
27
|
+
return adapter if file && ::File.exist?(::File.join(dir, file))
|
28
|
+
end
|
29
|
+
raise AdapterNotFound, "No adapter found for #{dir}"
|
30
|
+
end
|
31
|
+
|
32
|
+
# Loads an adapter identified by +name+ using +options+ hash.
|
33
|
+
def self.for(name, options={})
|
34
|
+
case name.to_sym
|
35
|
+
when :rails
|
36
|
+
return Rails.new(options.merge(:root => options[:chdir]))
|
37
|
+
|
38
|
+
when :ramaze
|
39
|
+
require "#{options[:chdir]}/start"
|
40
|
+
|
41
|
+
Ramaze.trait[:essentials].delete Ramaze::Adapter
|
42
|
+
Ramaze.start :force => true
|
43
|
+
|
44
|
+
return Ramaze::Adapter::Base
|
45
|
+
|
46
|
+
when :merb
|
47
|
+
require 'merb-core'
|
48
|
+
|
49
|
+
Merb::Config.setup(:merb_root => options[:chdir],
|
50
|
+
:environment => options[:environment])
|
51
|
+
Merb.environment = Merb::Config[:environment]
|
52
|
+
Merb.root = Merb::Config[:merb_root]
|
53
|
+
Merb::BootLoader.run
|
54
|
+
|
55
|
+
return Merb::Rack::Application.new
|
56
|
+
|
57
|
+
when :halcyon
|
58
|
+
require 'halcyon'
|
59
|
+
|
60
|
+
$:.unshift(Halcyon.root/'lib')
|
61
|
+
|
62
|
+
return Halcyon::Runner.new
|
63
|
+
|
64
|
+
when :mack
|
65
|
+
ENV["MACK_ENV"] = options[:environment]
|
66
|
+
load(::File.join(options[:chdir], "Rakefile"))
|
67
|
+
require 'mack'
|
68
|
+
return Mack::Utils::Server.build_app
|
69
|
+
|
70
|
+
when :file
|
71
|
+
return Rack::File.new(options[:chdir])
|
72
|
+
|
73
|
+
else
|
74
|
+
raise AdapterNotFound, "Adapter not found: #{name}"
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
3
|
+
# Adapter to run a Rails app with any supported Rack handler.
|
4
|
+
# By default it will try to load the Rails application in the
|
5
|
+
# current directory in the development environment.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
# root: Root directory of the Rails app
|
9
|
+
# environment: Rails environment to run in (development [default], production or test)
|
10
|
+
# prefix: Set the relative URL root.
|
11
|
+
#
|
12
|
+
# Based on http://fuzed.rubyforge.org/ Rails adapter
|
13
|
+
module Rack
|
14
|
+
module Adapter
|
15
|
+
class Rails
|
16
|
+
FILE_METHODS = %w(GET HEAD).freeze
|
17
|
+
|
18
|
+
def initialize(options={})
|
19
|
+
@root = options[:root] || Dir.pwd
|
20
|
+
@env = options[:environment] || 'development'
|
21
|
+
@prefix = options[:prefix]
|
22
|
+
|
23
|
+
load_application
|
24
|
+
|
25
|
+
@file_server = Rack::File.new(::File.join(RAILS_ROOT, "public"))
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_application
|
29
|
+
ENV['RAILS_ENV'] = @env
|
30
|
+
|
31
|
+
require "#{@root}/config/environment"
|
32
|
+
require 'dispatcher'
|
33
|
+
|
34
|
+
if @prefix
|
35
|
+
if ActionController::Base.respond_to?('relative_url_root=')
|
36
|
+
ActionController::Base.relative_url_root = @prefix # Rails 2.1.1
|
37
|
+
else
|
38
|
+
ActionController::AbstractRequest.relative_url_root = @prefix
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# TODO refactor this in File#can_serve?(path) ??
|
44
|
+
def file_exist?(path)
|
45
|
+
full_path = ::File.join(@file_server.root, Utils.unescape(path))
|
46
|
+
::File.file?(full_path) && ::File.readable_real?(full_path)
|
47
|
+
end
|
48
|
+
|
49
|
+
def serve_file(env)
|
50
|
+
@file_server.call(env)
|
51
|
+
end
|
52
|
+
|
53
|
+
def serve_rails(env)
|
54
|
+
request = Request.new(env)
|
55
|
+
response = Response.new
|
56
|
+
|
57
|
+
session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
|
58
|
+
cgi = CGIWrapper.new(request, response)
|
59
|
+
|
60
|
+
Dispatcher.dispatch(cgi, session_options, response)
|
61
|
+
|
62
|
+
response.finish
|
63
|
+
end
|
64
|
+
|
65
|
+
def call(env)
|
66
|
+
path = env['PATH_INFO'].chomp('/')
|
67
|
+
method = env['REQUEST_METHOD']
|
68
|
+
cached_path = (path.empty? ? 'index' : path) + ActionController::Base.page_cache_extension
|
69
|
+
|
70
|
+
if FILE_METHODS.include?(method)
|
71
|
+
if file_exist?(path) # Serve the file if it's there
|
72
|
+
return serve_file(env)
|
73
|
+
elsif file_exist?(cached_path) # Serve the page cache if it's there
|
74
|
+
env['PATH_INFO'] = cached_path
|
75
|
+
return serve_file(env)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# No static file, let Rails handle it
|
80
|
+
serve_rails(env)
|
81
|
+
end
|
82
|
+
|
83
|
+
protected
|
84
|
+
|
85
|
+
class CGIWrapper < ::CGI
|
86
|
+
def initialize(request, response, *args)
|
87
|
+
@request = request
|
88
|
+
@response = response
|
89
|
+
@args = *args
|
90
|
+
@input = request.body
|
91
|
+
|
92
|
+
super *args
|
93
|
+
end
|
94
|
+
|
95
|
+
def header(options = "text/html")
|
96
|
+
if options.is_a?(String)
|
97
|
+
@response['Content-Type'] = options unless @response['Content-Type']
|
98
|
+
else
|
99
|
+
@response['Content-Length'] = options.delete('Content-Length').to_s if options['Content-Length']
|
100
|
+
|
101
|
+
@response['Content-Type'] = options.delete('type') || "text/html"
|
102
|
+
@response['Content-Type'] += "; charset=" + options.delete('charset') if options['charset']
|
103
|
+
|
104
|
+
@response['Content-Language'] = options.delete('language') if options['language']
|
105
|
+
@response['Expires'] = options.delete('expires') if options['expires']
|
106
|
+
|
107
|
+
@response.status = options.delete('Status') if options['Status']
|
108
|
+
|
109
|
+
# Convert 'cookie' header to 'Set-Cookie' headers.
|
110
|
+
# Because Set-Cookie header can appear more the once in the response body,
|
111
|
+
# we store it in a line break seperated string that will be translated to
|
112
|
+
# multiple Set-Cookie header by the handler.
|
113
|
+
if cookie = options.delete('cookie')
|
114
|
+
cookies = []
|
115
|
+
|
116
|
+
case cookie
|
117
|
+
when Array then cookie.each { |c| cookies << c.to_s }
|
118
|
+
when Hash then cookie.each { |_, c| cookies << c.to_s }
|
119
|
+
else cookies << cookie.to_s
|
120
|
+
end
|
121
|
+
|
122
|
+
@output_cookies.each { |c| cookies << c.to_s } if @output_cookies
|
123
|
+
|
124
|
+
@response['Set-Cookie'] = [@response['Set-Cookie'], cookies].compact
|
125
|
+
# See http://groups.google.com/group/rack-devel/browse_thread/thread/e8759b91a82c5a10/a8dbd4574fe97d69?#a8dbd4574fe97d69
|
126
|
+
if Thin.ruby_18?
|
127
|
+
@response['Set-Cookie'].flatten!
|
128
|
+
else
|
129
|
+
@response['Set-Cookie'] = @response['Set-Cookie'].join("\n")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
options.each { |k,v| @response[k] = v }
|
134
|
+
end
|
135
|
+
|
136
|
+
""
|
137
|
+
end
|
138
|
+
|
139
|
+
def params
|
140
|
+
@params ||= @request.params
|
141
|
+
end
|
142
|
+
|
143
|
+
def cookies
|
144
|
+
@request.cookies
|
145
|
+
end
|
146
|
+
|
147
|
+
def query_string
|
148
|
+
@request.query_string
|
149
|
+
end
|
150
|
+
|
151
|
+
# Used to wrap the normal args variable used inside CGI.
|
152
|
+
def args
|
153
|
+
@args
|
154
|
+
end
|
155
|
+
|
156
|
+
# Used to wrap the normal env_table variable used inside CGI.
|
157
|
+
def env_table
|
158
|
+
@request.env
|
159
|
+
end
|
160
|
+
|
161
|
+
# Used to wrap the normal stdinput variable used inside CGI.
|
162
|
+
def stdinput
|
163
|
+
@input
|
164
|
+
end
|
165
|
+
|
166
|
+
def stdoutput
|
167
|
+
STDERR.puts "stdoutput should not be used."
|
168
|
+
@response.body
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Rack
|
2
|
+
module Handler
|
3
|
+
# Rack Handler stricly to be able to use Thin through the rackup command.
|
4
|
+
# To do so, simply require 'thin' in your Rack config file and run like this
|
5
|
+
#
|
6
|
+
# rackup --server thin
|
7
|
+
#
|
8
|
+
class Thin
|
9
|
+
def self.run(app, options={})
|
10
|
+
server = ::Thin::Server.new(options[:Host] || '0.0.0.0',
|
11
|
+
options[:Port] || 8080,
|
12
|
+
app)
|
13
|
+
yield server if block_given?
|
14
|
+
server.start
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/thin.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
require 'timeout'
|
5
|
+
require 'stringio'
|
6
|
+
require 'time'
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'eventmachine'
|
10
|
+
|
11
|
+
require 'thin/version'
|
12
|
+
require 'thin/statuses'
|
13
|
+
|
14
|
+
module Thin
|
15
|
+
autoload :Command, 'thin/command'
|
16
|
+
autoload :Connection, 'thin/connection'
|
17
|
+
autoload :Daemonizable, 'thin/daemonizing'
|
18
|
+
autoload :Logging, 'thin/logging'
|
19
|
+
autoload :Headers, 'thin/headers'
|
20
|
+
autoload :Request, 'thin/request'
|
21
|
+
autoload :Response, 'thin/response'
|
22
|
+
autoload :Runner, 'thin/runner'
|
23
|
+
autoload :Server, 'thin/server'
|
24
|
+
autoload :Stats, 'thin/stats'
|
25
|
+
|
26
|
+
module Backends
|
27
|
+
autoload :Base, 'thin/backends/base'
|
28
|
+
autoload :SwiftiplyClient, 'thin/backends/swiftiply_client'
|
29
|
+
autoload :TcpServer, 'thin/backends/tcp_server'
|
30
|
+
autoload :UnixServer, 'thin/backends/unix_server'
|
31
|
+
end
|
32
|
+
|
33
|
+
module Controllers
|
34
|
+
autoload :Cluster, 'thin/controllers/cluster'
|
35
|
+
autoload :Controller, 'thin/controllers/controller'
|
36
|
+
autoload :Service, 'thin/controllers/service'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
require 'rack'
|
41
|
+
require 'rack/adapter/loader'
|
42
|
+
|
43
|
+
module Rack
|
44
|
+
module Handler
|
45
|
+
autoload :Thin, 'rack/handler/thin'
|
46
|
+
end
|
47
|
+
module Adapter
|
48
|
+
autoload :Rails, 'rack/adapter/rails'
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
module Thin
|
2
|
+
module Backends
|
3
|
+
# A Backend connects the server to the client. It handles:
|
4
|
+
# * connection/disconnection to the server
|
5
|
+
# * initialization of the connections
|
6
|
+
# * manitoring of the active connections.
|
7
|
+
#
|
8
|
+
# == Implementing your own backend
|
9
|
+
# You can create your own minimal backend by inheriting this class and
|
10
|
+
# defining the +connect+ and +disconnect+ method.
|
11
|
+
# If your backend is not based on EventMachine you also need to redefine
|
12
|
+
# the +start+, +stop+, <tt>stop!</tt> and +config+ methods.
|
13
|
+
class Base
|
14
|
+
# Server serving the connections throught the backend
|
15
|
+
attr_accessor :server
|
16
|
+
|
17
|
+
# Maximum time for incoming data to arrive
|
18
|
+
attr_accessor :timeout
|
19
|
+
|
20
|
+
# Maximum number of file or socket descriptors that the server may open.
|
21
|
+
attr_accessor :maximum_connections
|
22
|
+
|
23
|
+
# Maximum number of connections that can be persistent
|
24
|
+
attr_accessor :maximum_persistent_connections
|
25
|
+
|
26
|
+
# Allow using threads in the backend.
|
27
|
+
attr_writer :threaded
|
28
|
+
def threaded?; @threaded end
|
29
|
+
|
30
|
+
# Number of persistent connections currently opened
|
31
|
+
attr_accessor :persistent_connection_count
|
32
|
+
|
33
|
+
# Disable the use of epoll under Linux
|
34
|
+
attr_accessor :no_epoll
|
35
|
+
|
36
|
+
def initialize
|
37
|
+
@connections = []
|
38
|
+
@timeout = Server::DEFAULT_TIMEOUT
|
39
|
+
@persistent_connection_count = 0
|
40
|
+
@maximum_connections = Server::DEFAULT_MAXIMUM_CONNECTIONS
|
41
|
+
@maximum_persistent_connections = Server::DEFAULT_MAXIMUM_PERSISTENT_CONNECTIONS
|
42
|
+
@no_epoll = false
|
43
|
+
end
|
44
|
+
|
45
|
+
# Start the backend and connect it.
|
46
|
+
def start
|
47
|
+
@stopping = false
|
48
|
+
starter = proc do
|
49
|
+
connect
|
50
|
+
@running = true
|
51
|
+
end
|
52
|
+
|
53
|
+
# Allow for early run up of eventmachine.
|
54
|
+
if EventMachine.reactor_running?
|
55
|
+
starter.call
|
56
|
+
else
|
57
|
+
EventMachine.run(&starter)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Stop of the backend from accepting new connections.
|
62
|
+
def stop
|
63
|
+
@running = false
|
64
|
+
@stopping = true
|
65
|
+
|
66
|
+
# Do not accept anymore connection
|
67
|
+
disconnect
|
68
|
+
stop! if @connections.empty?
|
69
|
+
end
|
70
|
+
|
71
|
+
# Force stop of the backend NOW, too bad for the current connections.
|
72
|
+
def stop!
|
73
|
+
@running = false
|
74
|
+
@stopping = false
|
75
|
+
|
76
|
+
EventMachine.stop if EventMachine.reactor_running?
|
77
|
+
@connections.each { |connection| connection.close_connection }
|
78
|
+
close
|
79
|
+
end
|
80
|
+
|
81
|
+
# Configure the backend. This method will be called before droping superuser privileges,
|
82
|
+
# so you can do crazy stuff that require godlike powers here.
|
83
|
+
def config
|
84
|
+
# See http://rubyeventmachine.com/pub/rdoc/files/EPOLL.html
|
85
|
+
EventMachine.epoll unless @no_epoll
|
86
|
+
|
87
|
+
# Set the maximum number of socket descriptors that the server may open.
|
88
|
+
# The process needs to have required privilege to set it higher the 1024 on
|
89
|
+
# some systems.
|
90
|
+
@maximum_connections = EventMachine.set_descriptor_table_size(@maximum_connections) unless Thin.win?
|
91
|
+
end
|
92
|
+
|
93
|
+
# Free up resources used by the backend.
|
94
|
+
def close
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns +true+ if the backend is connected and running.
|
98
|
+
def running?
|
99
|
+
@running
|
100
|
+
end
|
101
|
+
|
102
|
+
# Called by a connection when it's unbinded.
|
103
|
+
def connection_finished(connection)
|
104
|
+
@persistent_connection_count -= 1 if connection.can_persist?
|
105
|
+
@connections.delete(connection)
|
106
|
+
|
107
|
+
# Finalize gracefull stop if there's no more active connection.
|
108
|
+
stop! if @stopping && @connections.empty?
|
109
|
+
end
|
110
|
+
|
111
|
+
# Returns +true+ if no active connection.
|
112
|
+
def empty?
|
113
|
+
@connections.empty?
|
114
|
+
end
|
115
|
+
|
116
|
+
# Number of active connections.
|
117
|
+
def size
|
118
|
+
@connections.size
|
119
|
+
end
|
120
|
+
|
121
|
+
protected
|
122
|
+
# Initialize a new connection to a client.
|
123
|
+
def initialize_connection(connection)
|
124
|
+
connection.backend = self
|
125
|
+
connection.app = @server.app
|
126
|
+
connection.comm_inactivity_timeout = @timeout
|
127
|
+
connection.threaded = @threaded
|
128
|
+
|
129
|
+
# We control the number of persistent connections by keeping
|
130
|
+
# a count of the total one allowed yet.
|
131
|
+
if @persistent_connection_count < @maximum_persistent_connections
|
132
|
+
connection.can_persist!
|
133
|
+
@persistent_connection_count += 1
|
134
|
+
end
|
135
|
+
|
136
|
+
@connections << connection
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|