package redis.clients.jedis;

import static redis.clients.jedis.JedisClusterInfoCache.getNodeKey;

import java.util.Map;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.exceptions.JedisConnectionException;

public abstract class JedisClusterConnectionHandler {
  protected final JedisClusterInfoCache cache;

  abstract Jedis getConnection();

  private int timeout;

  public void returnConnection(Jedis connection) {
    cache.getNode(getNodeKey(connection.getClient())).returnResource(connection);
  }

  public void returnBrokenConnection(Jedis connection) {
    cache.getNode(getNodeKey(connection.getClient())).returnBrokenResource(connection);
  }

  abstract Jedis getConnectionFromSlot(int slot);

  public Jedis getConnectionFromNode(HostAndPort node) {
    cache.setNodeIfNotExist(node);
    return cache.getNode(JedisClusterInfoCache.getNodeKey(node)).getResource();
  }

  public JedisClusterConnectionHandler(Set<HostAndPort> nodes,
      final GenericObjectPoolConfig poolConfig, int timeout) {
    this(nodes, poolConfig, timeout, timeout);
  }

  public JedisClusterConnectionHandler(Set<HostAndPort> nodes,
      final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout) {
    this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout);
    initializeSlotsCache(nodes, poolConfig);
  }

  public Map<String, JedisPool> getNodes() {
    return cache.getNodes();
  }

  public void assignSlotToNode(int slot, HostAndPort targetNode) {
    cache.assignSlotToNode(slot, targetNode);
  }

  private void initializeSlotsCache(Set<HostAndPort> startNodes, GenericObjectPoolConfig poolConfig) {
    for (HostAndPort hostAndPort : startNodes) {
      Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort());
      try {
        cache.discoverClusterNodesAndSlots(jedis);
        break;
      } catch (JedisConnectionException e) {
        // try next nodes
      } finally {
        if (jedis != null) {
          jedis.close();
        }
      }
    }

    for (HostAndPort node : startNodes) {
      cache.setNodeIfNotExist(node);
    }
  }

  public void renewSlotCache() {
    for (JedisPool jp : cache.getNodes().values()) {
      Jedis jedis = null;
      try {
        jedis = jp.getResource();
        cache.discoverClusterSlots(jedis);
        break;
      } catch (JedisConnectionException e) {
        // try next nodes
      } finally {
        if (jedis != null) {
          jedis.close();
        }
      }
    }
  }

}

@@ -27,77 +27,8 @@
 s;%0A%0A
-import static redis.clients.jedis.JedisClusterInfoCache.getNodeKey;%0A%0A
 impo
@@ -45,16 +45,16 @@
 il.Map;%0A
+
 import j
@@ -337,320 +337,8 @@
 );%0A%0A
-  private int timeout;%0A%0A  public void returnConnection(Jedis connection) %7B%0A    cache.getNode(getNodeKey(connection.getClient())).returnResource(connection);%0A  %7D%0A%0A  public void returnBrokenConnection(Jedis connection) %7B%0A    cache.getNode(getNodeKey(connection.getClient())).returnBrokenResource(connection);%0A  %7D%0A%0A
   ab
@@ -565,186 +565,8 @@
  %7D%0A%0A
-  public JedisClusterConnectionHandler(Set%3CHostAndPort%3E nodes,%0A      final GenericObjectPoolConfig poolConfig, int timeout) %7B%0A    this(nodes, poolConfig, timeout, timeout);%0A  %7D%0A%0A
   pu
@@ -889,24 +889,24 @@
 etNodes() %7B%0A
+
     return c
@@ -931,126 +931,8 @@
  %7D%0A%0A
-  public void assignSlotToNode(int slot, HostAndPort targetNode) %7B%0A    cache.assignSlotToNode(slot, targetNode);%0A  %7D%0A%0A
   pr

Remove unused field / methods