@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg == CHANNEL_RELEASE_SIGNAL) { AsyncPool<Channel> pool = ctx.channel().attr(CHANNEL_POOL_ATTR_KEY).getAndSet(null); if (pool != null) { pool.put(ctx.channel()); } } else if (msg == CHANNEL_DESTROY_SIGNAL) { AsyncPool<Channel> pool = ctx.channel().attr(CHANNEL_POOL_ATTR_KEY).getAndSet(null); if (pool != null) { pool.dispose(ctx.channel()); } } }
@Override public Collection<Callback<Channel>> cancelWaiters() { Collection<Callback<Channel>> cancelled = new ArrayList<Callback<Channel>>(); final Collection<AsyncPool<Channel>> pools; synchronized (_mutex) { pools = _pool.values(); } for (AsyncPool<Channel> pool : pools) { cancelled.addAll(pool.cancelWaiters()); } return cancelled; }
public TimeoutAsyncPoolHandle( AsyncPool<T> pool, ScheduledExecutorService scheduler, long timeout, TimeUnit unit, T item) { _pool = pool; _timeout = new Timeout<>(scheduler, timeout, unit, item); _timeout.addTimeoutTask(() -> _pool.put(item)); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { AsyncPool<Channel> pool = ctx.channel().attr(CHANNEL_POOL_ATTR_KEY).getAndSet(null); if (pool != null) { // TODO do all exceptions mean we should get rid of the channel? pool.dispose(ctx.channel()); } }
@Override protected void doWriteRequestWithWireAttrHeaders(Request request, RequestContext requestContext, SocketAddress address, Map<String, String> wireAttrs, TimeoutTransportCallback<StreamResponse> callback, long requestTimeout) { final AsyncPool<Channel> pool; try { pool = getChannelPoolManagerPerRequest(request).getPoolForAddress(address); } catch (IllegalStateException e) { errorResponse(callback, e); return; } requestContext.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_1_1); Callback<Channel> getCallback = new ChannelPoolGetCallback(pool, request, requestContext, callback, requestTimeout); final Cancellable pendingGet = pool.get(getCallback); if (pendingGet != null) { callback.addTimeoutTask(pendingGet::cancel); } }
pool.start(); _pool.put(address, pool);
for (AsyncPool<Channel> pool : pools) pool.shutdown(poolCallback);
_pool.put(channel); return;
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { AsyncPool<Channel> pool = ctx.channel().attr(CHANNEL_POOL_ATTR_KEY).getAndSet(null); if (pool != null) { // TODO do all exceptions mean we should get rid of the channel? pool.dispose(ctx.channel()); } }
@Override protected void doWriteRequestWithWireAttrHeaders(Request request, final RequestContext requestContext, SocketAddress address, Map<String, String> wireAttrs, TimeoutTransportCallback<StreamResponse> callback, long requestTimeout) { final AsyncPool<Channel> pool; try { pool = getChannelPoolManagerPerRequest(request).getPoolForAddress(address); } catch (IllegalStateException e) { errorResponse(callback, e); return; } requestContext.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_2); Callback<Channel> getCallback = new ChannelPoolGetCallback(pool, request, requestContext, callback, requestTimeout); final Cancellable pendingGet = pool.get(getCallback); if (pendingGet != null) { callback.addTimeoutTask(pendingGet::cancel); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { AsyncPool<Channel> pool = ctx.channel().attr(CHANNEL_POOL_ATTR_KEY).getAndSet(null); if (pool != null) { RestResponse restResponse = (RestResponse) msg; List<String> connectionTokens = restResponse.getHeaderValues("connection"); if (connectionTokens != null) { for (String token: connectionTokens) { if ("close".equalsIgnoreCase(token)) { pool.dispose(ctx.channel()); return; } } } pool.put(ctx.channel()); } }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { AsyncPool<Channel> pool = ctx.channel().attr(CHANNEL_POOL_ATTR_KEY).getAndSet(null); if (pool != null) { pool.dispose(ctx.channel()); } } }
final Cancellable pendingGet = pool.get(new Callback<Channel>()
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { AsyncPool<Channel> pool = ctx.channel().attr(CHANNEL_POOL_ATTR_KEY).getAndSet(null); if (pool != null) { pool.dispose(ctx.channel()); } } }