<commit_before>require 'timeout'

module BackgroundQueue::ClientLib
  #A connection to a backend queue server
  #Handles sending the command to the server and receiving the reply
  #For now connections are not pooled/reused
  class Connection
    def initialize(client, server)
      @client = client
      @server = server
      @socket = nil
    end
    
    #send a command to the server
    def send_command(command)
      check_connected
      send_with_header(command.to_buf)
      response = receive_with_header
      BackgroundQueue::Command.from_buf(response)
    end
    
    private
    
    def connect
      begin
        Timeout::timeout(3) {
          @socket = TCPSocket.open(@server.host, @server.port)
          true
        }
      rescue Timeout::Error
        raise BackgroundQueue::ClientLib::ConnectionError, "Timeout Connecting to #{@server.host}:#{@server.port}"
      rescue Exception=>e
        raise BackgroundQueue::ClientLib::ConnectionError, "Error Connecting to #{@server.host}:#{@server.port}: #{e.message}"
      end
    end
    
    def check_connected
      connect if @socket.nil?<commit_msg>make sure sockets dont linger (resource starvation)<commit_after>require 'timeout'

module BackgroundQueue::ClientLib
  #A connection to a backend queue server
  #Handles sending the command to the server and receiving the reply
  #For now connections are not pooled/reused
  class Connection
    def initialize(client, server)
      @client = client
      @server = server
      @socket = nil
    end
    
    #send a command to the server
    def send_command(command)
      check_connected
      send_with_header(command.to_buf)
      response = receive_with_header
      BackgroundQueue::Command.from_buf(response)
    ensure
      begin
        @socket.close unless @socket.nil?
      rescue Exception=>e
        #dont care...
      end
      @socket = nil
    end
    
    private
    
    def connect
      begin
        Timeout::timeout(3) {
          @socket = TCPSocket.open(@server.host, @server.port)
          linger = [1,0].pack('ii')
          @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, linger) #force close so if called many times it wont clog the available ports
          true
        }
      rescue Timeout::Error
        raise BackgroundQueue::ClientLib::ConnectionError, "Timeout Connecting to #{@server.host}:#{@server.port}"
      rescue Exception=>e
        raise BackgroundQueue::ClientLib::ConnectionError, "Error Connecting to #{@server.host}:#{@server.port}: #{e.message}"
      end
    end
    
    def check_connected
      connect if @socket.nil?
{'fraction_non_alphanumeric': 0.0866081648225868, 'fraction_numerical': 0.0015261350629530714, 'mean_word_length': 2.7564469914040113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '6098084', 'n_tokens_mistral': 720, 'n_tokens_neox': 716, 'n_words': 209}