<commit_before>module FreshConnection
  class ConnectionManager
    def slave_connection
      @slave_connections ||= {}
      @slave_connections[current_thread_id] ||= new_connection
    end

    def put_aside!
      if @slave_connections.present?
        @slave_connections.each_value{|c| c && c.disconnect! rescue nil}
      end
      @slave_connections = {}
    end

    private

    def new_connection
      ActiveRecord::Base.send("#{spec["adapter"]}_connection", spec)
    end

    def spec
      @spec ||= get_spec
    end

    def get_spec
      ret = ActiveRecord::Base.configurations[Rails.env]
      ret.merge(ret["slave"] || {})
    end

    def current_thread_id
      Thread.current.object_id
    end
  end
end<commit_msg>add mutex lock<commit_after>module FreshConnection
  class ConnectionManager
    def initialize
      @mutex = Mutex.new
    end

    def slave_connection
      @mutex.synchronize do
        @slave_connections ||= {}
        @slave_connections[current_thread_id] ||= new_connection
      end
    end

    def put_aside!
      @mutex.synchronize do
        if @slave_connections.present?
          @slave_connections.each_value{|c| c && c.disconnect! rescue nil}
        end
        @slave_connections = {}
      end
    end

    private

    def new_connection
      ActiveRecord::Base.send("#{spec["adapter"]}_connection", spec)
    end

    def spec
      @spec ||= get_spec
    end

    def get_spec
      ret = ActiveRecord::Base.configurations[Rails.env]
      ret.merge(ret["slave"] || {})
    end

    def current_thread_id
      Thread.current.object_id
    end
  end
end
{'fraction_non_alphanumeric': 0.09344059405940594, 'fraction_numerical': 0.0, 'mean_word_length': 2.5538461538461537, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6602210', 'n_tokens_mistral': 503, 'n_tokens_neox': 477, 'n_words': 102}