Class: FaradayPersistentExcon::ConnectionPools

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday_persistent_excon/connection_pools.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.__poolsObject

Returns the value of attribute __pools.



4
5
6
# File 'lib/faraday_persistent_excon/connection_pools.rb', line 4

def __pools
  @__pools
end

Class Method Details

.connection_pool_for(url) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/faraday_persistent_excon/connection_pools.rb', line 27

def connection_pool_for(url)
  config = FaradayPersistentExcon.connection_pools[url]

  if config
    self.__pools.fetch_or_store(url) do
      ::ConnectionPool.new(config) do
        Connection.new(
          excon: ::Excon.new(
            url,
            {
              persistent: true,
              thread_safe_sockets: false
            }.merge(config.fetch(:connection_options, {}))
          ),
          idle_timeout: config.fetch(:idle_timeout, FaradayPersistentExcon.idle_timeout)
        )
      end
    end
  end
end

.with_connection_for(url, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/faraday_persistent_excon/connection_pools.rb', line 9

def with_connection_for(url, &block)
  pool = self.connection_pool_for(url)

  if pool
    pool.with do |conn|
      begin
        conn.reset if conn.expired?
        block.call(conn.excon)
      ensure
        conn.used!
      end
    end
  else
    # No pool configured.  Use normal connection
    block.call(::Excon.new(url, persistent: false))
  end
end