Class: Thin::Backends::Base
- Inherits:
-
Object
- Object
- Thin::Backends::Base
- Defined in:
- lib/thin/backends/base.rb
Overview
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#maximum_connections ⇒ Object
Maximum number of file or socket descriptors that the server may open.
-
#maximum_persistent_connections ⇒ Object
Maximum number of connections that can be persistent.
-
#no_epoll ⇒ Object
Disable the use of epoll under Linux.
-
#persistent_connection_count ⇒ Object
Number of persistent connections currently opened.
-
#server ⇒ Object
Server serving the connections throught the backend.
-
#ssl ⇒ Object
writeonly
Allow using SSL in the backend.
-
#ssl_options ⇒ Object
writeonly
Allow using SSL in the backend.
-
#threaded ⇒ Object
writeonly
Allow using threads in the backend.
-
#timeout ⇒ Object
Maximum time for incoming data to arrive.
Instance Method Summary collapse
-
#close ⇒ Object
Free up resources used by the backend.
-
#config ⇒ Object
Configure the backend.
-
#connection_finished(connection) ⇒ Object
Called by a connection when it’s unbinded.
-
#connections_list ⇒ Object
connection list.
-
#empty? ⇒ Boolean
Returns
true
if no active connection. -
#initialize ⇒ Base
constructor
A new instance of Base.
-
#running? ⇒ Boolean
Returns
true
if the backend is connected and running. -
#size ⇒ Object
Number of active connections.
- #ssl? ⇒ Boolean
-
#start ⇒ Object
Start the backend and connect it.
-
#stop ⇒ Object
Stop of the backend from accepting new connections.
-
#stop! ⇒ Object
Force stop of the backend NOW, too bad for the current connections.
- #threaded? ⇒ Boolean
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
40 41 42 43 44 45 46 47 48 49 |
# 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 |
Instance Attribute Details
#maximum_connections ⇒ Object
Maximum number of file or socket descriptors that the server may open.
21 22 23 |
# File 'lib/thin/backends/base.rb', line 21 def maximum_connections @maximum_connections end |
#maximum_persistent_connections ⇒ Object
Maximum number of connections that can be persistent
24 25 26 |
# File 'lib/thin/backends/base.rb', line 24 def maximum_persistent_connections @maximum_persistent_connections end |
#no_epoll ⇒ Object
Disable the use of epoll under Linux
38 39 40 |
# File 'lib/thin/backends/base.rb', line 38 def no_epoll @no_epoll end |
#persistent_connection_count ⇒ Object
Number of persistent connections currently opened
35 36 37 |
# File 'lib/thin/backends/base.rb', line 35 def persistent_connection_count @persistent_connection_count end |
#server ⇒ Object
Server serving the connections throught the backend
15 16 17 |
# File 'lib/thin/backends/base.rb', line 15 def server @server end |
#ssl=(value) ⇒ Object (writeonly)
Allow using SSL in the backend.
31 32 33 |
# File 'lib/thin/backends/base.rb', line 31 def ssl=(value) @ssl = value end |
#ssl_options=(value) ⇒ Object (writeonly)
Allow using SSL in the backend.
31 32 33 |
# File 'lib/thin/backends/base.rb', line 31 def (value) = value end |
#threaded=(value) ⇒ Object (writeonly)
Allow using threads in the backend.
27 28 29 |
# File 'lib/thin/backends/base.rb', line 27 def threaded=(value) @threaded = value end |
#timeout ⇒ Object
Maximum time for incoming data to arrive
18 19 20 |
# File 'lib/thin/backends/base.rb', line 18 def timeout @timeout end |
Instance Method Details
#close ⇒ Object
Free up resources used by the backend.
100 101 |
# File 'lib/thin/backends/base.rb', line 100 def close end |
#config ⇒ Object
Configure the backend. This method will be called before droping superuser privileges, so you can do crazy stuff that require godlike powers here.
89 90 91 92 93 94 95 96 97 |
# File 'lib/thin/backends/base.rb', line 89 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) ⇒ Object
Called by a connection when it’s unbinded.
109 110 111 112 113 114 115 |
# File 'lib/thin/backends/base.rb', line 109 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 |
#connections_list ⇒ Object
connection list.
128 129 130 |
# File 'lib/thin/backends/base.rb', line 128 def connections_list @connections end |
#empty? ⇒ Boolean
Returns true
if no active connection.
118 119 120 |
# File 'lib/thin/backends/base.rb', line 118 def empty? @connections.empty? end |
#running? ⇒ Boolean
Returns true
if the backend is connected and running.
104 105 106 |
# File 'lib/thin/backends/base.rb', line 104 def running? @running end |
#size ⇒ Object
Number of active connections.
123 124 125 |
# File 'lib/thin/backends/base.rb', line 123 def size @connections.size end |
#ssl? ⇒ Boolean
32 |
# File 'lib/thin/backends/base.rb', line 32 def ssl?; @ssl end |
#start ⇒ Object
Start the backend and connect it.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# 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 ⇒ Object
Stop of the backend from accepting new connections.
68 69 70 71 72 73 74 75 |
# File 'lib/thin/backends/base.rb', line 68 def stop @running = false @stopping = true # Do not accept anymore connection disconnect stop! if @connections.empty? end |
#stop! ⇒ Object
Force stop of the backend NOW, too bad for the current connections.
78 79 80 81 82 83 84 85 |
# File 'lib/thin/backends/base.rb', line 78 def stop! @running = false @stopping = false EventMachine.stop if EventMachine.reactor_running? @connections.each { |connection| connection.close_connection } close end |
#threaded? ⇒ Boolean
28 |
# File 'lib/thin/backends/base.rb', line 28 def threaded?; @threaded end |