Class: Thin::Backends::Base

Class Thin::Backends::Base < Object

(in files lib/thin/backends/base.rb )

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

  • connection/disconnection to the server
  • initialization of the connections
  • manitoring of the active connections.

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.

Methods

Public Class new()

    # File lib/thin/backends/base.rb, line 36
36:       def initialize
37:         @connections                    = []
38:         @timeout                        = Server::DEFAULT_TIMEOUT
39:         @persistent_connection_count    = 0
40:         @maximum_connections            = Server::DEFAULT_MAXIMUM_CONNECTIONS
41:         @maximum_persistent_connections = Server::DEFAULT_MAXIMUM_PERSISTENT_CONNECTIONS
42:         @no_epoll                       = false
43:       end

Public Instance close()

Free up resources used by the backend.

    # File lib/thin/backends/base.rb, line 94
94:       def close
95:       end

Public Instance config()

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 83
83:       def config
84:         # See http://rubyeventmachine.com/pub/rdoc/files/EPOLL.html
85:         EventMachine.epoll unless @no_epoll
86:         
87:         # Set the maximum number of socket descriptors that the server may open.
88:         # The process needs to have required privilege to set it higher the 1024 on
89:         # some systems.
90:         @maximum_connections = EventMachine.set_descriptor_table_size(@maximum_connections) unless Thin.win?
91:       end

Public Instance connection_finished(connection)

Called by a connection when it‘s unbinded.

     # File lib/thin/backends/base.rb, line 103
103:       def connection_finished(connection)
104:         @persistent_connection_count -= 1 if connection.can_persist?
105:         @connections.delete(connection)
106:         
107:         # Finalize gracefull stop if there's no more active connection.
108:         stop! if @stopping && @connections.empty?
109:       end

Public Instance empty?()

Returns true if no active connection.

     # File lib/thin/backends/base.rb, line 112
112:       def empty?
113:         @connections.empty?
114:       end

Public Instance running?()

Returns true if the backend is connected and running.

     # File lib/thin/backends/base.rb, line 98
 98:       def running?
 99:         @running
100:       end

Public Instance size()

Number of active connections.

     # File lib/thin/backends/base.rb, line 117
117:       def size
118:         @connections.size
119:       end

Public Instance start()

Start the backend and connect it.

    # File lib/thin/backends/base.rb, line 46
46:       def start
47:         @stopping = false
48:         starter   = proc do
49:           connect
50:           @running = true
51:         end
52:         
53:         # Allow for early run up of eventmachine.
54:         if EventMachine.reactor_running?
55:           starter.call
56:         else
57:           EventMachine.run(&starter)
58:         end
59:       end

Public Instance stop()

Stop of the backend from accepting new connections.

    # File lib/thin/backends/base.rb, line 62
62:       def stop
63:         @running  = false
64:         @stopping = true
65:         
66:         # Do not accept anymore connection
67:         disconnect
68:         stop! if @connections.empty?
69:       end

Public Instance stop!()

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

    # File lib/thin/backends/base.rb, line 72
72:       def stop!
73:         @running  = false
74:         @stopping = false
75:         
76:         EventMachine.stop if EventMachine.reactor_running?
77:         @connections.each { |connection| connection.close_connection }
78:         close
79:       end

Public Instance threaded?()

    # File lib/thin/backends/base.rb, line 28
28:       def threaded?; @threaded end

Protected Instance initialize_connection(connection)

Initialize a new connection to a client.

     # File lib/thin/backends/base.rb, line 123
123:         def initialize_connection(connection)
124:           connection.backend                 = self
125:           connection.app                     = @server.app
126:           connection.comm_inactivity_timeout = @timeout
127:           connection.threaded                = @threaded
128: 
129:           # We control the number of persistent connections by keeping
130:           # a count of the total one allowed yet.
131:           if @persistent_connection_count < @maximum_persistent_connections
132:             connection.can_persist!
133:             @persistent_connection_count += 1
134:           end
135: 
136:           @connections << connection
137:         end