/** Probes {@code url} until it becomes available. */ static void waitUntilAvailable(final URL url, int timeoutMs) { try { Void unusedReturnValue = SimpleTimeLimiter.create(newCachedThreadPool()) .callWithTimeout( () -> { int exponentialBackoffMs = 1; while (true) { if (isAvailable(url)) { return null; } Thread.sleep(exponentialBackoffMs *= 2); } }, timeoutMs, TimeUnit.MILLISECONDS); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } }
/** Stops the HTTP server. */ public void stop() { try { Void unusedReturnValue = SimpleTimeLimiter.create(newCachedThreadPool()) .callWithTimeout( () -> { server.stop(); return null; }, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } }
@Inject public DnsResolverFilter(@Named("dns_resolver_timeout") Period resolverTimeout, @Named("dns_resolver_run_before_extractors") boolean shouldRunBeforeExtractors, @Named("dns_resolver_enabled") boolean enabled, MetricRegistry metricRegistry) { this.shouldRunBeforeExtractors = shouldRunBeforeExtractors; this.enabled = enabled; timeout = resolverTimeout.toStandardDuration().getMillis(); timeLimiter = SimpleTimeLimiter.create( Executors.newSingleThreadExecutor( new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat("dns-resolver-thread-%d") .build() ) ); this.resolveTime = metricRegistry.timer(name(DnsResolverFilter.class, "resolveTime")); this.resolveTimeouts = metricRegistry.meter(name(DnsResolverFilter.class, "resolveTimeouts")); }
@Test public void shouldUseCallWithTimeout() throws Exception { TimeLimiter timeLimiter = new SimpleTimeLimiter(); long start = System.nanoTime(); String result = timeLimiter.callWithTimeout( () -> doSomeHeavyWeightOperation(), ENOUGH_MS, MILLISECONDS, true); assertEquals("done", result); TimeOutAssertion.assertTheCallTookBetween(start, DELAY_MS, ENOUGH_MS); }
public UpsConsoleThread(UpsConsole nanoConsole) { super.setName("UnknownPandaServer|Console Thread"); super.setDaemon(true); this.console = nanoConsole; this.limiter = new SimpleTimeLimiter(); }
@Test public void shouldUseProxy() throws Exception { TimeLimiter timeLimiter = new SimpleTimeLimiter(); long start = System.nanoTime(); HeavyOperation target = () -> doSomeHeavyWeightOperation(); HeavyOperation proxy = timeLimiter.newProxy(target, HeavyOperation.class, ENOUGH_MS, MILLISECONDS); assertEquals("done", proxy.doHeavyWeightOperation()); TimeOutAssertion.assertTheCallTookBetween(start, DELAY_MS, ENOUGH_MS); }
public boolean testWithTimeout(int timeout) { SimpleTimeLimiter limiter = new SimpleTimeLimiter(); boolean result = false; try { limiter.callWithTimeout(new Callable<Boolean>() { public Boolean call() { return test(); } }, timeout, TimeUnit.MILLISECONDS, false); } catch (Exception e) { result = true; loadDescription("Timed out with " + Integer.toString(timeout) + " ms."); } return result; }
public static <T> T tryWith(long timeoutDuration, TimeUnit timeoutUnit, Callable<T> callable) { try { TimeLimiter limiter = new SimpleTimeLimiter(); //noinspection unchecked Callable<T> proxy = limiter.newProxy(callable, Callable.class, timeoutDuration, timeoutUnit); return proxy.call(); } catch (Exception e) { throw uncheckedException().apply(e); } }
public static void tryWith(long timeoutDuration, TimeUnit timeoutUnit, Effect effect) { try { TimeLimiter limiter = new SimpleTimeLimiter(); //noinspection unchecked Effect proxy = limiter.newProxy(effect, Effect.class, timeoutDuration, timeoutUnit); proxy.cause(); } catch (Exception e) { throw uncheckedException().apply(e); } }
public static TimeLimiter create() { return SimpleTimeLimiter.create(new NewRequestThreadExecutorService()); }
private static <T> T timeLimited(T target, Class<T> clazz, Duration timeout, ExecutorService executor) { TimeLimiter limiter = new SimpleTimeLimiter(executor); return limiter.newProxy(target, clazz, timeout.toMillis(), MILLISECONDS); }
@Test(expected = UncheckedTimeoutException.class) public void shouldTriggerTimeout() throws Exception { new SimpleTimeLimiter().callWithTimeout( () -> doSomeHeavyWeightOperation(), NOT_ENOUGH_MS, MILLISECONDS, true); }
DecoratedCallableBuilder() { this(new SimpleTimeLimiter()); }
/** * Timing out after the specified time limit with {@link ExecutorService} * * @param duration * @param timeUnit * @param executor * @return */ public Retryer<R> timeout(long duration, TimeUnit timeUnit, ExecutorService executor) { return timeout(null == executor ? new SimpleTimeLimiter() : new SimpleTimeLimiter(executor), duration, timeUnit); }