Class: Thin::Backends::Base

Inherits:
Object
  • Object
show all
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

  • monitoring 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

SwiftiplyClient, TcpServer, UnixServer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Attribute Details

#maximum_connectionsObject

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_connectionsObject

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_epollObject

Disable the use of epoll under Linux



45
46
47
# File 'lib/thin/backends/base.rb', line 45

def no_epoll
  @no_epoll
end

#persistent_connection_countObject

Number of persistent connections currently opened



42
43
44
# File 'lib/thin/backends/base.rb', line 42

def persistent_connection_count
  @persistent_connection_count
end

#serverObject

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.



38
39
40
# File 'lib/thin/backends/base.rb', line 38

def ssl=(value)
  @ssl = value
end

#ssl_options=(value) ⇒ Object (writeonly)

Allow using SSL in the backend.



38
39
40
# File 'lib/thin/backends/base.rb', line 38

def ssl_options=(value)
  @ssl_options = value
end

#threaded=(value) ⇒ Object (writeonly)

Allow using threads in the backend.



34
35
36
# File 'lib/thin/backends/base.rb', line 34

def threaded=(value)
  @threaded = value
end

#threadpool_sizeObject

allows setting of the eventmachine threadpool size



27
28
29
# File 'lib/thin/backends/base.rb', line 27

def threadpool_size
  @threadpool_size
end

#timeoutObject

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

#closeObject

Free up resources used by the backend.



112
113
# File 'lib/thin/backends/base.rb', line 112

def close
end

#configObject

Configure the backend. This method will be called before droping superuser privileges, so you can do crazy stuff that require godlike powers here.



101
102
103
104
105
106
107
108
109
# 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

#connection_finished(connection) ⇒ Object

Called by a connection when it’s unbinded.



125
126
127
128
129
130
131
# 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

#empty?Boolean

Returns true if no active connection.

Returns:

  • (Boolean)


134
135
136
# File 'lib/thin/backends/base.rb', line 134

def empty?
  @connections.empty?
end

#running?Boolean

Returns true if the backend is connected and running.

Returns:

  • (Boolean)


116
117
118
# File 'lib/thin/backends/base.rb', line 116

def running?
  @running
end

#sizeObject

Number of active connections.



139
140
141
# File 'lib/thin/backends/base.rb', line 139

def size
  @connections.size
end

#ssl?Boolean

Returns:

  • (Boolean)


39
# File 'lib/thin/backends/base.rb', line 39

def ssl?; @ssl end

#startObject

Start the backend and connect it.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

#started_reactor?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/thin/backends/base.rb', line 120

def started_reactor?
  @started_reactor
end

#stopObject

Stop of the backend from accepting new connections.



78
79
80
81
82
83
84
85
86
87
# 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

#stop!Object

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



90
91
92
93
94
95
96
97
# 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

#threaded?Boolean

Returns:

  • (Boolean)


35
# File 'lib/thin/backends/base.rb', line 35

def threaded?; @threaded end