Java Code Examples for io.github.resilience4j.ratelimiter.RateLimiter#Metrics
The following examples show how to use
io.github.resilience4j.ratelimiter.RateLimiter#Metrics .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RateLimitersImplementationTest.java From resilience4j with Apache License 2.0 | 6 votes |
protected void waitForRefresh(RateLimiter.Metrics metrics, RateLimiterConfig config, char printedWhileWaiting) { Instant start = Instant.now(); while (Instant.now().isBefore(start.plus(config.getLimitRefreshPeriod()))) { try { if (metrics.getAvailablePermissions() == config.getLimitForPeriod()) { break; } System.out.print(printedWhileWaiting); Thread.sleep(10); } catch (InterruptedException ex) { throw new IllegalStateException(ex); } } System.out.println(); }
Example 2
Source File: RateLimitersHealthIndicator.java From resilience4j with Apache License 2.0 | 6 votes |
private Health mapRateLimiterHealth(RateLimiter rateLimiter) { RateLimiter.Metrics metrics = rateLimiter.getMetrics(); int availablePermissions = metrics.getAvailablePermissions(); int numberOfWaitingThreads = metrics.getNumberOfWaitingThreads(); long timeoutInNanos = rateLimiter.getRateLimiterConfig().getTimeoutDuration().toNanos(); if (availablePermissions > 0 || numberOfWaitingThreads == 0) { return rateLimiterHealth(Status.UP, availablePermissions, numberOfWaitingThreads); } if (rateLimiter instanceof AtomicRateLimiter) { AtomicRateLimiter atomicRateLimiter = (AtomicRateLimiter) rateLimiter; AtomicRateLimiter.AtomicRateLimiterMetrics detailedMetrics = atomicRateLimiter .getDetailedMetrics(); if (detailedMetrics.getNanosToWait() > timeoutInNanos) { boolean allowHealthIndicatorToFail = allowHealthIndicatorToFail(rateLimiter); return rateLimiterHealth(allowHealthIndicatorToFail ? Status.DOWN : new Status("RATE_LIMITED"), availablePermissions, numberOfWaitingThreads); } } return rateLimiterHealth(Status.UNKNOWN, availablePermissions, numberOfWaitingThreads); }
Example 3
Source File: AtomicRateLimiterTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void permissionsInFirstCycle() throws Exception { setup(Duration.ZERO); setTimeOnNanos(CYCLE_IN_NANOS - 10); RateLimiter.Metrics metrics = rateLimiter.getMetrics(); int availablePermissions = metrics.getAvailablePermissions(); then(availablePermissions).isEqualTo(PERMISSIONS_RER_CYCLE); }
Example 4
Source File: AtomicRateLimiterTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void metricsTest() { setup(Duration.ZERO); RateLimiter.Metrics metrics = rateLimiter.getMetrics(); then(metrics.getNumberOfWaitingThreads()).isEqualTo(0); then(metrics.getAvailablePermissions()).isEqualTo(1); AtomicRateLimiter.AtomicRateLimiterMetrics detailedMetrics = rateLimiter .getDetailedMetrics(); then(detailedMetrics.getNumberOfWaitingThreads()).isEqualTo(0); then(detailedMetrics.getAvailablePermissions()).isEqualTo(1); then(detailedMetrics.getNanosToWait()).isEqualTo(0); then(detailedMetrics.getCycle()).isGreaterThan(0); }
Example 5
Source File: SemaphoreBasedRateLimiterImplTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void getMetrics() throws Exception { ScheduledExecutorService scheduler = mock(ScheduledExecutorService.class); SemaphoreBasedRateLimiter limit = new SemaphoreBasedRateLimiter("test", config, scheduler); RateLimiter.Metrics metrics = limit.getMetrics(); then(metrics.getNumberOfWaitingThreads()).isEqualTo(0); }
Example 6
Source File: SemaphoreBasedRateLimiterImplTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void isUpperLimitedForPermissions() throws Exception { ScheduledExecutorService scheduler = mock(ScheduledExecutorService.class); SemaphoreBasedRateLimiter limit = new SemaphoreBasedRateLimiter("test", config, scheduler); RateLimiter.Metrics metrics = limit.getMetrics(); then(metrics.getAvailablePermissions()).isEqualTo(2); limit.refreshLimit(); then(metrics.getAvailablePermissions()).isEqualTo(2); }
Example 7
Source File: SemaphoreBasedRateLimiterImplTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void getDetailedMetrics() throws Exception { ScheduledExecutorService scheduler = mock(ScheduledExecutorService.class); SemaphoreBasedRateLimiter limit = new SemaphoreBasedRateLimiter("test", config, scheduler); RateLimiter.Metrics metrics = limit.getMetrics(); then(metrics.getNumberOfWaitingThreads()).isEqualTo(0); then(metrics.getAvailablePermissions()).isEqualTo(2); }
Example 8
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 5 votes |
private void alignTime(RateLimiter rateLimiter) { RateLimiter.Metrics metrics = rateLimiter.getMetrics(); while (rateLimiter.acquirePermission()) { state = !state; } // Wait to the start of the next cycle in spin loop while (metrics.getAvailablePermissions() == 0) { state = !state; } System.out.println(state); }
Example 9
Source File: SemaphoreBasedRateLimiterImplTest.java From resilience4j with Apache License 2.0 | 4 votes |
@Test public void acquirePermissionAndMetrics() throws Exception { ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class); RateLimiterConfig configSpy = spy(config); SemaphoreBasedRateLimiter limit = new SemaphoreBasedRateLimiter("test", configSpy, scheduledExecutorService); RateLimiter.Metrics detailedMetrics = limit.getMetrics(); SynchronousQueue<Object> synchronousQueue = new SynchronousQueue<>(); Thread thread = new Thread(() -> { run(() -> { for (int i = 0; i < LIMIT; i++) { synchronousQueue.put(O); limit.acquirePermission(); } limit.acquirePermission(); }); }); thread.setDaemon(true); thread.start(); for (int i = 0; i < LIMIT; i++) { synchronousQueue.take(); } awaitImpatiently() .atMost(100, TimeUnit.MILLISECONDS) .until(detailedMetrics::getAvailablePermissions, equalTo(0)); awaitImpatiently() .atMost(2, TimeUnit.SECONDS).until(thread::getState, equalTo(TIMED_WAITING)); then(detailedMetrics.getAvailablePermissions()).isEqualTo(0); limit.refreshLimit(); awaitImpatiently() .atMost(100, TimeUnit.MILLISECONDS) .until(detailedMetrics::getAvailablePermissions, equalTo(1)); awaitImpatiently() .atMost(2, TimeUnit.SECONDS).until(thread::getState, equalTo(TERMINATED)); then(detailedMetrics.getAvailablePermissions()).isEqualTo(1); limit.changeLimitForPeriod(3); limit.refreshLimit(); then(detailedMetrics.getAvailablePermissions()).isEqualTo(3); }