public Object withTimeout()

in safeguard-impl/src/main/java/org/apache/safeguard/impl/timeout/TimeoutInterceptor.java [59:89]


    public Object withTimeout(final InvocationContext context) throws Exception {
        final Map<Key, Model> timeouts = cache.getTimeouts();
        final Key key = new Key(context, cache.getUnwrappedCache().getUnwrappedCache());
        Model model = timeouts.get(key);
        if (model == null) {
            model = cache.create(context);
            timeouts.putIfAbsent(key, model);
        }
        if (model.disabled) {
            return context.proceed();
        }

        final FutureTask<Object> task = new FutureTask<>(context::proceed);
        final long start = System.nanoTime();
        executor.execute(task);
        try {
            final Object result = task.get(model.timeout, NANOSECONDS);
            model.successes.inc();
            return result;
        } catch (final ExecutionException ee) {
            cancel(task);
            throw toCause(ee);
        } catch (final TimeoutException te) {
            model.timeouts.inc();
            cancel(task);
            throw new org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException(te);
        } finally {
            final long end = System.nanoTime();
            model.executionDuration.update(end - start);
        }
    }