@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); if (proxyToTargetBrokerAddress == null) { if (log.isDebugEnabled()) { log.debug("{} Connected to broker", ctx.channel()); } } else { log.info("{} Connected through proxy to target broker at {}", ctx.channel(), proxyToTargetBrokerAddress); } // Send CONNECT command ctx.writeAndFlush(newConnectCommand()) .addListener(future -> { if (future.isSuccess()) { if (log.isDebugEnabled()) { log.debug("Complete: {}", future.isSuccess()); } state = State.SentConnectFrame; } else { log.warn("Error during handshake", future.cause()); ctx.close(); } }); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); log.info("{} Disconnected", ctx.channel()); if (!connectionFuture.isDone()) { connectionFuture.completeExceptionally(new PulsarClientException("Connection already closed")); } PulsarClientException e = new PulsarClientException( "Disconnected from server at " + ctx.channel().remoteAddress()); // Fail out all the pending ops pendingRequests.forEach((key, future) -> future.completeExceptionally(e)); pendingLookupRequests.forEach((key, future) -> future.completeExceptionally(e)); waitingLookupRequests.forEach(pair -> pair.getRight().getRight().completeExceptionally(e)); pendingGetLastMessageIdRequests.forEach((key, future) -> future.completeExceptionally(e)); pendingGetTopicsRequests.forEach((key, future) -> future.completeExceptionally(e)); pendingGetSchemaRequests.forEach((key, future) -> future.completeExceptionally(e)); // Notify all attached producers/consumers so they have a chance to reconnect producers.forEach((id, producer) -> producer.connectionClosed(this)); consumers.forEach((id, consumer) -> consumer.connectionClosed(this)); pendingRequests.clear(); pendingLookupRequests.clear(); waitingLookupRequests.clear(); pendingGetLastMessageIdRequests.clear(); pendingGetTopicsRequests.clear(); producers.clear(); consumers.clear(); timeoutTask.cancel(true); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { cancelKeepAliveTask(); }
private void handleKeepAliveTimeout() { if (!ctx.channel().isOpen()) { return; } if (!isHandshakeCompleted()) { log.warn("[{}] Pulsar Handshake was not completed within timeout, closing connection", ctx.channel()); ctx.close(); } else if (waitingForPingResponse && ctx.channel().config().isAutoRead()) { // We were waiting for a response and another keep-alive just completed. // If auto-read was disabled, it means we stopped reading from the connection, so we might receive the Ping // response later and thus not enforce the strict timeout here. log.warn("[{}] Forcing connection to close after keep-alive timeout", ctx.channel()); ctx.close(); } else if (remoteEndpointProtocolVersion >= ProtocolVersion.v1.getNumber()) { // Send keep alive probe to peer only if it supports the ping/pong commands, added in v1 if (log.isDebugEnabled()) { log.debug("[{}] Sending ping message", ctx.channel()); } waitingForPingResponse = true; ctx.writeAndFlush(Commands.newPing()); } else { if (log.isDebugEnabled()) { log.debug("[{}] Peer doesn't support keep-alive", ctx.channel()); } } }
@Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { switch (state) { case Init: case ProxyLookupRequests: // Do the regular decoding for the Connected message super.channelRead(ctx, msg); break; case ProxyConnectionToBroker: // Pass the buffer to the outbound connection and schedule next read // only // if we can write on the connection directProxyHandler.outboundChannel.writeAndFlush(msg).addListener(this); break; default: break; } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); LOG.warn("[{}] Got exception {} : {}", remoteAddress, cause.getClass().getSimpleName(), cause.getMessage(), ClientCnx.isKnownException(cause) ? null : cause); ctx.close(); }
@Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { super.channelUnregistered(ctx); activeConnections.dec(); }
@Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { super.channelRegistered(ctx); activeConnections.inc(); if (activeConnections.get() > service.getConfiguration().getMaxConcurrentInboundConnections()) { ctx.close(); rejectedConnections.inc(); return; } }
private void handleKeepAliveTimeout() { if (!ctx.channel().isOpen()) { return; } if (!isHandshakeCompleted()) { log.warn("[{}] Pulsar Handshake was not completed within timeout, closing connection", ctx.channel()); ctx.close(); } else if (waitingForPingResponse && ctx.channel().config().isAutoRead()) { // We were waiting for a response and another keep-alive just completed. // If auto-read was disabled, it means we stopped reading from the connection, so we might receive the Ping // response later and thus not enforce the strict timeout here. log.warn("[{}] Forcing connection to close after keep-alive timeout", ctx.channel()); ctx.close(); } else if (remoteEndpointProtocolVersion >= ProtocolVersion.v1.getNumber()) { // Send keep alive probe to peer only if it supports the ping/pong commands, added in v1 if (log.isDebugEnabled()) { log.debug("[{}] Sending ping message", ctx.channel()); } waitingForPingResponse = true; ctx.writeAndFlush(Commands.newPing()); } else { if (log.isDebugEnabled()) { log.debug("[{}] Peer doesn't support keep-alive", ctx.channel()); } } }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); log.info("New connection from {}", remoteAddress); this.ctx = ctx; }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); if (directProxyHandler != null && directProxyHandler.outboundChannel != null) { directProxyHandler.outboundChannel.close(); } if (client != null) { client.close(); } LOG.info("[{}] Connection closed", remoteAddress); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { cancelKeepAliveTask(); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); newConnections.inc(); LOG.info("[{}] New connection opened", remoteAddress); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); log.info("{} Disconnected", ctx.channel()); if (!connectionFuture.isDone()) { connectionFuture.completeExceptionally(new PulsarClientException("Connection already closed")); } PulsarClientException e = new PulsarClientException( "Disconnected from server at " + ctx.channel().remoteAddress()); // Fail out all the pending ops pendingRequests.forEach((key, future) -> future.completeExceptionally(e)); pendingLookupRequests.forEach((key, future) -> future.completeExceptionally(e)); waitingLookupRequests.forEach(pair -> pair.getRight().getRight().completeExceptionally(e)); pendingGetLastMessageIdRequests.forEach((key, future) -> future.completeExceptionally(e)); pendingGetTopicsRequests.forEach((key, future) -> future.completeExceptionally(e)); pendingGetSchemaRequests.forEach((key, future) -> future.completeExceptionally(e)); // Notify all attached producers/consumers so they have a chance to reconnect producers.forEach((id, producer) -> producer.connectionClosed(this)); consumers.forEach((id, consumer) -> consumer.connectionClosed(this)); pendingRequests.clear(); pendingLookupRequests.clear(); waitingLookupRequests.clear(); pendingGetLastMessageIdRequests.clear(); pendingGetTopicsRequests.clear(); producers.clear(); consumers.clear(); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); if (proxyToTargetBrokerAddress == null) { if (log.isDebugEnabled()) { log.debug("{} Connected to broker", ctx.channel()); } } else { log.info("{} Connected through proxy to target broker at {}", ctx.channel(), proxyToTargetBrokerAddress); } // Send CONNECT command ctx.writeAndFlush(newConnectCommand()) .addListener(future -> { if (future.isSuccess()) { if (log.isDebugEnabled()) { log.debug("Complete: {}", future.isSuccess()); } state = State.SentConnectFrame; } else { log.warn("Error during handshake", future.cause()); ctx.close(); } }); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); isActive = false; log.info("Closed connection from {}", remoteAddress); // Connection is gone, close the producers immediately producers.values().forEach((producerFuture) -> { if (producerFuture.isDone() && !producerFuture.isCompletedExceptionally()) { Producer producer = producerFuture.getNow(null); producer.closeNow(); } }); consumers.values().forEach((consumerFuture) -> { Consumer consumer; if (consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) { consumer = consumerFuture.getNow(null); } else { return; } try { consumer.close(); } catch (BrokerServiceException e) { log.warn("Consumer {} was already closed: {}", consumer, e.getMessage(), e); } }); }