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.
allows setting of the eventmachine threadpool size
Maximum time for incoming data to arrive
# 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
Free up resources used by the backend.
# File lib/thin/backends/base.rb, line 112 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 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
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
Returns true
if no active connection.
# File lib/thin/backends/base.rb, line 134 def empty? @connections.empty? end
Returns true
if the backend is connected and running.
# File lib/thin/backends/base.rb, line 116 def running? @running end
Number of active connections.
# File lib/thin/backends/base.rb, line 139 def size @connections.size end
# File lib/thin/backends/base.rb, line 39 def ssl?; @ssl end
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
# File lib/thin/backends/base.rb, line 120 def started_reactor? @started_reactor end
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
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
# File lib/thin/backends/base.rb, line 35 def threaded?; @threaded end
# File lib/thin/backends/base.rb, line 28 def threadpool_size=(size) @threadpool_size = size EventMachine.threadpool_size = size end
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