<commit_before>require "concurrent"
require "thread"

module Protobuf
  module ActiveRecord
    module Middleware
      class ConnectionManagementAsync
        START_MUTEX = ::Mutex.new

        def self.start_timed_task!
          if timed_task_started.false?
            START_MUTEX.synchronize do
              return if timed_task_started.true?

              timed_task = ::Concurrent::TimerTask.new(
                             :execution_interval => ::Protobuf::ActiveRecord.config.connection_reaping_interval,
                             :timeout_interval => ::Protobuf::ActiveRecord.config.connection_reaping_timeout_interval) do

                ::ActiveRecord::Base.clear_active_connections!
              end

              timed_task.execute
              timed_task_started.make_true
            end
          end
        end

        def self.timed_task_started
          if @timed_task_started.nil?
            @timed_task_started = ::Concurrent::AtomicBoolean.new(false)
          end

          @timed_task_started
        end

        def initialize(app)
          @app = app
        end

        def call(env)
          def call(env)
            @app.call(env)
          end

          self.class.start_timed_task!
          call(env)
        end

        timed_task_started
      end
    end
  end
end<commit_msg>Ensure we check connections in when done<commit_after>require "concurrent"
require "thread"

module Protobuf
  module ActiveRecord
    module Middleware
      class ConnectionManagementAsync
        START_MUTEX = ::Mutex.new

        def self.start_timed_task!
          if timed_task_started.false?
            START_MUTEX.synchronize do
              return if timed_task_started.true?

              timed_task = ::Concurrent::TimerTask.new(
                             :execution_interval => ::Protobuf::ActiveRecord.config.connection_reaping_interval,
                             :timeout_interval => ::Protobuf::ActiveRecord.config.connection_reaping_timeout_interval) do

                ::ActiveRecord::Base.clear_active_connections!
              end

              timed_task.execute
              timed_task_started.make_true
            end
          end
        end

        def self.timed_task_started
          if @timed_task_started.nil?
            @timed_task_started = ::Concurrent::AtomicBoolean.new(false)
          end

          @timed_task_started
        end

        def initialize(app)
          @app = app
        end

        def call(env)
          def call(env)
            ::ActiveRecord::Base.connection_pool.with_connection do
              @app.call(env)
            end
          end

          self.class.start_timed_task!
          call(env)
        end

        timed_task_started
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.06247755834829444, 'fraction_numerical': 0.0, 'mean_word_length': 1.7048543689320388, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7151335', 'n_tokens_mistral': 787, 'n_tokens_neox': 737, 'n_words': 138}