use of com.linkedin.r2.util.Cancellable in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testGetWithNoWaiter.
@Test(expectedExceptions = ExecutionException.class)
public void testGetWithNoWaiter() throws Exception {
AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, NO_WAITER);
pool.start();
FutureCallback<Object> callback = new FutureCallback<>();
Cancellable cancellable = pool.get(callback);
Assert.assertNotNull(cancellable);
Assert.assertFalse(cancellable.cancel());
callback.get(GET_TIMEOUT, TIME_UNIT);
}
use of com.linkedin.r2.util.Cancellable 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.util.Cancellable in project rest.li by linkedin.
the class HttpNettyClient method writeRequest.
private void writeRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, final TimeoutTransportCallback<RestResponse> callback) {
State state = _state.get();
if (state != State.RUNNING) {
errorResponse(callback, new IllegalStateException("Client is " + state));
return;
}
URI uri = request.getURI();
String scheme = uri.getScheme();
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
errorResponse(callback, new IllegalArgumentException("Unknown scheme: " + scheme + " (only http/https is supported)"));
return;
}
String host = uri.getHost();
int port = uri.getPort();
if (port == -1) {
port = "http".equalsIgnoreCase(scheme) ? HTTP_DEFAULT_PORT : HTTPS_DEFAULT_PORT;
}
final RestRequest newRequest = new RestRequestBuilder(request).overwriteHeaders(WireAttributeHelper.toWireAttributes(wireAttrs)).build();
final SocketAddress address;
try {
// TODO investigate DNS resolution and timing
InetAddress inetAddress = InetAddress.getByName(host);
address = new InetSocketAddress(inetAddress, port);
requestContext.putLocalAttr(R2Constants.REMOTE_SERVER_ADDR, inetAddress.getHostAddress());
} catch (UnknownHostException e) {
errorResponse(callback, e);
return;
}
requestContext.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_1_1);
final AsyncPool<Channel> pool;
try {
pool = _channelPoolManager.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(new Runnable() {
@Override
public void run() {
AsyncPool<Channel> pool = channel.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY).getAndRemove();
if (pool != null) {
pool.dispose(channel);
}
}
});
// This handler invokes the callback with the response once it arrives.
channel.attr(RAPResponseHandler.CALLBACK_ATTR_KEY).set(callback);
final State state = _state.get();
if (state == State.REQUESTS_STOPPING || state == State.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(callback, new TimeoutException("Operation did not complete before shutdown"));
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(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
@Override
public void onError(Throwable e) {
errorResponse(callback, e);
}
});
if (pendingGet != null) {
callback.addTimeoutTask(new Runnable() {
@Override
public void run() {
pendingGet.cancel();
}
});
}
}
use of com.linkedin.r2.util.Cancellable in project rest.li by linkedin.
the class HttpNettyStreamClient method doWriteRequest.
@Override
protected void doWriteRequest(Request request, 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_1_1);
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.util.Cancellable in project rest.li by linkedin.
the class TestAsyncSharedPoolImpl method testMixedPutAndDisposeItemSucceeds.
@Test
public void testMixedPutAndDisposeItemSucceeds() throws Exception {
AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
pool.start();
final List<Object> items = new ArrayList<>(GET_COUNT);
for (int i = 0; i < GET_COUNT; i++) {
FutureCallback<Object> getCallback = new FutureCallback<>();
Cancellable cancellable = pool.get(getCallback);
// Operation should not be cancellable
Assert.assertNotNull(cancellable);
Assert.assertEquals(cancellable.cancel(), false);
Object item = getCallback.get(GET_TIMEOUT, TIME_UNIT);
Assert.assertNotNull(item);
items.add(item);
}
// All items should essentially be the same instance
Assert.assertEquals(items.size(), GET_COUNT);
items.stream().forEach(item -> Assert.assertSame(item, items.get(0)));
verifyStats(pool.getStats(), 1, GET_COUNT, 0, 0, 0, 0, 1, 0, 0);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
pool.shutdown(shutdownCallback);
// Put items back to the pool
IntStream.range(0, GET_COUNT).forEach(i -> {
if (i % 2 == 0) {
pool.put(items.get(i));
} else {
pool.dispose(items.get(i));
}
});
shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Aggregations