Channel isChannelReady() { Channel channel = channelRef.get(); if (channel == null) { return null; } // improve performance skill check if (!channel.isWritable()) { return null; } return channel; }
public Channel waitForChannelReady() { Channel channel = channelRef.get(); long pendingTime = 0; while ((channel == null && !isClosed()) || (channel != null && !channel.isWritable())) { JStormUtils.sleepMs(1); pendingTime++; if (discardCheck(pendingTime, timeoutMs, messageBuffer.size())) { messageBuffer.clear(); return null; } if (pendingTime % 30000 == 0) { LOG.info("Pending total time={}, channel.isWritable={}, pendingNum={}, remoteAddress={}", pendingTime, channel != null ? channel.isWritable() : null, pendings.get(), channel != null ? channel.getRemoteAddress() : null); } channel = channelRef.get(); } return channel; }
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception { if (!(e instanceof MessageEvent)) { ctx.sendDownstream(e); return; } boolean offered = queue.offer((MessageEvent) e); assert offered; final Channel channel = ctx.getChannel(); // call flush if the channel is writable or not connected. flush(..) will take care of the rest if (channel.isWritable() || !channel.isConnected()) { this.ctx = ctx; flush(ctx, false); } }
if (acquired && (!channel.isConnected() || channel.isWritable() && !queue.isEmpty())) { flush(consolidateOnFlush);
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent evt) throws Exception { long wait = 0; long size = calculateSize(evt.getMessage()); long now = TrafficCounter.milliSecondFromNano(); Channel channel = ctx.getChannel(); try { if (size > 0 && trafficCounter != null) { // compute the number of ms to wait before continue with the channel wait = trafficCounter.writeTimeToWait(size, writeLimit, maxTime, now); if (logger.isDebugEnabled()) { logger.debug("Write suspend: " + wait + ':' + channel.isWritable() + ':' + channel.getUserDefinedWritability(index)); } if (wait < MINIMAL_WAIT || release.get()) { wait = 0; } } } finally { // The message is scheduled submitWrite(ctx, evt, size, wait, now); } }
void pushBatch(NettyMessage message) { if (message == null || message.isEmpty()) { return; } synchronized (writeLock) { Channel channel = channelRef.get(); if (channel == null) { messageBuffer.add(message, false); LOG.debug("Pending requested message, the size is {}, because channel is not ready.", messageBuffer.size()); } else { if (channel.isWritable()) { MessageBatch messageBatch = messageBuffer.add(message); if (messageBatch != null) { flushRequest(channel, messageBatch); } } else { messageBuffer.add(message, false); } } if (messageBuffer.size() >= BATCH_THRESHOLD_WARN) { channel = waitForChannelReady(); if (channel != null) { MessageBatch messageBatch = messageBuffer.drain(); flushRequest(channel, messageBatch); } } } }
while (channel.isWritable()) { if (currentEvent == null) { currentEvent = queue.poll(); if (acquired && (!channel.isConnected() || channel.isWritable() && !queue.isEmpty() && !suspend || flushNeeded)) { flush(ctx, fireNow);
/** * Is the write queue full?, see {@link WriteStream#writeQueueFull} */ public boolean writeQueueFull() { return !channel.isWritable(); }
Channel isChannelReady() { Channel channel = channelRef.get(); if (channel == null) { return null; } // improve performance skill check if (channel.isWritable() == false) { return null; } return channel; }
private synchronized void flush() { if (channel == null || !channel.isWritable()) { return; } if (!messageQueue.isEmpty()) { logger.debug("Session " + id + " flushing queue"); channel.write(Frame.messageFrame(new ArrayList<SockJsMessage>(messageQueue).toArray(new SockJsMessage[messageQueue.size()]))); messageQueue.clear(); } }
@Override public synchronized void send(String message) { final SockJsMessage msg = new SockJsMessage(message); // Check and see if we can send the message straight away if (channel != null && channel.isWritable() && messageQueue.isEmpty()) { channel.write(Frame.messageFrame(msg)); } else { messageQueue.addLast(msg); flush(); } }
private void callDrainHandler() { if (drainHandler != null) { if (channel.isWritable()) { try { drainHandler.handle(null); } catch (Throwable t) { handleHandlerException(t); } } } } }
@Override public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { final Channel outboundChannel = e.getChannel(); //support for throttling driven by socket saturation if (!outboundChannel.isWritable()) { //inboundChannel.setReadable(false); } else { //inboundChannel.setReadable(true); } super.channelInterestChanged(ctx, e); }
public void notifyInterestChanged(Channel channel) { synchronized (writeLock) { if (channel.isWritable()) { MessageBatch messageBatch = messageBuffer.drain(); if (messageBatch != null) { flushRequest(channel, messageBatch); } } } }
@Override public void run(Timeout timeout) throws Exception { if (!timeout.isCancelled() && channel != null && channel.isWritable()) { logger.debug("Sending heartbeat/ping frame"); channel.write(new PingWebSocketFrame()); scheduleHeartbeatTask(); } } }
@Override public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { listener.connectionReadyForWrites(e.getChannel().getId(), e.getChannel().isWritable()); }
void handleInterestedOpsChanged() { try { if (channel.isWritable()) { if (currentRequest != null) { setContext(); currentRequest.handleDrained(); } else if (ws != null) { ws.writable(); } } } catch (Throwable t) { handleHandlerException(t); } }
void handleInterestedOpsChanged() { try { if (channel.isWritable()) { setContext(); if (pendingResponse != null) { pendingResponse.handleDrained(); } else if (ws != null) { ws.writable(); } } } catch (Throwable t) { handleHandlerException(t); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { if (e.getCause() instanceof SessionHandler.NotFoundException) { respond(e.getChannel(), HttpResponseStatus.NOT_FOUND, "Session not found."); } else if (e.getCause() instanceof SessionHandler.LockException) { if (e.getChannel().isWritable()) { e.getChannel().write(Frame.closeFrame(2010, "Another connection still open")).addListener(ChannelFutureListener.CLOSE); } } else { super.exceptionCaught(ctx, e); } }
public static void respond(Channel channel, HttpResponseStatus status, String message) throws Exception { // TODO: Why aren't response data defined in SockJS for error messages? HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_0, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); final ChannelBuffer buffer = ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8); response.setContent(buffer); response.setHeader(CONTENT_LENGTH, buffer.readableBytes()); response.setHeader(SET_COOKIE, "JSESSIONID=dummy; path=/"); // FIXME: Don't sprinkle cookies in every request response.setHeader(CACHE_CONTROL, "no-store, no-cache, must-revalidate, max-age=0"); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Credentials", "true"); if (channel.isWritable()) channel.write(response).addListener(ChannelFutureListener.CLOSE); }