Class: UState::Server::Backends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ustate/server/backends/base.rb

Overview

Largely stolen from Thin

Direct Known Subclasses

TCP

Constant Summary collapse

TIMEOUT =
5
MAXIMUM_CONNECTIONS =
1024
MAX_CONNECTIONS =
512

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
19
# File 'lib/ustate/server/backends/base.rb', line 14

def initialize(opts = {})
  @connections = []
  @server = opts[:server]
  @timeout = opts[:timeout] || TIMEOUT
  @maximum_connections = opts[:maximum_connections] || MAXIMUM_CONNECTIONS
end

Instance Attribute Details

#maximum_connectionsObject

Returns the value of attribute maximum_connections.



10
11
12
# File 'lib/ustate/server/backends/base.rb', line 10

def maximum_connections
  @maximum_connections
end

#serverObject

Returns the value of attribute server.



8
9
10
# File 'lib/ustate/server/backends/base.rb', line 8

def server
  @server
end

#ssl=(value) ⇒ Object (writeonly)

Sets the attribute ssl

Parameters:

  • value

    the value to set the attribute ssl to.



11
12
13
# File 'lib/ustate/server/backends/base.rb', line 11

def ssl=(value)
  @ssl = value
end

#ssl_options=(value) ⇒ Object (writeonly)

Sets the attribute ssl_options

Parameters:

  • value

    the value to set the attribute ssl_options to.



11
12
13
# File 'lib/ustate/server/backends/base.rb', line 11

def ssl_options=(value)
  @ssl_options = value
end

#timeoutObject

Returns the value of attribute timeout.



9
10
11
# File 'lib/ustate/server/backends/base.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#closeObject

Free up resources used by the backend



65
66
# File 'lib/ustate/server/backends/base.rb', line 65

def close
end

#configObject

Configure backend



57
58
59
60
61
62
# File 'lib/ustate/server/backends/base.rb', line 57

def config
  EventMachine.epoll

  @maximum_connections =
    EventMachine.set_descriptor_table_size(@maximum_connections)
end

#connection_finished(connection) ⇒ Object

Called by a connection when it’s unbound



73
74
75
76
77
78
# File 'lib/ustate/server/backends/base.rb', line 73

def connection_finished(connection)
  @connections.delete connection

  # Finalize graceful stop if there's no more active connections.
  stop! if @stopping and @connections.empty?
end

#empty?Boolean

No connections?

Returns:

  • (Boolean)


81
82
83
# File 'lib/ustate/server/backends/base.rb', line 81

def empty?
  @connections.empty?
end

#running?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/ustate/server/backends/base.rb', line 68

def running?
  @running
end

#sizeObject

No of connections



86
87
88
# File 'lib/ustate/server/backends/base.rb', line 86

def size
  @connections.size
end

#ssl?Boolean

Returns:

  • (Boolean)


12
# File 'lib/ustate/server/backends/base.rb', line 12

def ssl?; @ssl; end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ustate/server/backends/base.rb', line 21

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

#stopObject

Graceful stop



37
38
39
40
41
42
43
44
# File 'lib/ustate/server/backends/base.rb', line 37

def stop
  @running = false
  @stopping = true

  # Stop accepting connections
  disconnect
  stop! if @connections.empty?
end

#stop!Object

Force stop



47
48
49
50
51
52
53
54
# File 'lib/ustate/server/backends/base.rb', line 47

def stop!
  @running = false
  @stopping = false

  EventMachine.stop if EventMachine.reactor_running?
  @connections.each { |connection| connection.close_connection }
  close
end