A Backend connects the server to the client. It handles:
connection/disconnection to the server
initialization of the connections
manitoring of the active connections.
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.
Maximum number of file or socket descriptors that the server may open.
Maximum number of connections that can be persistent
Disable the use of epoll under Linux
Number of persistent connections currently opened
Server serving the connections throught the backend
Allow using SSL in the backend.
Allow using SSL in the backend.
Allow using threads in the backend.
Maximum time for incoming data to arrive
# 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 @ssl = nil @threaded = nil end
Free up resources used by the backend.
# File lib/thin/backends/base.rb, line 102 def close end
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 91 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
Called by a connection when it’s unbinded.
# File lib/thin/backends/base.rb, line 111 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
Returns true
if no active connection.
# File lib/thin/backends/base.rb, line 120 def empty? @connections.empty? end
Returns true
if the backend is connected and running.
# File lib/thin/backends/base.rb, line 106 def running? @running end
Number of active connections.
# File lib/thin/backends/base.rb, line 125 def size @connections.size end
# File lib/thin/backends/base.rb, line 32 def ssl?; @ssl end
Start the backend and connect it.
# File lib/thin/backends/base.rb, line 52 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 of the backend from accepting new connections.
# File lib/thin/backends/base.rb, line 68 def stop @running = false @stopping = true # Do not accept anymore connection disconnect # Close idle persistent connections @connections.each { |connection| connection.close_connection if connection.idle? } stop! if @connections.empty? end
Force stop of the backend NOW, too bad for the current connections.
# File lib/thin/backends/base.rb, line 80 def stop! @running = false @stopping = false EventMachine.stop if EventMachine.reactor_running? @connections.each { |connection| connection.close_connection } close end
# File lib/thin/backends/base.rb, line 28 def threaded?; @threaded end
Initialize a new connection to a client.
# File lib/thin/backends/base.rb, line 131 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