Class: EzPool::TimedStack

Inherits:
Object
  • Object
show all
Defined in:
lib/ezpool/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(connection_manager, size = 0) ⇒ TimedStack

Creates a new pool with size connections that are created by constructing the given connection_wrapper class



37
38
39
40
41
42
43
44
45
# File 'lib/ezpool/timed_stack.rb', line 37

def initialize(connection_manager, size = 0)
  @created = 0
  @que = []
  @max = size
  @mutex = Mutex.new
  @resource = ConditionVariable.new
  @connection_manager = connection_manager
  @shutting_down = false
end

Instance Method Details

#abandon(connection_wrapper) ⇒ Object

Mark a connection as abandoned so that it cannot be used again. Will call the pre-configured shutdown proc, if provided.



97
98
99
100
101
102
# File 'lib/ezpool/timed_stack.rb', line 97

def abandon(connection_wrapper)
  @mutex.synchronize do
    connection_wrapper.shutdown!
    @created -= 1
  end
end

#add_oneObject

Add one connection to the queue

Returns true iff a connection was successfully created



143
144
145
146
147
148
149
150
151
# File 'lib/ezpool/timed_stack.rb', line 143

def add_one
  connection = try_create
  if connection.nil?
    false
  else
    push(connection)
    true
  end
end

#empty?Boolean

Returns true if there are no available connections.

Returns:

  • (Boolean)


121
122
123
# File 'lib/ezpool/timed_stack.rb', line 121

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

#fillObject

Pre-create all possible connections



134
135
136
137
# File 'lib/ezpool/timed_stack.rb', line 134

def fill
  while add_one
  end
end

#lengthObject

The number of connections available on the stack.



128
129
130
# File 'lib/ezpool/timed_stack.rb', line 128

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.



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

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

  deadline = EzPool.monotonic_time + timeout
  @mutex.synchronize do
    loop do
      raise EzPool::PoolShuttingDownError if @shutting_down
      return fetch_connection(options) if connection_stored?(options)

      connection = try_create(options)
      return connection if connection

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

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

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



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ezpool/timed_stack.rb', line 51

def push(wrapper, options = {})
  @mutex.synchronize do
    if @shutting_down
      wrapper.shutdown!
    else
      store_connection wrapper, options
    end

    @resource.broadcast
  end
end

#shutdownObject

Shuts down the TimedStack which prevents connections from being checked out. Calls the shutdown program specified in the EzPool initializer



109
110
111
112
113
114
115
116
# File 'lib/ezpool/timed_stack.rb', line 109

def shutdown()
  @mutex.synchronize do
    @shutting_down = true
    @resource.broadcast

    shutdown_connections
  end
end