use of ratpack.exec.ExecResult in project ratpack by ratpack.
the class BlockingHttpClient method request.
public ReceivedResponse request(HttpClient httpClient, URI uri, ExecController execController, Duration timeout, Action<? super RequestSpec> action) throws Throwable {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<ExecResult<ReceivedResponse>> result = new AtomicReference<>();
execController.fork().start(e -> httpClient.request(uri, action.prepend(s -> s.readTimeout(Duration.ofHours(1)))).map(response -> {
TypedData responseBody = response.getBody();
ByteBuf responseBuffer = responseBody.getBuffer();
ByteBuf heapResponseBodyBuffer = unreleasableBuffer(responseBuffer.isDirect() ? TestByteBufAllocators.LEAKING_UNPOOLED_HEAP.heapBuffer(responseBuffer.readableBytes()).writeBytes(responseBuffer) : responseBuffer.retain());
return new DefaultReceivedResponse(response.getStatus(), response.getHeaders(), new ByteBufBackedTypedData(heapResponseBodyBuffer, responseBody.getContentType()));
}).connect(new Downstream<ReceivedResponse>() {
@Override
public void success(ReceivedResponse value) {
result.set(ExecResult.of(Result.success(value)));
latch.countDown();
}
@Override
public void error(Throwable throwable) {
result.set(ExecResult.of(Result.error(throwable)));
latch.countDown();
}
@Override
public void complete() {
result.set(ExecResult.complete());
latch.countDown();
}
}));
try {
if (!latch.await(timeout.toNanos(), TimeUnit.NANOSECONDS)) {
TemporalUnit unit = timeout.getUnits().get(0);
throw new IllegalStateException("Request to " + uri + " took more than " + timeout.get(unit) + " " + unit.toString() + " to complete");
}
} catch (InterruptedException e) {
throw Exceptions.uncheck(e);
}
return result.get().getValueOrThrow();
}
use of ratpack.exec.ExecResult in project ratpack by ratpack.
the class DefaultParallelBatch method yieldAll.
@Override
public Promise<List<? extends ExecResult<T>>> yieldAll() {
List<Promise<T>> promises = Lists.newArrayList(this.promises);
if (promises.isEmpty()) {
return Promise.value(Collections.emptyList());
}
List<ExecResult<T>> results = Types.cast(promises);
AtomicInteger counter = new AtomicInteger(promises.size());
return Promise.async(d -> {
for (int i = 0; i < promises.size(); ++i) {
final int finalI = i;
// noinspection CodeBlock2Expr
Execution.fork().onStart(execInit).onComplete(e -> {
if (counter.decrementAndGet() == 0) {
d.success(results);
}
}).start(e -> promises.get(finalI).result(t -> {
results.set(finalI, t);
}));
}
});
}
Aggregations