<commit_before>require "redic"

class Redic::Cluster
  attr :url
  attr :node
  attr_accessor :debug

  def initialize(url="redis://localhost:12001", timeout=10_000_000)
    @url   = url
    @node  = Redic::Client.new(url, timeout)
    @debug = false
  end

  def call(*args)
    res = @node.connect do
      @node.write(args)
      @node.read
    end

    return res unless res.is_a?(RuntimeError)

    parts = res.message.split

    if parts.first == "MOVED"
      slot, addr = parts[1,2]

      $stderr.puts "-> Redirected to slot [#{slot}] located at #{addr}" if @debug

      @node = Redic::Client.new("redis://#{addr}", @node.timeout)

      call(*args)
    else
      res
    end
  end
end<commit_msg>Disconnect from previous node on redirection<commit_after>require "redic"

class Redic::Cluster
  attr :url
  attr :node
  attr_accessor :debug

  def initialize(url="redis://localhost:12001", timeout=10_000_000)
    @url   = url
    @node  = Redic::Client.new(url, timeout)
    @debug = false
  end

  def call(*args)
    res = @node.connect do
      @node.write(args)
      @node.read
    end

    return res unless res.is_a?(RuntimeError)

    parts = res.message.split

    if parts.first == "MOVED"
      slot, addr = parts[1,2]

      $stderr.puts "-> Redirected to slot [#{slot}] located at #{addr}" if @debug

      @node.call("QUIT")

      @node = Redic::Client.new("redis://#{addr}", @node.timeout)

      call(*args)
    else
      res
    end
  end
end
{'fraction_non_alphanumeric': 0.13713509843856075, 'fraction_numerical': 0.020366598778004074, 'mean_word_length': 2.694235588972431, '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': '727655', 'n_tokens_mistral': 537, 'n_tokens_neox': 505, 'n_words': 125}