Parent

Included Modules

Thin::Server

The uterly famous Thin HTTP server. It listen for incoming request through a given backend and forward all request to app.

TCP server

Create a new TCP server on bound to host:port by specifiying host and port as the first 2 arguments.

  Thin::Server.start('0.0.0.0', 3000, app)

UNIX domain server

Create a new UNIX domain socket bound to socket file by specifiying a filename as the first argument. Eg.: /tmp/thin.sock. If the first argument contains a / it will be assumed to be a UNIX socket.

  Thin::Server.start('/tmp/thin.sock', app)

Using a custom backend

You can implement your own way to connect the server to its client by creating your own Backend class and pass it as the :backend option.

  Thin::Server.start('galaxy://faraway', 1345, app, :backend => Thin::Backends::MyFancyBackend)

Rack application (app)

All requests will be processed through app that must be a valid Rack adapter. A valid Rack adapter (application) must respond to call(env#Hash) and return an array of [status, headers, body].

Building an app in place

If a block is passed, a Rack::Builder instance will be passed to build the app. So you can do cool stuff like this:

  Thin::Server.start('0.0.0.0', 3000) do
    use Rack::CommonLogger
    use Rack::ShowExceptions
    map "/lobster" do
      use Rack::Lint
      run Rack::Lobster.new
    end
  end

Controlling with signals

Disable signals by passing :signals => false

Constants

DEFAULT_TIMEOUT

Default values

DEFAULT_HOST
DEFAULT_PORT
DEFAULT_MAXIMUM_CONNECTIONS
DEFAULT_MAXIMUM_PERSISTENT_CONNECTIONS

Attributes

app[RW]

Application (Rack adapter) called with the request that produces the response.

tag[RW]

A tag that will show in the process listing

backend[RW]

Backend handling the connections to the clients.

Public Class Methods

new(*args, &block) click to toggle source
     # File lib/thin/server.rb, line 97
 97:     def initialize(*args, &block)
 98:       host, port, options = DEFAULT_HOST, DEFAULT_PORT, {}
 99:       
100:       # Guess each parameter by its type so they can be
101:       # received in any order.
102:       args.each do |arg|
103:         case arg
104:         when Fixnum, /^\d+$/ then port    = arg.to_i
105:         when String          then host    = arg
106:         when Hash            then options = arg
107:         else
108:           @app = arg if arg.respond_to?(:call)
109:         end
110:       end
111:       
112:       # Set tag if needed
113:       self.tag = options[:tag]
114: 
115:       # Try to intelligently select which backend to use.
116:       @backend = select_backend(host, port, options)
117:       
118:       load_cgi_multipart_eof_fix
119:       
120:       @backend.server = self
121:       
122:       # Set defaults
123:       @backend.maximum_connections            = DEFAULT_MAXIMUM_CONNECTIONS
124:       @backend.maximum_persistent_connections = DEFAULT_MAXIMUM_PERSISTENT_CONNECTIONS
125:       @backend.timeout                        = DEFAULT_TIMEOUT
126:       
127:       # Allow using Rack builder as a block
128:       @app = Rack::Builder.new(&block).to_app if block
129:       
130:       # If in debug mode, wrap in logger adapter
131:       @app = Rack::CommonLogger.new(@app) if Logging.debug?
132:       
133:       setup_signals unless options[:signals].class == FalseClass
134:     end
start(*args, &block) click to toggle source

Lil’ shortcut to turn this:

  Server.new(...).start

into this:

  Server.start(...)
     # File lib/thin/server.rb, line 144
144:     def self.start(*args, &block)
145:       new(*args, &block).start!
146:     end

Public Instance Methods

config() click to toggle source

Configure the server

The process might need to have superuser privilege to configure server with optimal options.

     # File lib/thin/server.rb, line 193
193:     def config
194:       @backend.config
195:     end
name() click to toggle source

Name of the server and type of backend used. This is also the name of the process in which Thin is running as a daemon.

     # File lib/thin/server.rb, line 199
199:     def name
200:       "thin server (#{@backend})" + (tag ? " [#{tag}]" : "")
201:     end
Also aliased as: to_s
running?() click to toggle source

Return true if the server is running and ready to receive requests. Note that the server might still be running and return false when shuting down and waiting for active connections to complete.

     # File lib/thin/server.rb, line 207
207:     def running?
208:       @backend.running?
209:     end
start() click to toggle source

Start the server and listen for connections.

     # File lib/thin/server.rb, line 149
149:     def start
150:       raise ArgumentError, 'app required' unless @app
151:       
152:       log   ">> Thin web server (v#{VERSION::STRING} codename #{VERSION::CODENAME})"
153:       debug ">> Debugging ON"
154:       trace ">> Tracing ON"
155:       
156:       log ">> Maximum connections set to #{@backend.maximum_connections}"
157:       log ">> Listening on #{@backend}, CTRL+C to stop"
158:       
159:       @backend.start
160:     end
Also aliased as: start!
start!() click to toggle source
Alias for: start
stop() click to toggle source

Gracefull shutdown

Stops the server after processing all current connections. As soon as this method is called, the server stops accepting new requests and wait for all current connections to finish. Calling twice is the equivalent of calling stop!.

     # File lib/thin/server.rb, line 168
168:     def stop
169:       if running?
170:         @backend.stop
171:         unless @backend.empty?
172:           log ">> Waiting for #{@backend.size} connection(s) to finish, " +
173:                  "can take up to #{timeout} sec, CTRL+C to stop now"
174:         end
175:       else
176:         stop!
177:       end
178:     end
stop!() click to toggle source

Force shutdown

Stops the server closing all current connections right away. This doesn’t wait for connection to finish their work and send data. All current requests will be dropped.

     # File lib/thin/server.rb, line 184
184:     def stop!
185:       log ">> Stopping ..."
186: 
187:       @backend.stop!
188:     end
to_s() click to toggle source
Alias for: name

Protected Instance Methods

load_cgi_multipart_eof_fix() click to toggle source

Taken from Mongrel cgi_multipart_eof_fix Ruby 1.8.5 has a security bug in cgi.rb, we need to patch it.

     # File lib/thin/server.rb, line 241
241:       def load_cgi_multipart_eof_fix
242:         version = RUBY_VERSION.split('.').map { |i| i.to_i }
243:         
244:         if version[0] <= 1 && version[1] <= 8 && version[2] <= 5 && RUBY_PLATFORM !~ /java/
245:           begin
246:             require 'cgi_multipart_eof_fix'
247:           rescue LoadError
248:             log "!! Ruby 1.8.5 is not secure please install cgi_multipart_eof_fix:"
249:             log "   gem install cgi_multipart_eof_fix"
250:           end
251:         end
252:       end
select_backend(host, port, options) click to toggle source
     # File lib/thin/server.rb, line 225
225:       def select_backend(host, port, options)
226:         case
227:         when options.has_key?(:backend)
228:           raise ArgumentError, ":backend must be a class" unless options[:backend].is_a?(Class)
229:           options[:backend].new(host, port, options)
230:         when options.has_key?(:swiftiply)
231:           Backends::SwiftiplyClient.new(host, port, options)
232:         when host.include?('/')
233:           Backends::UnixServer.new(host)
234:         else
235:           Backends::TcpServer.new(host, port)
236:         end
237:       end
setup_signals() click to toggle source

Register signals:

  • TERM & QUIT calls stop to shutdown gracefully.

  • INT calls stop! to force shutdown.

  • HUP calls restart to … surprise, restart!

     # File lib/thin/server.rb, line 216
216:       def setup_signals
217:         trap('INT')  { stop! }
218:         trap('TERM') { stop }
219:         unless Thin.win?
220:           trap('QUIT') { stop }
221:           trap('HUP')  { restart }
222:         end
223:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.