@Override public final void run(Timeout timeout) throws Exception { if (timeout.isCancelled()) { // reschedule has already been done, don't incur the overhead of redoing it return; } long startPoint = Math.max(getLastIoTimeMillis(), getLastIdleTimeMillis()); // Doing the calculation here avoids having to call currentTimeMillis twice (here and in // reschedule). // Given that the precision of timeout is limited, and that lastIdleTime is only updated if // idle is fired, we must // always call currentTimeMillis(). For example, imagine session idle last fired at t0. // Timeout will occur at or // after t0 + configured idleTime. Even if an I/O event occurred after t0, we may still need // to fire sessionIdle. long timeUntilSessionIdle = startPoint + idleTimeMillis - currentTimeMillis(); if (timeUntilSessionIdle <= 0 && idleTimeMillis != 0) { fireSessionIdle(filterChain); reschedule(idleTimeMillis); } else { // An intervening I/O means we should not fire session idle, but we must reschedule to // ensure accuracy // of when we do fire sessionIdle. reschedule(timeUntilSessionIdle); } }
@Override public void run(Timeout timeout) throws Exception { if (timeout.isCancelled()) { return; } if (!ctx.getChannel().isOpen()) { return; } State state = (State) ctx.getAttachment(); long currentTime = System.currentTimeMillis(); long nextDelay = timeoutMillis - (currentTime - state.lastReadTime); if (nextDelay <= 0) { // Read timed out - set a new timeout and notify the callback. state.timeout = timer.newTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS); try { // FIXME This should be called from an I/O thread. // To be fixed in Netty 4. readTimedOut(ctx); } catch (Throwable t) { fireExceptionCaught(ctx, t); } } else { // Read occurred before the timeout - set a new timeout with shorter delay. state.timeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } }
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; } }
/** {@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; }
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(); }
/** Stops the host monitor. */ void shutdown() { synchronized (this) { timeout.cancel(); timeout = null; } }