<commit_before>require 'perpetuity/postgres/connection'
require 'thread'

module Perpetuity
  class Postgres
    class ConnectionPool
      attr_reader :connections, :size

      def initialize options={}
        @connections = Queue.new
        @size = options.delete(:pool_size) { 5 }
        @size.times do
          connections << Connection.new(options)
        end
      end

      def lend_connection
        if block_given?
          connection = connections.pop
          result = yield connection
          connections << connection
          result
        end
      end

      def execute sql
        lend_connection do |connection|
          connection.execute sql
        end
      end

      def tables
        lend_connection do |connection|
          connection.tables
        end
      end
    end
  end
end<commit_msg>Return the connection to the pool after exceptions<commit_after>require 'perpetuity/postgres/connection'
require 'thread'

module Perpetuity
  class Postgres
    class ConnectionPool
      attr_reader :connections, :size

      def initialize options={}
        @connections = Queue.new
        @size = options.delete(:pool_size) { 5 }
        @size.times do
          connections << Connection.new(options)
        end
      end

      def lend_connection
        if block_given?
          connection = connections.pop
          yield connection
        end
      ensure
        connections << connection
      end

      def execute sql
        lend_connection do |connection|
          connection.execute sql
        end
      end

      def tables
        lend_connection do |connection|
          connection.tables
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.052476415094339625, 'fraction_numerical': 0.0011792452830188679, 'mean_word_length': 1.8377926421404682, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3094982', 'n_tokens_mistral': 434, 'n_tokens_neox': 415, 'n_words': 129}