<commit_before>class WebsocketServer
  Pairing = Struct.new(:is_ws, :proxy)

  def initialize
    @pairing = {}
    @sockets = Concurrent::Array.new

    Thread.new do
      errors = []
      loop do
        begin
          reads, writes, errors = IO.select(@sockets, @sockets, @sockets, 1)
        rescue IOError
          cleanup(errors)
        end

        # Skip this loop if we can't do anything
        next if Array(reads).empty? || writes.empty? || errors.any?

        # Do the data transfers
        reads.each do |read|
          @pairing[read].proxy.transmit(writes, @pairing[read].is_ws)
        end
      end
    end
  end

  def call(env)
    if WebSocket::Driver.websocket?(env)
      exp = %r{^/ws/console/([a-zA-Z0-9]+)/?$}.match(env['REQUEST_URI'])
      return not_found if exp.nil?

      init_proxy(env, exp[1])

      [-1, {}, []]
    else
      not_found
    end
  end

  private

  def init_proxy(env, url)
    console = SystemConsole.find_by!(:url_secret => url)
    proxy = WebsocketProxy.new(env, console)
    return proxy.cleanup if proxy.error
    proxy.start

    # Release the connection because one SPICE console can open multiple TCP connections<commit_msg>Sleep for a second if IO.select returns no usable descriptors<commit_after>class WebsocketServer
  Pairing = Struct.new(:is_ws, :proxy)

  def initialize
    @pairing = {}
    @sockets = Concurrent::Array.new

    Thread.new do
      errors = []
      loop do
        begin
          reads, writes, errors = IO.select(@sockets, @sockets, @sockets, 1)
        rescue IOError
          cleanup(errors)
        end

        # Skip this loop if we can't do anything
        if Array(reads).empty? || writes.empty? || errors.any?
          sleep(1) if @sockets.empty?
          next
        end

        # Do the data transfers
        reads.each do |read|
          @pairing[read].proxy.transmit(writes, @pairing[read].is_ws)
        end
      end
    end
  end

  def call(env)
    if WebSocket::Driver.websocket?(env)
      exp = %r{^/ws/console/([a-zA-Z0-9]+)/?$}.match(env['REQUEST_URI'])
      return not_found if exp.nil?

      init_proxy(env, exp[1])

      [-1, {}, []]
    else
      not_found
    end
  end

  private

  def init_proxy(env, url)
    console = SystemConsole.find_by!(:url_secret => url)
    proxy = WebsocketProxy.new(env, console)
    return proxy.cleanup if proxy.error
    proxy.start

    # Release the connection because one SPICE console can open multiple TCP connections
{'fraction_non_alphanumeric': 0.11235955056179775, 'fraction_numerical': 0.004414125200642055, 'mean_word_length': 2.306366047745358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '82114', 'n_tokens_mistral': 813, 'n_tokens_neox': 777, 'n_words': 206}