public void returnConnection(RedisConnection connection) {
   SubscribesConnectionEntry entry = clients.get(connection.getRedisClient());
   if (entry.isFreezed()) {
     connection.closeAsync();
   } else {
     entry.getConnections().add(connection);
   }
   entry.getConnectionsSemaphore().release();
 }
  @Override
  public void shutdown() {
    monitorFuture.cancel(true);
    super.shutdown();

    for (RedisConnection connection : nodeConnections.values()) {
      connection.getRedisClient().shutdown();
    }
  }
  private void tryReconnect(
      final EventLoopGroup group, final RedisConnection connection, final int attempts) {
    if (connection.isClosed() || group.isShuttingDown()) {
      return;
    }

    log.debug(
        "reconnecting {} to {} ", connection, connection.getRedisClient().getAddr(), connection);

    bootstrap
        .connect()
        .addListener(
            new ChannelFutureListener() {

              @Override
              public void operationComplete(final ChannelFuture future) throws Exception {
                if (connection.isClosed() || group.isShuttingDown()) {
                  return;
                }

                try {
                  if (future.isSuccess()) {
                    log.debug(
                        "{} connected to {}", connection, connection.getRedisClient().getAddr());
                    reconnect(connection, future.channel());
                    return;
                  }
                } catch (RedisException e) {
                  log.warn(
                      "Can't connect "
                          + connection
                          + " to "
                          + connection.getRedisClient().getAddr(),
                      e);
                }

                int timeout = 2 << attempts;
                group.schedule(
                    new Runnable() {
                      @Override
                      public void run() {
                        tryReconnect(group, connection, Math.min(BACKOFF_CAP, attempts + 1));
                      }
                    },
                    timeout,
                    TimeUnit.MILLISECONDS);
              }
            });
  }
 private void reconnect(final RedisConnection connection, final Channel channel) {
   if (connection.getReconnectListener() != null) {
     // new connection used only for channel init
     RedisConnection rc = new RedisConnection(connection.getRedisClient(), channel);
     Promise<RedisConnection> connectionFuture = bootstrap.group().next().newPromise();
     connection.getReconnectListener().onReconnect(rc, connectionFuture);
     connectionFuture.addListener(
         new FutureListener<RedisConnection>() {
           @Override
           public void operationComplete(Future<RedisConnection> future) throws Exception {
             if (future.isSuccess()) {
               connection.updateChannel(channel);
               resubscribe(connection);
             }
           }
         });
   } else {
     connection.updateChannel(channel);
     resubscribe(connection);
   }
 }
Beispiel #5
0
 public void returnConnection(RedisConnection connection) {
   ClientConnectionsEntry entry = addr2Entry.get(connection.getRedisClient().getAddr());
   entries.returnConnection(entry, connection);
 }