/** * Blocks on the input fibers and creates a new list from the results. The result list is the same order as the * input list. * * @param fibers to combine */ public static <V> List<V> get(final List<Fiber<V>> fibers) throws InterruptedException { final List<V> results = new ArrayList<>(fibers.size()); //TODO on interrupt, should all input fibers be canceled? for (final Fiber<V> f : fibers) { try { results.add(f.get()); } catch (ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } } return Collections.unmodifiableList(results); }
/** * Blocks on the input fibers and creates a new list from the results. The result list is the same order as the * input list. * * @param timeout to wait for all requests to complete * @param unit the time is in * @param fibers to combine */ public static <V> List<V> get(long timeout, TimeUnit unit, List<Fiber<V>> fibers) throws InterruptedException, TimeoutException { if (unit == null) return get(fibers); if (timeout < 0) timeout = 0; final List<V> results = new ArrayList<>(fibers.size()); long left = unit.toNanos(timeout); final long deadline = System.nanoTime() + left; //TODO on interrupt, should all input fibers be canceled? try { for (final Fiber<V> f : fibers) { if (left >= 0) { results.add(f.get(left, TimeUnit.NANOSECONDS)); left = deadline - System.nanoTime(); } else throw new TimeoutException("timed out sequencing fiber results"); } return Collections.unmodifiableList(results); } catch (ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } }
@Override public CloseableHttpResponse execute(final long nanoTime, final HttpRequestBase request) throws SuspendExecution, InterruptedException { // TODO See if timeout can be configured per-request final CloseableHttpResponse ret; try { ret = new Fiber<>((SuspendableCallable<CloseableHttpResponse>) () -> { try { return client.execute(request); } catch (final IOException e) { throw Exceptions.rethrowUnwrap(e); } }).start().get(); } catch (final ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } if (validator != null) { validator.validate(ret); } return ret; }