Sha256: a02ab49b0a8a3044cd6dd4e03203fe80acd11f54fb0f55991a25402ab0a5f382

Contents?: true

Size: 1.4 KB

Versions: 7

Compression:

Stored size: 1.4 KB

Contents

require 'thread'

module Typhoeus

  # The easy pool stores already initialized
  # easy handles for future use. This is useful
  # because creating them is expensive.
  #
  # @api private
  module Pool
    @mutex = Mutex.new
    @pid = Process.pid

    # Releases easy into the pool. The easy handle is
    # reset before it gets back in.
    #
    # @example Release easy.
    #   Typhoeus::Pool.release(easy)
    def self.release(easy)
      easy.reset
      @mutex.synchronize { easies << easy }
    end

    # Return an easy from the pool.
    #
    # @example Return easy.
    #   Typhoeus::Pool.get
    #
    # @return [ Ethon::Easy ] The easy.
    def self.get
      @mutex.synchronize do
        if @pid == Process.pid
          easies.pop
        else
          # Process has forked. Clear all easies to avoid sockets being
          # shared between processes.
          @pid = Process.pid
          easies.clear
          nil
        end
      end || Ethon::Easy.new
    end

    # Clear the pool
    def self.clear
      @mutex.synchronize { easies.clear }
    end

    # Use yielded easy, will be released automatically afterwards.
    #
    # @example Use easy.
    #   Typhoeus::Pool.with_easy do |easy|
    #     # use easy
    #   end
    def self.with_easy(&block)
      easy = get
      yield easy
    ensure
      release(easy) if easy
    end

    private

    def self.easies
      @easies ||= []
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
typhoeus-1.0.1 lib/typhoeus/pool.rb
typhoeus-1.0.0 lib/typhoeus/pool.rb
dwolla_swagger-1.0.6 vendor/bundle/ruby/2.2.0/gems/typhoeus-0.8.0/lib/typhoeus/pool.rb
typhoeus-0.8.0 lib/typhoeus/pool.rb
typhoeus-0.7.3 lib/typhoeus/pool.rb
typhoeus-0.7.2 lib/typhoeus/pool.rb
typhoeus-0.7.1 lib/typhoeus/pool.rb