class 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.

threadpool_size[R]

allows setting of the eventmachine threadpool size

timeout[RW]

Maximum time for incoming data to arrive

Public Class Methods

new() click to toggle source
# File lib/thin/backends/base.rb, line 47
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
  @ssl                            = nil
  @threaded                       = nil
  @started_reactor                = false
end

Public Instance Methods

close() click to toggle source

Free up resources used by the backend.

# File lib/thin/backends/base.rb, line 112
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 101
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 125
def connection_finished(connection)
  @persistent_connection_count -= 1 if connection.can_persist?
  @connections.delete(connection.__id__)
  
  # 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 134
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 116
def running?
  @running
end
size() click to toggle source

Number of active connections.

# File lib/thin/backends/base.rb, line 139
def size
  @connections.size
end
ssl?() click to toggle source
# File lib/thin/backends/base.rb, line 39
def ssl?; @ssl end
start() { || ... } click to toggle source

Start the backend and connect it.

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

Stop of the backend from accepting new connections.

# File lib/thin/backends/base.rb, line 78
def stop
  @running  = false
  @stopping = true
  
  # Do not accept anymore connection
  disconnect
  # Close idle persistent connections
  @connections.each_value { |connection| connection.close_connection if connection.idle? }
  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 90
def stop!
  @running  = false
  @stopping = false
  
  EventMachine.stop if @started_reactor && EventMachine.reactor_running?
  @connections.each_value { |connection| connection.close_connection }
  close
end
threaded?() click to toggle source
# File lib/thin/backends/base.rb, line 35
def threaded?; @threaded end
threadpool_size=(size) click to toggle source
# File lib/thin/backends/base.rb, line 28
def threadpool_size=(size)
  @threadpool_size = size
  EventMachine.threadpool_size = size
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 145
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.__id__] = connection
end