Parent

Thin::Backends::Base

A Backend connects the server to the client. It handles:

Implementing your own backend

You can create your own minimal backend by inheriting this class and defining the connect and disconnect method. If your backend is not based on EventMachine you also need to redefine the start, stop, stop! and config methods.

Attributes

maximum_connections[RW]

Maximum number of file or socket descriptors that the server may open.

maximum_persistent_connections[RW]

Maximum number of connections that can be persistent

no_epoll[RW]

Disable the use of epoll under Linux

persistent_connection_count[RW]

Number of persistent connections currently opened

server[RW]

Server serving the connections throught the backend

ssl[W]

Allow using SSL in the backend.

ssl_options[W]

Allow using SSL in the backend.

threaded[W]

Allow using threads in the backend.

timeout[RW]

Maximum time for incoming data to arrive

Public Class Methods

new() click to toggle source
# File lib/thin/backends/base.rb, line 40
def initialize
  @connections                    = []
  @timeout                        = Server::DEFAULT_TIMEOUT
  @persistent_connection_count    = 0
  @maximum_connections            = Server::DEFAULT_MAXIMUM_CONNECTIONS
  @maximum_persistent_connections = Server::DEFAULT_MAXIMUM_PERSISTENT_CONNECTIONS
  @no_epoll                       = false
end

Public Instance Methods

close() click to toggle source

Free up resources used by the backend.

# File lib/thin/backends/base.rb, line 98
def close
end
config() click to toggle source

Configure the backend. This method will be called before droping superuser privileges, so you can do crazy stuff that require godlike powers here.

# File lib/thin/backends/base.rb, line 87
def config
  # See http://rubyeventmachine.com/pub/rdoc/files/EPOLL.html
  EventMachine.epoll unless @no_epoll
  
  # Set the maximum number of socket descriptors that the server may open.
  # The process needs to have required privilege to set it higher the 1024 on
  # some systems.
  @maximum_connections = EventMachine.set_descriptor_table_size(@maximum_connections) unless Thin.win?
end
connection_finished(connection) click to toggle source

Called by a connection when it's unbinded.

# File lib/thin/backends/base.rb, line 107
def connection_finished(connection)
  @persistent_connection_count -= 1 if connection.can_persist?
  @connections.delete(connection)
  
  # Finalize gracefull stop if there's no more active connection.
  stop! if @stopping && @connections.empty?
end
empty?() click to toggle source

Returns true if no active connection.

# File lib/thin/backends/base.rb, line 116
def empty?
  @connections.empty?
end
running?() click to toggle source

Returns true if the backend is connected and running.

# File lib/thin/backends/base.rb, line 102
def running?
  @running
end
size() click to toggle source

Number of active connections.

# File lib/thin/backends/base.rb, line 121
def size
  @connections.size
end
ssl?() click to toggle source
# File lib/thin/backends/base.rb, line 32
def ssl?; @ssl end
start() click to toggle source

Start the backend and connect it.

# File lib/thin/backends/base.rb, line 50
def start
  @stopping = false
  starter   = proc do
    connect
    @running = true
  end
  
  # Allow for early run up of eventmachine.
  if EventMachine.reactor_running?
    starter.call
  else
    EventMachine.run(&starter)
  end
end
stop() click to toggle source

Stop of the backend from accepting new connections.

# File lib/thin/backends/base.rb, line 66
def stop
  @running  = false
  @stopping = true
  
  # Do not accept anymore connection
  disconnect
  stop! if @connections.empty?
end
stop!() click to toggle source

Force stop of the backend NOW, too bad for the current connections.

# File lib/thin/backends/base.rb, line 76
def stop!
  @running  = false
  @stopping = false
  
  EventMachine.stop if EventMachine.reactor_running?
  @connections.each { |connection| connection.close_connection }
  close
end
threaded?() click to toggle source
# File lib/thin/backends/base.rb, line 28
def threaded?; @threaded end

Protected Instance Methods

initialize_connection(connection) click to toggle source

Initialize a new connection to a client.

# File lib/thin/backends/base.rb, line 127
def initialize_connection(connection)
  connection.backend                 = self
  connection.app                     = @server.app
  connection.comm_inactivity_timeout = @timeout
  connection.threaded                = @threaded
  
  if @ssl
    connection.start_tls(@ssl_options)
  end

  # We control the number of persistent connections by keeping
  # a count of the total one allowed yet.
  if @persistent_connection_count < @maximum_persistent_connections
    connection.can_persist!
    @persistent_connection_count += 1
  end

  @connections << connection
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.