<commit_before>module Grenache
  # Implement Grape connection helpers
  class Link

    # Connect to grape
    def connect
      unless connected?
        ws_connect
      end
    end

    # Return true if it's connected to Grape
    def connected?
      @connected
    end

    # Disconnect from grape
    def disconnect
      @ws.close
      @ws = nil
    end

    # Send a request to grape
    def send(type, payload, opts = {}, &block)
      req = Request.new(type,payload,opts, &block)
      requests[req.rid] = req
      ws_send req.to_json
    end

    private

    def requests
      @requests ||= {}
    end

    def grape_url
      @grape_url ||= Base.config.grape_address
    end

    def ws_send(payload)
      ws_connect unless connected?
      @ws.send(payload)
    end

    def ws_connect
      @ws = Faye::WebSocket::Client.new(grape_url)
      @ws.on(:open, method(:on_open))
      @ws.on(:message, method(:on_message))
      @ws.on(:close, method(:on_close))
    end

    def on_open(ev)
      @connected = true
    end

    def on_message(ev)
      msg = Oj.load(ev.data)
      if req = requests[msg[0]]
        req.yield(msg) if req.block_given?
      end
    end
<commit_msg>save memory deleting hash entry when a message is received<commit_after>module Grenache
  # Implement Grape connection helpers
  class Link

    # Connect to grape
    def connect
      unless connected?
        ws_connect
      end
    end

    # Return true if it's connected to Grape
    def connected?
      @connected
    end

    # Disconnect from grape
    def disconnect
      @ws.close
      @ws = nil
    end

    # Send a request to grape
    def send(type, payload, opts = {}, &block)
      req = Request.new(type,payload,opts, &block)
      requests[req.rid] = req
      ws_send req.to_json
    end

    private

    def requests
      @requests ||= {}
    end

    def grape_url
      @grape_url ||= Base.config.grape_address
    end

    def ws_send(payload)
      ws_connect unless connected?
      @ws.send(payload)
    end

    def ws_connect
      @ws = Faye::WebSocket::Client.new(grape_url)
      @ws.on(:open, method(:on_open))
      @ws.on(:message, method(:on_message))
      @ws.on(:close, method(:on_close))
    end

    def on_open(ev)
      @connected = true
    end

    def on_message(ev)
      msg = Oj.load(ev.data)
      if req = requests[msg[0]]
        requests.delete(msg[0])
        req.yield(msg) if req.block_given?
      end
    end

{'fraction_non_alphanumeric': 0.09437019036046983, 'fraction_numerical': 0.001215066828675577, 'mean_word_length': 2.1345177664974617, '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': '7618668', 'n_tokens_mistral': 837, 'n_tokens_neox': 785, 'n_words': 212}