private AtomicLong getChannelCounter(Channel channel) { AtomicLong counter = channelCounters.get(channel); if (counter == null) { counter = new AtomicLong(); AtomicLong oldCounter = channelCounters.putIfAbsent(channel, counter); if (oldCounter != null) { counter = oldCounter; } } // Remove the entry when the channel closes. if (!channel.isOpen()) { channelCounters.remove(channel); } return counter; }
@Override public String toString() { String channelString = getChannel().toString(); StringBuilder buf = new StringBuilder(channelString.length() + 32); buf.append(channelString); buf.append(getChildChannel().isOpen()? " CHILD_OPEN: " : " CHILD_CLOSED: "); buf.append(getChildChannel().getId()); return buf.toString(); } }
/** * Tests whether the given channel is ready for writing. * @return true if the channel is open and ready; false otherwise. */ private static boolean isChannelReady(Channel channel) { return (channel != null) && channel.isOpen() && channel.isBound() && channel.isConnected(); }
@Override public void close() { if (LOG.isDebugEnabled()) { LOG.debug("close called for sessionid:0x" + Long.toHexString(sessionId)); } // ZOOKEEPER-2743: // Always unregister connection upon close to prevent // connection bean leak under certain race conditions. factory.unregisterConnection(this); factory.removeCnxn(this); if (channel.isOpen()) { channel.close(); } }
@Override public void sendResponse(ReplyHeader h, Record r, String tag) throws IOException { if (!channel.isOpen()) { return; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Make space for length BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos); try { baos.write(fourBytes); bos.writeRecord(h, "header"); if (r != null) { bos.writeRecord(r, tag); } baos.close(); } catch (IOException e) { LOG.error("Error serializing response"); } byte b[] = baos.toByteArray(); ByteBuffer bb = ByteBuffer.wrap(b); bb.putInt(b.length - 4).rewind(); sendBuffer(bb); if (h.getXid() > 0) { // zks cannot be null otherwise we would not have gotten here! if (!zkServer.shouldThrottle(outstandingCount.decrementAndGet())) { enableRecv(); } } }
private void removeIfClosed(ChannelEventRunnable eventRunnable, Object key) { ChannelEvent event = eventRunnable.getEvent(); if (event instanceof ChannelStateEvent) { ChannelStateEvent se = (ChannelStateEvent) event; if (se.getState() == ChannelState.OPEN && !event.getChannel().isOpen()) { removeKey(key); } } }
@Override public boolean isGood(ChannelFuture resource) { Channel channel = resource.awaitUninterruptibly().getChannel(); boolean isSuccess = resource.isSuccess(); boolean isConnected = channel.isConnected(); boolean isOpen = channel.isOpen(); if (log.isTraceEnabled()) { log.trace("isGood = isSucess[%s] && isConnected[%s] && isOpen[%s]", isSuccess, isConnected, isOpen); } return isSuccess && isConnected && isOpen; }
protected Executor getChildExecutor(ChannelEvent e) { Object key = getChildExecutorKey(e); Executor executor = childExecutors.get(key); if (executor == null) { executor = new ChildExecutor(); Executor oldExecutor = childExecutors.putIfAbsent(key, executor); if (oldExecutor != null) { executor = oldExecutor; } } // Remove the entry when the channel closes. if (e instanceof ChannelStateEvent) { Channel channel = e.getChannel(); ChannelStateEvent se = (ChannelStateEvent) e; if (se.getState() == ChannelState.OPEN && !channel.isOpen()) { removeChildExecutor(key); } } return executor; }
public void run(Timeout timeout) throws Exception { if (timeout.isCancelled()) { return; } if (!ctx.getChannel().isOpen()) { return; } // Mark the future as failure if (future.setFailure(EXCEPTION)) { // If succeeded to mark as failure, notify the pipeline, too. fireWriteTimeOut(ctx); } }
public void run(Timeout timeout) throws Exception { if (timeout.isCancelled() || !ctx.getChannel().isOpen()) { return; } State state = (State) ctx.getAttachment(); long currentTime = System.currentTimeMillis(); long lastIoTime = Math.max(state.lastReadTime, state.lastWriteTime); long nextDelay = allIdleTimeMillis - (currentTime - lastIoTime); if (nextDelay <= 0) { // Both reader and writer are idle - set a new timeout and // notify the callback. state.allIdleTimeout = timer.newTimeout(this, allIdleTimeMillis, TimeUnit.MILLISECONDS); fireChannelIdle(ctx, IdleState.ALL_IDLE, lastIoTime); } else { // Either read or write occurred before the timeout - set a new // timeout with shorter delay. state.allIdleTimeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } } }
@Override public void disconnect() { super.disconnect(); if (channel.isOpen()) { channel.close().awaitUninterruptibly(); } WorldProvider worldProvider = CoreRegistry.get(WorldProvider.class); if (worldProvider != null) { worldProvider.unregisterListener(this); } }
public void run(Timeout timeout) throws Exception { if (timeout.isCancelled() || !ctx.getChannel().isOpen()) { return; } State state = (State) ctx.getAttachment(); long currentTime = System.currentTimeMillis(); long lastReadTime = state.lastReadTime; long nextDelay = readerIdleTimeMillis - (currentTime - lastReadTime); if (nextDelay <= 0) { // Reader is idle - set a new timeout and notify the callback. state.readerIdleTimeout = timer.newTimeout(this, readerIdleTimeMillis, TimeUnit.MILLISECONDS); fireChannelIdle(ctx, IdleState.READER_IDLE, lastReadTime); } else { // Read occurred before the timeout - set a new timeout with shorter delay. state.readerIdleTimeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } } }
public void run(Timeout timeout) throws Exception { if (timeout.isCancelled() || !ctx.getChannel().isOpen()) { return; } State state = (State) ctx.getAttachment(); long currentTime = System.currentTimeMillis(); long lastWriteTime = state.lastWriteTime; long nextDelay = writerIdleTimeMillis - (currentTime - lastWriteTime); if (nextDelay <= 0) { // Writer is idle - set a new timeout and notify the callback. state.writerIdleTimeout = timer.newTimeout(this, writerIdleTimeMillis, TimeUnit.MILLISECONDS); fireChannelIdle(ctx, IdleState.WRITER_IDLE, lastWriteTime); } else { // Write occurred before the timeout - set a new timeout with shorter delay. state.writerIdleTimeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } } }
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); fireReadTimedOut(ctx); } else { // Read occurred before the timeout - set a new timeout with shorter delay. state.timeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { final Channel channel = ctx.getChannel(); if (channel == null) { // For the case where this pipeline is not attached yet. handshakeFuture.setFailure(new ChannelException( StringUtils.format("Channel is null. The context name is [%s]", ctx.getName()) )); return; } handshakeFuture.setFailure(e.getCause()); if (channel.isOpen()) { channel.close(); } } });
@Override public void exceptionCaught(ChannelHandlerContext context, ExceptionEvent event) { if (log.isDebugEnabled()) { final Throwable cause = event.getCause(); if (cause == null) { log.debug("[%s] Caught exception", requestDesc); } else { log.debug(cause, "[%s] Caught exception", requestDesc); } } retVal.setException(event.getCause()); // response is non-null if we received initial chunk and then exception occurs if (response != null) { handler.exceptionCaught(response, event.getCause()); } try { if (channel.isOpen()) { channel.close(); } } catch (Exception e) { log.warn(e, "Error while closing channel"); } finally { channelResourceContainer.returnResource(); } }
long channelCounter = getChannelCounter(channel).addAndGet(increment); if (maxChannelMemorySize != 0 && channelCounter >= maxChannelMemorySize && channel.isOpen()) { if (channel.isReadable()) {
long channelCounter = getChannelCounter(channel).addAndGet(-increment); if (maxChannelMemorySize != 0 && channelCounter < maxChannelMemorySize && channel.isOpen()) { if (!channel.isReadable()) {
} else if (e instanceof ChildChannelStateEvent) { ChildChannelStateEvent evt = (ChildChannelStateEvent) e; if (evt.getChildChannel().isOpen()) { childChannelOpen(ctx, evt); } else {
} else if (e instanceof ChildChannelStateEvent) { ChildChannelStateEvent evt = (ChildChannelStateEvent) e; if (evt.getChildChannel().isOpen()) { childChannelOpen(ctx, evt); } else {