use of com.linkedin.r2.transport.http.client.TimeoutTransportCallback in project rest.li by linkedin.
the class Http2NettyStreamClient method doWriteRequest.
@Override
protected void doWriteRequest(Request request, final RequestContext context, SocketAddress address, TimeoutTransportCallback<StreamResponse> callback) {
final AsyncPool<Channel> pool;
try {
pool = _channelPoolManager.getPoolForAddress(address);
} catch (IllegalStateException e) {
errorResponse(callback, e);
return;
}
context.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_2);
Callback<Channel> getCallback = new ChannelPoolGetCallback(pool, request, callback);
final Cancellable pendingGet = pool.get(getCallback);
if (pendingGet != null) {
callback.addTimeoutTask(() -> pendingGet.cancel());
}
}
use of com.linkedin.r2.transport.http.client.TimeoutTransportCallback in project rest.li by linkedin.
the class TestHttp2AlpnHandler method testChannelCloseBeforeNegotiation.
@Test(timeOut = 10000)
@SuppressWarnings("unchecked")
public void testChannelCloseBeforeNegotiation() throws Exception {
SslContext sslContext = Mockito.mock(SslContext.class);
Http2StreamCodec http2StreamCodec = Mockito.mock(Http2StreamCodec.class);
Http2AlpnHandler handler = new Http2AlpnHandler(sslContext, http2StreamCodec, true, Integer.MAX_VALUE);
EmbeddedChannel channel = new EmbeddedChannel(handler);
RequestWithCallback request = Mockito.mock(RequestWithCallback.class);
TimeoutAsyncPoolHandle handle = Mockito.mock(TimeoutAsyncPoolHandle.class);
TimeoutTransportCallback callback = Mockito.mock(TimeoutTransportCallback.class);
Mockito.when(request.handle()).thenReturn(handle);
Mockito.when(request.callback()).thenReturn(callback);
// Write should not succeed before negotiation completes
Assert.assertFalse(channel.writeOutbound(request));
Assert.assertFalse(channel.finish());
// Synchronously waiting for channel to close
channel.close().sync();
Mockito.verify(request).handle();
Mockito.verify(request).callback();
Mockito.verify(handle).dispose();
Mockito.verify(callback).onResponse(Mockito.any(TransportResponse.class));
}
use of com.linkedin.r2.transport.http.client.TimeoutTransportCallback in project rest.li by linkedin.
the class Http2NettyStreamClient method doWriteRequestWithWireAttrHeaders.
@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);
}
}
use of com.linkedin.r2.transport.http.client.TimeoutTransportCallback in project rest.li by linkedin.
the class HttpNettyClient method doWriteRequest.
@Override
protected void doWriteRequest(RestRequest request, RequestContext requestContext, SocketAddress address, Map<String, String> wireAttrs, final TimeoutTransportCallback<RestResponse> callback, long requestTimeout) {
final RestRequest newRequest = new RestRequestBuilder(request).overwriteHeaders(WireAttributeHelper.toWireAttributes(wireAttrs)).build();
requestContext.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_1_1);
final AsyncPool<Channel> pool;
try {
pool = getChannelPoolManagerPerRequest(request).getPoolForAddress(address);
} catch (IllegalStateException e) {
errorResponse(callback, e);
return;
}
final Cancellable pendingGet = pool.get(new Callback<Channel>() {
@Override
public void onSuccess(final Channel channel) {
// This handler ensures the channel is returned to the pool at the end of the
// Netty pipeline.
channel.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
callback.addTimeoutTask(() -> {
AsyncPool<Channel> pool1 = channel.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY).getAndSet(null);
if (pool1 != null) {
pool1.dispose(channel);
}
});
TransportCallback<RestResponse> sslTimingCallback = SslHandshakeTimingHandler.getSslTimingCallback(channel, requestContext, callback);
// This handler invokes the callback with the response once it arrives.
channel.attr(RAPResponseHandler.CALLBACK_ATTR_KEY).set(sslTimingCallback);
// Set the session validator requested by the user
SslSessionValidator sslSessionValidator = (SslSessionValidator) requestContext.getLocalAttr(R2Constants.REQUESTED_SSL_SESSION_VALIDATOR);
channel.attr(NettyChannelAttributes.SSL_SESSION_VALIDATOR).set(sslSessionValidator);
final NettyClientState state = _state.get();
if (state == NettyClientState.REQUESTS_STOPPING || state == NettyClientState.SHUTDOWN) {
// In this case, we acquired a channel from the pool as request processing is halting.
// The shutdown task might not timeout this callback, since it may already have scanned
// all the channels for pending requests before we set the callback as the channel
// attachment. The TimeoutTransportCallback ensures the user callback in never
// invoked more than once, so it is safe to invoke it unconditionally.
errorResponse(sslTimingCallback, new TimeoutException("Operation did not complete before shutdown"));
// The channel is usually release in two places: timeout or in the netty pipeline.
// Since we call the callback above, the timeout associated will be never invoked. On top of that
// we never send the request to the pipeline (due to the return statement), and nobody is releasing the channel
// until the channel is forcefully closed by the shutdownTimeout. Therefore we have to release it here
AsyncPool<Channel> pool = channel.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY).getAndSet(null);
if (pool != null) {
pool.put(channel);
}
return;
}
// here we want the exception in outbound operations to be passed back through pipeline so that
// the user callback would be invoked with the exception and the channel can be put back into the pool
channel.writeAndFlush(newRequest).addListener(new ErrorChannelFutureListener());
}
@Override
public void onError(Throwable e) {
errorResponse(callback, e);
}
});
if (pendingGet != null) {
callback.addTimeoutTask(pendingGet::cancel);
}
}
use of com.linkedin.r2.transport.http.client.TimeoutTransportCallback in project rest.li by linkedin.
the class HttpNettyStreamClient method doWriteRequestWithWireAttrHeaders.
@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);
}
}
Aggregations