Class: ConnectionPool::TimedStack

Inherits:
Object
  • Object
show all
Defined in:
lib/connection_pool/timed_stack.rb

Overview

Examples:

ts = TimedStack.new(1) { MyConnection.new }

# fetch a connection
conn = ts.pop

# return a connection
ts.push conn

conn = ts.pop
ts.pop timeout: 5
#=> raises Timeout::Error after 5 seconds

Instance Method Summary collapse

Constructor Details

#initialize(size = 0, &block) ⇒ TimedStack

Creates a new pool with size connections that are created from the given block.



35
36
37
38
39
40
41
42
43
# File 'lib/connection_pool/timed_stack.rb', line 35

def initialize(size = 0, &block)
  @create_block = block
  @created = 0
  @que = []
  @max = size
  @mutex = Mutex.new
  @resource = ConditionVariable.new
  @shutdown_block = nil
end

Instance Method Details

#discard!(obj) ⇒ Object

Indicates that a connection isn’t coming back, allowing a new one to be created to replace it.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/connection_pool/timed_stack.rb', line 124

def discard!(obj)
  @mutex.synchronize do
    if @shutdown_block
      @shutdown_block.call(obj)
    else
      # try to shut down the connection before throwing it away
      if obj.respond_to?(:close) # Dalli::Client
        obj.close rescue nil
      elsif obj.respond_to?(:disconnect!) # Redis
        obj.disconnect! rescue nil
      end
      @created -= 1
    end

    @resource.broadcast
  end
end

#empty?Boolean

Returns true if there are no available connections.

Returns:

  • (Boolean)


109
110
111
# File 'lib/connection_pool/timed_stack.rb', line 109

def empty?
  (@created - @que.length) >= @max
end

#lengthObject

The number of connections available on the stack.



116
117
118
# File 'lib/connection_pool/timed_stack.rb', line 116

def length
  @max - @created + @que.length
end

#pop(timeout = 0.5, options = {}) ⇒ Object

Retrieves a connection from the stack. If a connection is available it is immediately returned. If no connection is available within the given timeout a Timeout::Error is raised.

:timeout is the only checked entry in options and is preferred over the timeout argument (which will be removed in a future release). Other options may be used by subclasses that extend TimedStack.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/connection_pool/timed_stack.rb', line 71

def pop(timeout = 0.5, options = {})
  options, timeout = timeout, 0.5 if Hash === timeout
  timeout = options.fetch :timeout, timeout

  deadline = Time.now + timeout
  @mutex.synchronize do
    loop do
      raise ConnectionPool::PoolShuttingDownError if @shutdown_block
      return fetch_connection(options) if connection_stored?(options)

      connection = try_create(options)
      return connection if connection

      to_wait = deadline - Time.now
      raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0
      @resource.wait(@mutex, to_wait)
    end
  end
end

#push(obj, options = {}) ⇒ Object Also known as: <<

Returns obj to the stack. options is ignored in TimedStack but may be used by subclasses that extend TimedStack.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/connection_pool/timed_stack.rb', line 49

def push(obj, options = {})
  @mutex.synchronize do
    if @shutdown_block
      @shutdown_block.call(obj)
    else
      store_connection obj, options
    end

    @resource.broadcast
  end
end

#shutdown(&block) ⇒ Object

Shuts down the TimedStack which prevents connections from being checked out. The block is called once for each connection on the stack.

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
# File 'lib/connection_pool/timed_stack.rb', line 95

def shutdown(&block)
  raise ArgumentError, "shutdown must receive a block" unless block_given?

  @mutex.synchronize do
    @shutdown_block = block
    @resource.broadcast

    shutdown_connections
  end
end