private void reschedule(long delayMillis) {
      // KG-9954 - We do not want to synchronize the reschedule method, instead use the NULL Timeout
      // in case
      // timeout gets set to null by another thread before we call cancel.
      if (timeout != NULL_TIMEOUT) {
        timeout.cancel();
      }

      if (idleTimeMillis != 0) {
        timeout = timer.newTimeout(this, delayMillis, MILLISECONDS);
      } else {
        timeout = NULL_TIMEOUT;
      }
    }
    public void run(Timeout timeout) {
      if (isClosed.get()) return;
      Object attachment =
          channel.getPipeline().getContext(NettyAsyncHttpProvider.class).getAttachment();
      if (attachment != null) {
        if (NettyResponseFuture.class.isAssignableFrom(attachment.getClass())) {
          NettyResponseFuture<?> future = (NettyResponseFuture<?>) attachment;

          if (!future.isDone() && !future.isCancelled()) {
            log.warn("Future not in appropriate state {}", future);
            return;
          }
        }
      }

      if (activeChannels.remove(channel)) {
        log.debug("Channel idle. Expiring {}", channel);
        close(channel);
      }
      timeout.cancel();
    }
  /** {@inheritDoc} */
  public boolean removeAll(Channel channel) {
    if (isClosed.get()) return false;

    boolean isRemoved = false;
    Iterator<Map.Entry<String, ConcurrentLinkedQueue<Channel>>> i =
        connectionsPool.entrySet().iterator();
    while (i.hasNext()) {
      Map.Entry<String, ConcurrentLinkedQueue<Channel>> e = i.next();
      boolean removed = e.getValue().remove(channel);
      if (removed) {
        log.debug("Removing uri: {} for channel {}", e.getKey(), e.getValue());
        Timeout idleFuture = trackedIdleConnections.remove(channel);
        if (idleFuture != null) {
          idleFuture.cancel();
        } else {
          log.debug(
              "ConnectionsPool decrementAndGet totalConnections {}", trackedIdleConnections.size());
        }
      }
      isRemoved |= removed;
    }
    return isRemoved;
  }
  /** {@inheritDoc} */
  public Channel poll(String uri) {
    if (!provider.getConfig().isSslConnectionPoolEnabled() && uri.startsWith("https")) {
      return null;
    }

    Channel channel = null;
    ConcurrentLinkedQueue<Channel> pooledConnectionForHost = connectionsPool.get(uri);
    if (pooledConnectionForHost != null) {
      boolean poolEmpty = false;
      while (!poolEmpty && channel == null) {
        if (pooledConnectionForHost.size() > 0) {
          channel = pooledConnectionForHost.poll();
        }

        if (channel == null) {
          poolEmpty = true;
        } else if (!channel.isConnected() || !channel.isOpen()) {
          removeAll(channel);
          channel = null;
        } else {
          Timeout idleFuture = trackedIdleConnections.remove(channel);
          if (idleFuture != null) {
            idleFuture.cancel();
          }

          // Double checking the channel hasn't been closed in between.
          if (!channel.isConnected() || !channel.isOpen()) {
            channel = null;
          }

          log.debug(
              "ConnectionsPool decrementAndGet totalConnections {}", trackedIdleConnections.size());
        }
      }
    }
    return channel;
  }
Example #5
0
 /** Stops the host monitor. */
 void shutdown() {
   synchronized (this) {
     timeout.cancel();
     timeout = null;
   }
 }