Object
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.
# File lib/thin/backends/base.rb, line 40 40: def initialize 41: @connections = [] 42: @timeout = Server::DEFAULT_TIMEOUT 43: @persistent_connection_count = 0 44: @maximum_connections = Server::DEFAULT_MAXIMUM_CONNECTIONS 45: @maximum_persistent_connections = Server::DEFAULT_MAXIMUM_PERSISTENT_CONNECTIONS 46: @no_epoll = false 47: end
Free up resources used by the backend.
# File lib/thin/backends/base.rb, line 98 98: def close 99: 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 87 87: def config 88: # See http://rubyeventmachine.com/pub/rdoc/files/EPOLL.html 89: EventMachine.epoll unless @no_epoll 90: 91: # Set the maximum number of socket descriptors that the server may open. 92: # The process needs to have required privilege to set it higher the 1024 on 93: # some systems. 94: @maximum_connections = EventMachine.set_descriptor_table_size(@maximum_connections) unless Thin.win? 95: end
Called by a connection when it’s unbinded.
# File lib/thin/backends/base.rb, line 107 107: def connection_finished(connection) 108: @persistent_connection_count -= 1 if connection.can_persist? 109: @connections.delete(connection) 110: 111: # Finalize gracefull stop if there's no more active connection. 112: stop! if @stopping && @connections.empty? 113: end
Returns true if no active connection.
# File lib/thin/backends/base.rb, line 116 116: def empty? 117: @connections.empty? 118: end
Returns true if the backend is connected and running.
# File lib/thin/backends/base.rb, line 102 102: def running? 103: @running 104: end
Number of active connections.
# File lib/thin/backends/base.rb, line 121 121: def size 122: @connections.size 123: end
Start the backend and connect it.
# File lib/thin/backends/base.rb, line 50 50: def start 51: @stopping = false 52: starter = proc do 53: connect 54: @running = true 55: end 56: 57: # Allow for early run up of eventmachine. 58: if EventMachine.reactor_running? 59: starter.call 60: else 61: EventMachine.run(&starter) 62: end 63: end
Stop of the backend from accepting new connections.
# File lib/thin/backends/base.rb, line 66 66: def stop 67: @running = false 68: @stopping = true 69: 70: # Do not accept anymore connection 71: disconnect 72: stop! if @connections.empty? 73: end
Force stop of the backend NOW, too bad for the current connections.
# File lib/thin/backends/base.rb, line 76 76: def stop! 77: @running = false 78: @stopping = false 79: 80: EventMachine.stop if EventMachine.reactor_running? 81: @connections.each { |connection| connection.close_connection } 82: close 83: end
Initialize a new connection to a client.
# File lib/thin/backends/base.rb, line 127 127: def initialize_connection(connection) 128: connection.backend = self 129: connection.app = @server.app 130: connection.comm_inactivity_timeout = @timeout 131: connection.threaded = @threaded 132: 133: if @ssl 134: connection.start_tls(@ssl_options) 135: end 136: 137: # We control the number of persistent connections by keeping 138: # a count of the total one allowed yet. 139: if @persistent_connection_count < @maximum_persistent_connections 140: connection.can_persist! 141: @persistent_connection_count += 1 142: end 143: 144: @connections << connection 145: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.