<commit_before>require_relative 'mgrep_client'

module RI::Population::Mgrep
  class ThreadedClient
    def initialize(host, port, threads = 1)
      @host = host
      @port = port
      @pool = []
      (threads * 1.5).to_i.times do
        @pool << RI::Population::Mgrep::Client.new(@host, @port)
      end
      @assigned = {}
    end

    def close
      pooled_client.close()
    end

    def close_all
      @pool.each {|c| c.close}
      @assigned.values.each {|c| c.close}
    end

    def annotate(text, longword, wholeword = nil)
      pooled_client.annotate(text, longword, wholeword)
    end

    def pooled_client
      client = @assigned[Thread.current.object_id]
      client ||= @assigned[Thread.current.object_id] = @pool.pop
      client ||= @assigned[Thread.current.object_id] = RI::Population::Mgrep::Client.new(@host, @port)
      client
    end
  end
end<commit_msg>True pooling (before it would just keep creating new clients)<commit_after>require_relative 'mgrep_client'

module RI::Population::Mgrep
  class ThreadedClient
    def initialize(host, port, threads = 1)
      @host = host
      @port = port
      @pool = []
      threads.times do
        @pool << RI::Population::Mgrep::Client.new(@host, @port)
      end
    end

    def close
      raise NoMethodError, "ThreadedClient uses a pool, cannot close individual connections. Use #close_all if that's really what you want."
    end

    def close_all
      @pool.each {|c| c.close}
    end

    def annotate(text, longword, wholeword = nil)
      annotation = nil
      pooled_client do |client|
        annotation = client.annotate(text, longword, wholeword)
      end
      annotation
    end

    def pooled_client(&block)
      puts @pool.length
      client = @pool.pop
      client ||= RI::Population::Mgrep::Client.new(@host, @port)
      yield client if block_given?
      @pool.push(client)
    end
  end
end
{'fraction_non_alphanumeric': 0.11035207566999475, 'fraction_numerical': 0.0021019442984760903, 'mean_word_length': 2.71875, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6267103', 'n_tokens_mistral': 632, 'n_tokens_neox': 592, 'n_words': 155}