Class: Extlib::Pooling::Pool

### Home Classes Methods

### In Files

- lib/extlib/pooling.rb

### Parent

Object

### Methods

- ::new
- #delete
- #dispose
- #expired?
- #flush!
- #inspect
- #length
- #lock
- #new
- #release
- #scavenge_interval
- #size
- #wait

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Extlib
- Extlib::Assertions
- Extlib::ByteArray
- Extlib::Hook
- Extlib::Hook::ClassMethods
- Extlib::Inflection
- Extlib::LocalObjectSpace
- Extlib::Logger
- Extlib::Pooling
- Extlib::Pooling::InvalidResourceError
- Extlib::Pooling::Pool
- Extlib::SimpleSet
- Gem
- Gem::SourceIndex
- Gem::Specification
- LazyModule
- LazyModule::ClassMethods
- Array
- Class
- DateTime
- Dictionary
- FalseClass
- Hash
- LazyArray
- Mash
- Module
- NilClass
- Numeric
- Object
- ObjectSpace
- Pathname
- REXMLUtilityNode
- String
- Struct
- Symbol
- Time
- ToHashParser
- TrueClass
- VirtualFile

No matching classes.

# Extlib::Pooling::Pool

### Attributes

available[R]
used[R]

### Public Class Methods

new(max_size, resource, args) click to toggle source

    # File lib/extlib/pooling.rb, line 129
    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

### Public Instance Methods

delete(instance) click to toggle source

    # File lib/extlib/pooling.rb, line 191
    def delete(instance)
      lock.synchronize do
        instance.instance_variable_set(:@__pool, nil)
        @used.delete(instance.object_id)
        wait.signal
      end
      nil
    end

dispose() click to toggle source

    # File lib/extlib/pooling.rb, line 213
    def dispose
      flush!
      @resource.__pools.delete(@args)
      !Extlib::Pooling.pools.delete?(self).nil?
    end

expired?() click to toggle source

    # File lib/extlib/pooling.rb, line 219
    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!() click to toggle source

    # File lib/extlib/pooling.rb, line 209
    def flush!
      @available.pop.dispose until @available.empty?
    end

inspect() click to toggle source

    # File lib/extlib/pooling.rb, line 205
    def inspect
      "#<Extlib::Pooling::Pool<#{@resource.name}> available=#{@available.size} used=#{@used.size} size=#{@max_size}>"
    end

length() click to toggle source
Alias for: size
lock() click to toggle source

    # File lib/extlib/pooling.rb, line 142
    def lock
      @resource.__pool_lock
    end

new() click to toggle source

    # File lib/extlib/pooling.rb, line 154
    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) click to toggle source

    # File lib/extlib/pooling.rb, line 181
    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_interval() click to toggle source

    # File lib/extlib/pooling.rb, line 150
    def scavenge_interval
      @resource.scavenge_interval
    end

size() click to toggle source

    # File lib/extlib/pooling.rb, line 200
    def size
      @used.size + @available.size
    end

Also aliased as: length
wait() click to toggle source

    # File lib/extlib/pooling.rb, line 146
    def wait
      @resource.__pool_wait
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.