Class: ExperellaProxy::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/experella-proxy/connection_manager.rb

Overview

The ConnectionManager is responsible for queueing and matching frontend Connection and BackendServer objects

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager

The constructor



13
14
15
16
17
# File 'lib/experella-proxy/connection_manager.rb', line 13

def initialize
  @connection_queue = [] # array queue of client connection objects
  @backend_queue = [] # array queue of available backend servers
  @backend_list = {} # list of all backend servers
end

Instance Method Details

#add_backend(backend) ⇒ Connection, Boolean

Adds a new BackendServer to the list and queues or connects it

Parameters:

Returns:

  • (Connection)

    a queued connection that would match the BackendServer

  • (Boolean)

    true if backend was added to list



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/experella-proxy/connection_manager.rb', line 78

def add_backend(backend)
  @backend_list[backend.name] = backend

  # check if any queued connections match new available backend
  conn = match_connections(backend)
  if conn
    # return matching connection
    # you should try to connect the new backend to this connection
    return conn
  else
    # queue new backend
    @backend_queue.push(backend)
  end
  true
end

#backend_available?(request) ⇒ BackendServer, ...

Matches Request to queued BackendServer

Removes first matching BackendServer from queue and returns it. It will requeue the BackendServer instantly, if BackendServer#workload is smaller than BackendServer#concurrency

Queues Request#conn if no available BackendServer matches

Returns false if no registered BackendServer matches

Returns:

  • (BackendServer)

    first matching BackendServer from the queue

  • (Symbol)

    :queued if Connection was queued

  • (Boolean)

    false if no registered Backend matches the Request



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/experella-proxy/connection_manager.rb', line 32

def backend_available?(request)
  @backend_queue.each do |backend|
    if backend.accept?(request)
      # connect backend to requests connection if request matches
      backend.workload += 1
      ret = @backend_queue.delete(backend)
      # requeue backend if concurrency isnt maxed
      @backend_queue.push(backend) if backend.workload < backend.concurrency
      return ret
    end
  end
  if match_any_backend?(request)
    # push requests connection on queue if no backend was connected
    @connection_queue.push(request.conn)
    :queued
  else
    false
  end
end

#backend_countint

returns the count of the registeredBackendServers

Returns:

  • (int)


126
127
128
# File 'lib/experella-proxy/connection_manager.rb', line 126

def backend_count
  @backend_list.size
end

#backend_queue_countint

returns the count of the currently queued BackendServers

Returns:

  • (int)


119
120
121
# File 'lib/experella-proxy/connection_manager.rb', line 119

def backend_queue_count
  @backend_queue.size
end

#connection_countint

returns the count of the currently queued connections

Returns:

  • (int)


133
134
135
# File 'lib/experella-proxy/connection_manager.rb', line 133

def connection_count
  @connection_queue.size
end

#free_backend(backend) ⇒ NilClass

Called by a ExperellaProxy::Connection when the BackendServer is done.

Connects backend to a matching queued ExperellaProxy::Connection or pushes server back on queue

Parameters:

Returns:

  • (NilClass)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/experella-proxy/connection_manager.rb', line 58

def free_backend(backend)
  # check if any queued connections match new available backend
  conn = match_connections(backend)
  if conn
    # return matching connection
    # you should try to connect the new backend to this connection
    return conn
  else
    # push free backend on queue if it wasn't used for a queued conn or is already queued (concurrency)
    @backend_queue.push(backend) if @backend_list.include?(backend.name) && !@backend_queue.include?(backend)
    backend.workload -= 1
  end
  nil
end

#free_connection(conn) ⇒ Object

Removes a connection from the connection_queue

Parameters:



112
113
114
# File 'lib/experella-proxy/connection_manager.rb', line 112

def free_connection(conn)
  @connection_queue.delete(conn)
end

#remove_backend(backend) ⇒ Boolean

Removes a BackendServer from list and queue

Parameters:

Returns:

  • (Boolean)

    true if a backend was removed, else returns false



98
99
100
101
102
103
104
105
106
107
# File 'lib/experella-proxy/connection_manager.rb', line 98

def remove_backend(backend)
  ret = @backend_list.delete(backend.name)
  @backend_queue.delete(backend)

  if ret
    true
  else
    false
  end
end