Class: Extlib::Pooling::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/extlib-0.9.9/lib/extlib/pooling.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size, resource, args) ⇒ Pool

Returns a new instance of Pool.

Raises:

  • (ArgumentError)


125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 125

def initialize(max_size, resource, args)
  raise ArgumentError.new("+max_size+ should be a Fixnum but was #{max_size.inspect}") unless Fixnum === max_size
  raise ArgumentError.new("+resource+ should be a Class but was #{resource.inspect}") unless Class === resource

  @max_size = max_size
  @resource = resource
  @args = args

  @available = []
  @used      = {}
  Extlib::Pooling::append_pool(self)
end

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



122
123
124
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 122

def available
  @available
end

#usedObject (readonly)

Returns the value of attribute used.



123
124
125
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 123

def used
  @used
end

Instance Method Details

#delete(instance) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 187

def delete(instance)
  lock.synchronize do
    instance.instance_variable_set(:@__pool, nil)
    @used.delete(instance.object_id)
    wait.signal
  end
  nil
end

#disposeObject



209
210
211
212
213
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 209

def dispose
  flush!
  @resource.__pools.delete(@args)
  !Extlib::Pooling::pools.delete?(self).nil?
end

#expired?Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
221
222
223
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 215

def expired?
  @available.each do |instance|
    if Extlib.exiting || instance.instance_variable_get(:@__allocated_in_pool) + Extlib::Pooling.scavenger_interval <= (Time.now + 0.02)
      instance.dispose
      @available.delete(instance)
    end
  end
  size == 0
end

#flush!Object



205
206
207
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 205

def flush!
  @available.pop.dispose until @available.empty?
end

#inspectObject



201
202
203
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 201

def inspect
  "#<Extlib::Pooling::Pool<#{@resource.name}> available=#{@available.size} used=#{@used.size} size=#{@max_size}>"
end

#lockObject



138
139
140
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 138

def lock
  @resource.__pool_lock
end

#newObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 150

def new
  instance = nil
  begin
    lock.synchronize do
      if @available.size > 0
        instance = @available.pop
        @used[instance.object_id] = instance
      elsif @used.size < @max_size
        instance = @resource.__new(*@args)
        raise InvalidResourceError.new("#{@resource} constructor created a nil object") if instance.nil?
        raise InvalidResourceError.new("#{instance} is already part of the pool") if @used.include? instance
        instance.instance_variable_set(:@__pool, self)
        instance.instance_variable_set(:@__allocated_in_pool, Time.now)
        @used[instance.object_id] = instance
      else
        # Wait for another thread to release an instance.
        # If we exhaust the pool and don't release the active instance,
        # we'll wait here forever, so it's *very* important to always
        # release your services and *never* exhaust the pool within
        # a single thread.
        wait.wait(lock)
      end
    end
  end until instance
  instance
end

#release(instance) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 177

def release(instance)
  lock.synchronize do
    instance.instance_variable_set(:@__allocated_in_pool, Time.now)
    @used.delete(instance.object_id)
    @available.push(instance)
    wait.signal
  end
  nil
end

#scavenge_intervalObject



146
147
148
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 146

def scavenge_interval
  @resource.scavenge_interval
end

#sizeObject Also known as: length



196
197
198
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 196

def size
  @used.size + @available.size
end

#waitObject



142
143
144
# File 'lib/gems/extlib-0.9.9/lib/extlib/pooling.rb', line 142

def wait
  @resource.__pool_wait
end