Class: Restify::Adapter::PooledEM::Pool

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/restify/adapter/pooled_em.rb

Overview

This class maintains a pool of connection objects, grouped by origin, and ensures limits for total parallel requests and per-origin requests.

It does so by maintaining a list of already open, reusable connections. When any of them are checked out for usage, it counts the usages to prevent constraints being broken.

Defined Under Namespace

Classes: Deferrable

Instance Method Summary collapse

Methods included from Logging

#_fmt, #_log_prefix, #debug, #logger

Constructor Details

#initialize(size: 32, per_host: 6, connect_timeout: 2, inactivity_timeout: 10) ⇒ Pool

Returns a new instance of Pool.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/restify/adapter/pooled_em.rb', line 20

def initialize(size: 32, per_host: 6, connect_timeout: 2, inactivity_timeout: 10)
  @size = size
  @per_host = per_host
  @connect_timeout = connect_timeout
  @inactivity_timeout = inactivity_timeout

  @host = Hash.new {|h, k| h[k] = 0 }
  @available = []
  @queue = []
  @used = 0
end

Instance Method Details

#get(request, timeout: 2) ⇒ Deferrable<Request>

Request a connection from the pool.

Attempts to checkout a reusable connection from the pool (or create a new one). If any of the limits have been reached, the request will be put onto a queue until other connections are released.

Returns a Deferrable that succeeds with a connection instance once a connection has been checked out (usually immediately).

Returns:



43
44
45
46
47
48
49
50
51
# File 'lib/restify/adapter/pooled_em.rb', line 43

def get(request, timeout: 2)
  defer = Deferrable.new(request)
  defer.timeout(timeout, :timeout)
  defer.errback { @queue.delete(defer) }

  checkout(defer)

  defer
end

#release(conn) ⇒ void Also known as: <<

This method returns an undefined value.

Return a connection to the pool.

If there are requests in the queue (due to one of the limits having been reached), they will be given an attempt to use the released connection.

If no requests are queued, the connection will be held for reuse by a subsequent request.



64
65
66
67
68
69
70
71
72
73
# File 'lib/restify/adapter/pooled_em.rb', line 64

def release(conn)
  @available.unshift(conn) if @available.size < @size
  @used -= 1 if @used > 0

  logger.debug do
    "[#{conn.uri}] Released to pool (#{@available.size}/#{@used}/#{size})"
  end

  checkout(@queue.shift) if @queue.any? # checkout next waiting defer
end

#remove(conn) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/restify/adapter/pooled_em.rb', line 77

def remove(conn)
  close(conn)

  logger.debug do
    "[#{conn.uri}] Removed from pool (#{@available.size}/#{@used}/#{size})"
  end

  checkout(@queue.shift) if @queue.any? # checkout next waiting defer
end

#sizeInteger

Determine the number of connections in the pool.

This takes into account both reusable (idle) and used connections.

Returns:

  • (Integer)


93
94
95
# File 'lib/restify/adapter/pooled_em.rb', line 93

def size
  @available.size + @used
end