DefaultProtectedStrategy implements ProviderProtectedStrategy, StatisticCallback { public boolean isAllowRequest(int requestCounter, int totalCounter, int maxThread) { if (methodCounter.get() == 1 || requestCounter == 1) { return true; } if (requestCounter > (maxThread / 2) && totalCounter > (maxThread * 3 / 4)) { return false; } return !(methodCounter.get() >= 4 && totalCounter > (maxThread * 3 / 4) && requestCounter > (maxThread / 4)); } DefaultProtectedStrategy(); @Override void setMethodCounter(AtomicInteger methodCounter); @Override Response call(Request request, Provider<?> provider); boolean isAllowRequest(int requestCounter, int totalCounter, int maxThread); @Override String statisticCallback();  }
@Test public void testIsAllowRequest() { DefaultProtectedStrategy defaultProtectedStrategy = new DefaultProtectedStrategy(); defaultProtectedStrategy.setMethodCounter(new AtomicInteger(PUBLIC_METHOD_COUNT_A)); for (int i = 1; i <= MAX_WORKER_THREAD; i++) { Assert.assertTrue(defaultProtectedStrategy.isAllowRequest(i, i, MAX_WORKER_THREAD)); } defaultProtectedStrategy.setMethodCounter(new AtomicInteger(PUBLIC_METHOD_COUNT_A + PUBLIC_METHOD_COUNT_B)); for (int i = 1; i <= MAX_WORKER_THREAD; i++) { if (i <= (MAX_WORKER_THREAD * 3 / 4)) { Assert.assertTrue(defaultProtectedStrategy.isAllowRequest(i, i, MAX_WORKER_THREAD)); } else { Assert.assertFalse(defaultProtectedStrategy.isAllowRequest(i, i, MAX_WORKER_THREAD)); } } Assert.assertTrue(defaultProtectedStrategy.isAllowRequest(MAX_WORKER_THREAD / 2, MAX_WORKER_THREAD * 3 / 4 + 1, MAX_WORKER_THREAD)); Assert.assertFalse(defaultProtectedStrategy.isAllowRequest(MAX_WORKER_THREAD / 2 + 1, MAX_WORKER_THREAD * 3 / 4 + 1, MAX_WORKER_THREAD)); defaultProtectedStrategy.setMethodCounter(new AtomicInteger(PUBLIC_METHOD_COUNT_A + PUBLIC_METHOD_COUNT_C)); for (int i = 1; i <= MAX_WORKER_THREAD; i++) { if (i <= (MAX_WORKER_THREAD * 3 / 4)) { Assert.assertTrue(defaultProtectedStrategy.isAllowRequest(i, i, MAX_WORKER_THREAD)); } else { Assert.assertFalse(defaultProtectedStrategy.isAllowRequest(i, i, MAX_WORKER_THREAD)); } } Assert.assertTrue(defaultProtectedStrategy.isAllowRequest(MAX_WORKER_THREAD / 4, MAX_WORKER_THREAD * 3 / 4 + 1, MAX_WORKER_THREAD)); Assert.assertFalse(defaultProtectedStrategy.isAllowRequest(MAX_WORKER_THREAD / 4 + 1, MAX_WORKER_THREAD * 3 / 4 + 1, MAX_WORKER_THREAD)); }