summaryrefslogtreecommitdiff
path: root/lib/net/hippie/connection_pool.rb
blob: 2e220b7d14928bab7f33f027c52294752f4631b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

module Net
  module Hippie
    # Thread-safe connection pool with LRU eviction.
    class ConnectionPool
      def initialize(max_size: 100, dns_ttl: 300)
        @max_size = max_size
        @dns_ttl = dns_ttl
        @connections = {}
        @monitor = Monitor.new
      end

      def checkout(key, &block)
        reuse(key) || create(key, &block)
      end

      def close_all
        @monitor.synchronize do
          @connections.each_value(&:close)
          @connections.clear
        end
      end

      private

      def reuse(key)
        @monitor.synchronize do
          return nil unless @connections.key?(key)

          conn = @connections.delete(key)
          return @connections[key] = conn unless conn.stale?(@dns_ttl)

          conn.close
          nil
        end
      end

      def create(key)
        conn = yield
        @monitor.synchronize do
          existing = reuse(key)
          if existing
            conn.close
            return existing
          end
          evict_lru if @connections.size >= @max_size
          @connections[key] = conn
        end
      end

      def evict_lru
        key, conn = @connections.first
        conn.close
        @connections.delete(key)
      end
    end
  end
end