use of com.linkedin.r2.transport.http.client.AsyncPool in project rest.li by linkedin.
the class TestAsyncPool method testGetStatsWithErrors.
@Test
public void testGetStatsWithErrors() throws Exception {
final int POOL_SIZE = 25;
final int GET = 20;
final int PUT_BAD = 5;
final int DISPOSE = 7;
final int CREATE_BAD = 9;
final int TIMEOUT = 100;
final UnreliableLifecycle lifecycle = new UnreliableLifecycle();
final AsyncPool<AtomicBoolean> pool = new AsyncPoolImpl<>("object pool", lifecycle, POOL_SIZE, TIMEOUT, _executor);
PoolStats stats;
final List<AtomicBoolean> objects = new ArrayList<>();
pool.start();
// do a few gets
for (int i = 0; i < GET; i++) {
FutureCallback<AtomicBoolean> cb = new FutureCallback<>();
pool.get(cb);
AtomicBoolean obj = cb.get();
objects.add(obj);
}
// put and destroy some, with errors
lifecycle.setFail(true);
for (int i = 0; i < PUT_BAD; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
obj.set(false);
pool.put(obj);
}
for (int i = 0; i < DISPOSE; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
pool.dispose(obj);
}
stats = pool.getStats();
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD + DISPOSE);
// create some with errors
for (int i = 0; i < CREATE_BAD; i++) {
FutureCallback<AtomicBoolean> cb = new FutureCallback<>();
try {
pool.get(cb);
} catch (Exception e) {
// this error is expected
}
}
stats = pool.getStats();
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getTotalCreateErrors(), CREATE_BAD);
}
use of com.linkedin.r2.transport.http.client.AsyncPool in project rest.li by linkedin.
the class TestHttpNettyClient method testFailBackoff.
@Test
public void testFailBackoff() throws Exception {
final int WARM_UP = 10;
final int N = 5;
final int MAX_RATE_LIMITING_PERIOD = 500;
final CountDownLatch warmUpLatch = new CountDownLatch(WARM_UP);
final CountDownLatch latch = new CountDownLatch(N);
final AtomicReference<Boolean> isShutdown = new AtomicReference<>(false);
AsyncPool<Channel> testPool = new AsyncPoolImpl<>("test pool", new AsyncPool.Lifecycle<Channel>() {
@Override
public void create(Callback<Channel> callback) {
if (warmUpLatch.getCount() > 0) {
warmUpLatch.countDown();
} else {
latch.countDown();
}
callback.onError(new Throwable("Oops..."));
}
@Override
public boolean validateGet(Channel obj) {
return false;
}
@Override
public boolean validatePut(Channel obj) {
return false;
}
@Override
public void destroy(Channel obj, boolean error, Callback<Channel> callback) {
}
@Override
public PoolStats.LifecycleStats getStats() {
return null;
}
}, 200, 30000, _scheduler, Integer.MAX_VALUE, AsyncPoolImpl.Strategy.MRU, 0, new ExponentialBackOffRateLimiter(0, MAX_RATE_LIMITING_PERIOD, Math.max(10, MAX_RATE_LIMITING_PERIOD / 32), _scheduler), new SettableClock(), new LongTracking());
HttpNettyClient client = new HttpNettyClient(address -> testPool, _scheduler, MAX_RATE_LIMITING_PERIOD * 2, 500);
final RestRequest r = new RestRequestBuilder(URI.create("http://localhost:8080/")).setMethod("GET").build();
final ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
while (!isShutdown.get()) {
try {
FutureCallback<RestResponse> callback = new FutureCallback<>();
client.restRequest(r, new RequestContext(), new HashMap<>(), new TransportCallbackAdapter<>(callback));
callback.get();
} catch (Exception e) {
// ignore
}
}
});
// First ensure a bunch fail to get the rate limiting going
warmUpLatch.await(120, TimeUnit.SECONDS);
// Now we should be rate limited
long start = System.currentTimeMillis();
System.err.println("Starting at " + start);
long lowTolerance = N * MAX_RATE_LIMITING_PERIOD * 4 / 5;
long highTolerance = N * MAX_RATE_LIMITING_PERIOD * 5 / 4;
Assert.assertTrue(latch.await(highTolerance, TimeUnit.MILLISECONDS), "Should have finished within " + highTolerance + "ms");
long elapsed = System.currentTimeMillis() - start;
Assert.assertTrue(elapsed > lowTolerance, "Should have finished after " + lowTolerance + "ms (took " + elapsed + ")");
// shutdown everything
isShutdown.set(true);
executor.shutdown();
}
use of com.linkedin.r2.transport.http.client.AsyncPool in project rest.li by linkedin.
the class ChannelPoolHandler method channelRead.
@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());
}
}
Aggregations