@Test void attemptOne() { long base = System.nanoTime(); AtomicLong extra = new AtomicLong(); Ticker testTicker = new Ticker() { @Override public long read() { return base + extra.get(); } }; Ratelimiter ratelimiter = new GuavaCacheRatelimiter(1000, TimeUnit.MILLISECONDS, testTicker); assertTrue(ratelimiter.attempt(InetAddress.getLoopbackAddress())); assertFalse(ratelimiter.attempt(InetAddress.getLoopbackAddress())); extra.addAndGet(TimeUnit.SECONDS.toNanos(2)); assertTrue(ratelimiter.attempt(InetAddress.getLoopbackAddress())); }
@Override public boolean attempt(InetAddress address) { Preconditions.checkNotNull(address, "address"); long expectedNewValue = System.nanoTime() + timeoutNanos; long last; try { last = expiringCache.get(address, () -> expectedNewValue); } catch (ExecutionException e) { throw new AssertionError(e); } return expectedNewValue == last; }
GuavaCacheRatelimiter implements Ratelimiter { @Override public boolean attempt(InetAddress address) { Preconditions.checkNotNull(address, "address"); long expectedNewValue = System.nanoTime() + timeoutNanos; long last; try { last = expiringCache.get(address, () -> expectedNewValue); } catch (ExecutionException e) { throw new AssertionError(e); } return expectedNewValue == last; } }
GuavaCacheRatelimiter implements Ratelimiter { @Override public boolean attempt(InetAddress address) { Preconditions.checkNotNull(address, "address"); long expectedNewValue = System.nanoTime() + timeoutNanos; long last; try { last = expiringCache.get(address, () -> expectedNewValue); } catch (ExecutionException e) { throw new AssertionError(e); } return expectedNewValue == last; } GuavaCacheRatelimiter(long time, TimeUnit unit); @VisibleForTesting  GuavaCacheRatelimiter(long time, TimeUnit unit, Ticker ticker); }
GuavaCacheRatelimiter implements Ratelimiter { @Override public boolean attempt(InetAddress address) { Preconditions.checkNotNull(address, "address"); long expectedNewValue = System.nanoTime() + timeoutNanos; long last; try { last = expiringCache.get(address, () -> expectedNewValue); } catch (ExecutionException e) { throw new AssertionError(e); } return expectedNewValue == last; } GuavaCacheRatelimiter(long time, TimeUnit unit); @VisibleForTesting  GuavaCacheRatelimiter(long time, TimeUnit unit, Ticker ticker); @Override boolean attempt(InetAddress address); }
GuavaCacheRatelimiter implements Ratelimiter { @Override public boolean attempt(InetAddress address) { Preconditions.checkNotNull(address, "address"); long expectedNewValue = System.nanoTime() + timeoutNanos; long last; try { last = expiringCache.get(address, () -> expectedNewValue); } catch (ExecutionException e) { throw new AssertionError(e); } return expectedNewValue == last; } GuavaCacheRatelimiter(long time, TimeUnit unit); @VisibleForTesting  GuavaCacheRatelimiter(long time, TimeUnit unit, Ticker ticker); @Override boolean attempt(InetAddress address);  }