Class: Extlib::Pooling::Pool

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

Instance Method Summary collapse

Constructor Details

#initialize(max_size, resource, args) ⇒ Pool

Returns a new instance of Pool.

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/extlib/pooling.rb', line 109

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 = []
  @reserved_count = 0
end

Instance Method Details

#delete(instance) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/extlib/pooling.rb', line 160

def delete(instance)
  lock.synchronize do
    instance.instance_variable_set(:@__pool, nil)
    @reserved_count -= 1
  end
  nil
end

#disposeObject



181
182
183
184
185
# File 'lib/extlib/pooling.rb', line 181

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

#flush!Object



177
178
179
# File 'lib/extlib/pooling.rb', line 177

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

#inspectObject



173
174
175
# File 'lib/extlib/pooling.rb', line 173

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

#lockObject



121
122
123
# File 'lib/extlib/pooling.rb', line 121

def lock
  @resource.__pool_lock
end

#newObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/extlib/pooling.rb', line 129

def new
  instance = nil

  lock.synchronize do
    instance = acquire
  end

  Extlib::Pooling::append_pool(self)

  if instance.nil?
    # Account for the current thread, and the pool scavenger.
    if ThreadGroup::Default.list.size == 2 && @reserved_count >= @max_size
      raise ThreadStopError.new(size)
    else
      sleep(0.05)
      new
    end
  else
    instance
  end
end

#release(instance) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/extlib/pooling.rb', line 151

def release(instance)
  lock.synchronize do
    instance.instance_variable_set(:@__pool, nil)
    @reserved_count -= 1
    @available.push(instance)
  end
  nil
end

#reserved_countObject

Disabled temporarily.

def expired?

lock.synchronize do
  @available.each do |instance|
    if instance.instance_variable_get(:@__allocated_in_pool) + scavenge_interval < Time.now
      instance.dispose
      @available.delete(instance)
    end
  end

  size == 0
end

end



202
203
204
# File 'lib/extlib/pooling.rb', line 202

def reserved_count
  @reserved_count
end

#scavenge_intervalObject



125
126
127
# File 'lib/extlib/pooling.rb', line 125

def scavenge_interval
  @resource.scavenge_interval
end

#sizeObject Also known as: length



168
169
170
# File 'lib/extlib/pooling.rb', line 168

def size
  @available.size + @reserved_count
end