use of com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter in project rest.li by linkedin.
the class TestRateLimiter method testSimple.
@Test
public void testSimple() throws Exception {
final int total = 10;
// NB on Solaris x86 there seems to be an extra 10ms that gets added to the period; need
// to figure this out. For now set the period high enough that period + 10 will be within
// the tolerance.
final int period = 100;
final CountDownLatch latch = new CountDownLatch(total);
final Task incr = new Task() {
@Override
public void run(SimpleCallback doneCallback) {
latch.countDown();
doneCallback.onDone();
}
};
RateLimiter limiter = new ExponentialBackOffRateLimiter(period, period, period, _executor);
limiter.setPeriod(period);
long start = System.currentTimeMillis();
long lowTolerance = (total * period) * 4 / 5;
long highTolerance = (total * period) * 5 / 4;
for (int i = 0; i < total * period; i++) {
limiter.submit(incr);
}
Assert.assertTrue(latch.await(highTolerance, TimeUnit.MILLISECONDS), "Should have finished within " + highTolerance + "ms");
long t = System.currentTimeMillis() - start;
Assert.assertTrue(t > lowTolerance, "Should have finished after " + lowTolerance + "ms (took " + t + ")");
}
use of com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter 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));
HttpNettyClient client = new HttpNettyClient(address -> testPool, _scheduler, 500, 500, 1024 * 1024 * 2);
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<RestResponse>(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.ExponentialBackOffRateLimiter in project rest.li by linkedin.
the class TestRateLimiter method testSetPeriod.
@Test
public void testSetPeriod() throws Exception {
final RateLimiter rl = new ExponentialBackOffRateLimiter(0, MAXPERIOD, 20, _executor);
final ScheduledFuture<?> future = _executor.scheduleAtFixedRate(new Runnable() {
private int _period = 300;
@Override
public void run() {
rl.setPeriod(_period);
_period += 50;
}
}, 0, 100, TimeUnit.MILLISECONDS);
RateLimiterRunner runner = new RateLimiterRunner(System.currentTimeMillis());
rl.submit(runner);
Assert.assertTrue(runner.getElapsedTime() <= MAXPERIOD, "Elapsed Time exceed MAX Period");
future.cancel(false);
}
use of com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter in project rest.li by linkedin.
the class TestRateLimiter method testMaxRunningTasks.
@Test
public void testMaxRunningTasks() throws Exception {
final int total = 20;
final int maxRunning = 5;
final int period = 100;
final Random rand = new Random();
final CountDownLatch latch = new CountDownLatch(total);
final AtomicInteger totalStarted = new AtomicInteger();
final AtomicInteger totalFinished = new AtomicInteger();
final Task r = new Task() {
@Override
public void run(final SimpleCallback callback) {
totalStarted.incrementAndGet();
int delay = period + rand.nextInt(period);
_executor.schedule(new Runnable() {
@Override
public void run() {
totalFinished.incrementAndGet();
callback.onDone();
}
}, delay, TimeUnit.MILLISECONDS);
latch.countDown();
}
};
RateLimiter limiter = new ExponentialBackOffRateLimiter(period, period, period, _executor, maxRunning);
limiter.setPeriod(period);
for (int i = 0; i < total; ++i) {
limiter.submit(r);
}
// check the current number of concurrent tasks every 100ms.
for (int i = 0; i < total * 2; ++i) {
int currentRunning = totalStarted.get() - totalFinished.get();
Assert.assertTrue(currentRunning <= maxRunning, "Should have less than " + maxRunning + " concurrent tasks");
Thread.sleep(period);
}
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(total, totalStarted.get());
}
Aggregations