private static void closeChannel(Channel channel) { channel.attr(POOL_KEY).getAndSet(null); channel.close(); }
public static boolean isActiveTokenSet(Channel channel) { return channel != null && channel.attr(ACTIVE_TOKEN_ATTRIBUTE).getAndSet(null) != null; }
private static void closeChannel(Channel channel) { channel.attr(POOL_KEY).getAndSet(null); channel.close(); }
private static void closeChannel(Channel channel) { channel.attr(POOL_KEY).getAndSet(null); channel.close(); }
/** * Remove the current request from the context. * * @param ctx The context * @return The request or null if it is not present */ static NettyHttpRequest remove(ChannelHandlerContext ctx) { Channel channel = ctx.channel(); io.netty.util.Attribute<NettyHttpRequest> attr = channel.attr(KEY); return attr.getAndSet(null); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { LOG.warn("Exception caught", cause); NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).getAndSet(null); if (cnxn != null) { if (LOG.isDebugEnabled()) { LOG.debug("Closing {}", cnxn); } cnxn.close(); } }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (LOG.isTraceEnabled()) { LOG.trace("Channel inactive {}", ctx.channel()); } allChannels.remove(ctx.channel()); NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).getAndSet(null); if (cnxn != null) { if (LOG.isTraceEnabled()) { LOG.trace("Channel inactive caused close {}", cnxn); } cnxn.close(); } }
public synchronized void tryRelease(Channel channel) { String host = channel.attr(hostKey).getAndSet(null); List<Channel> channels = channelPool.get(host); if (channels == null || channels.size() < maxConnPerHost) { LOGGER.debug("tryRelease channel success, host={}", host); channelPool.put(host, channel); } else { LOGGER.debug("tryRelease channel pool size over limit={}, host={}, channel closed.", maxConnPerHost, host); channel.close(); } }
public void sendNextCommand(Channel channel) { QueueCommand command = channel.attr(CommandsQueue.CURRENT_COMMAND).getAndSet(null); if (command != null) { queue.poll(); } else { QueueCommandHolder c = queue.peek(); if (c != null) { QueueCommand data = c.getCommand(); List<CommandData<Object, Object>> pubSubOps = data.getPubSubOperations(); if (!pubSubOps.isEmpty()) { queue.poll(); } } } sendData(channel); }
public void sendNextCommand(Channel channel) { QueueCommand command = channel.attr(CommandsQueue.CURRENT_COMMAND).getAndSet(null); if (command != null) { queue.poll(); } else { QueueCommandHolder c = queue.peek(); if (c != null) { QueueCommand data = c.getCommand(); List<CommandData<Object, Object>> pubSubOps = data.getPubSubOperations(); if (!pubSubOps.isEmpty()) { queue.poll(); } } } sendData(channel); }
@Subscribe @AllowConcurrentEvents void on(ConnectionConnectEvent event) { Connection connection = event.connection; String hostAndPort = connection.getChannel().attr(attrKey).getAndSet(null); if (hostAndPort == null) { InetSocketAddress address = (InetSocketAddress) connection.getChannel().remoteAddress(); hostAndPort = getHostAndPort(address.getAddress().getHostAddress(), address.getPort()); } connections.computeIfAbsent(hostAndPort, key -> new ArrayList<>(gateway_client_num)).add(connection); logger.info("one gateway client connect success, hostAndPort={}, conn={}", hostAndPort, connection); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { RequestContext context = ctx.channel().attr(client.requestKey).getAndSet(null); try { if (context != null && context.tryDone()) { context.onException(cause); } } finally { client.pool.tryRelease(ctx.channel()); } LOGGER.error("http client caught an ex, info={}", context, cause); }
private void writeRequest(Channel channel, RequestContext context) { channel.attr(requestKey).set(context); pool.attachHost(context.host, channel); channel.writeAndFlush(context.request).addListener((ChannelFutureListener) future -> { if (!future.isSuccess()) { RequestContext info = future.channel().attr(requestKey).getAndSet(null); info.tryDone(); info.onFailure(503, "Service Unavailable"); LOGGER.debug("request failure request={}", info); pool.tryRelease(future.channel()); } }); }
private static Scope createScopeForRequestThread(RequestContext ctx, @Nullable TraceContext currentSpan) { final Attribute<TraceContext> traceContextAttribute = ctx.attr(TRACE_CONTEXT_KEY); final TraceContext previous = traceContextAttribute.getAndSet(currentSpan); // Don't remove the outer-most context (client or server request) if (previous == null) { return INITIAL_REQUEST_SCOPE; } // Removes sub-spans (i.e. local spans) from the current context when Brave's scope does. // If an asynchronous sub-span, it may still complete later. class RequestContextTraceContextScope implements Scope { @Override public void close() { // re-lookup the attribute to avoid holding a reference to the request if this scope is leaked getTraceContextAttributeOrWarnOnce().set(previous); } @Override public String toString() { return "RequestContextTraceContextScope"; } } return new RequestContextTraceContextScope(); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { int clientNum = STATISTICS.clientNum.incrementAndGet(); LOGGER.info("client connect channel={}, clientNum={}", ctx.channel(), clientNum); for (int i = 0; i < 3; i++) { if (clientConfig != null) break; clientConfig = ctx.channel().attr(CONFIG_KEY).getAndSet(null); if (clientConfig == null) TimeUnit.SECONDS.sleep(1); } if (clientConfig == null) { throw new NullPointerException("client config is null, channel=" + ctx.channel()); } connection.init(ctx.channel(), true); if (perfTest) { handshake(); } else { tryFastConnect(); } }
private void doReleaseChannel(Channel channel, Promise<Void> promise) { assert channel.eventLoop().inEventLoop(); // Remove the POOL_KEY attribute from the Channel and check if it was acquired from this pool, if not fail. if (channel.attr(POOL_KEY).getAndSet(null) != this) { closeAndFail(channel, // Better include a stacktrace here as this is an user error. new IllegalArgumentException( "Channel " + channel + " was not acquired from this ChannelPool"), promise); } else { try { if (releaseHealthCheck) { doHealthCheckOnRelease(channel, promise); } else { releaseAndOffer(channel, promise); } } catch (Throwable cause) { closeAndFail(channel, cause, promise); } } }
private void doReleaseChannel(Channel channel, Promise<Void> promise) { assert channel.eventLoop().inEventLoop(); // Remove the POOL_KEY attribute from the Channel and check if it was acquired from this pool, if not fail. if (channel.attr(POOL_KEY).getAndSet(null) != this) { closeAndFail(channel, // Better include a stacktrace here as this is an user error. new IllegalArgumentException( "Channel " + channel + " was not acquired from this ChannelPool"), promise); } else { try { if (releaseHealthCheck) { doHealthCheckOnRelease(channel, promise); } else { releaseAndOffer(channel, promise); } } catch (Throwable cause) { closeAndFail(channel, cause, promise); } } }
private void doReleaseChannel(Channel channel, Promise<Void> promise) { assert channel.eventLoop().inEventLoop(); // Remove the POOL_KEY attribute from the Channel and check if it was acquired from this pool, if not fail. if (channel.attr(POOL_KEY).getAndSet(null) != this) { closeAndFail(channel, // Better include a stacktrace here as this is an user error. new IllegalArgumentException( "Channel " + channel + " was not acquired from this ChannelPool"), promise); } else { try { if (releaseHealthCheck) { doHealthCheckOnRelease(channel, promise); } else { releaseAndOffer(channel, promise); } } catch (Throwable cause) { closeAndFail(channel, cause, promise); } } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { RequestContext context = ctx.channel().attr(client.requestKey).getAndSet(null); try { if (context != null && context.tryDone()) { LOGGER.debug("receive server response, request={}, response={}", context, msg); HttpResponse response = (HttpResponse) msg; if (isRedirect(response)) { if (context.onRedirect(response)) { String location = getRedirectLocation(context.request, response); if (location != null && location.length() > 0) { context.cancelled.set(false); context.request.setUri(location); client.request(context); return; } } } context.onResponse(response); } else { LOGGER.warn("receive server response but timeout, request={}, response={}", context, msg); } } finally { client.pool.tryRelease(ctx.channel()); ReferenceCountUtil.release(msg); } }
public static <T> TransportCallback<T> getSslTimingCallback(Channel channel, RequestContext requestContext, TimeoutTransportCallback<T> callback) { return response -> { Long duration = channel.attr(SslHandshakeTimingHandler.SSL_HANDSHAKE_START_TIME).getAndSet(null); if (duration != null) { TimingContextUtil.markTiming(requestContext, TIMING_KEY, duration); } callback.onResponse(response); }; } }