use of ratpack.stream.TransformablePublisher in project ratpack by ratpack.
the class DefaultParallelBatch method publisher.
@Override
public TransformablePublisher<T> publisher() {
Iterator<? extends Promise<T>> iterator = promises.iterator();
return new BufferingPublisher<>(Action.noop(), write -> {
return new Subscription() {
volatile boolean cancelled;
volatile boolean complete;
final AtomicLong finished = new AtomicLong();
volatile long started;
@Override
public void request(long n) {
while (n-- > 0 && !cancelled) {
if (iterator.hasNext()) {
++started;
Promise<T> promise = iterator.next();
if (!iterator.hasNext()) {
complete = true;
}
Execution.fork().onStart(execInit).onComplete(e -> {
long finished = this.finished.incrementAndGet();
if (finished == started && complete && !cancelled) {
write.complete();
}
}).start(e -> promise.onError(write::error).then(write::item));
} else {
return;
}
}
}
@Override
public void cancel() {
cancelled = true;
}
};
});
}
use of ratpack.stream.TransformablePublisher in project ratpack by ratpack.
the class DefaultRequestFixture method invoke.
private HandlingResult invoke(Handler handler, Registry registry, DefaultHandlingResult.ResultsHolder results) throws HandlerTimeoutException {
ServerConfig serverConfig = registry.get(ServerConfig.class);
DefaultRequest request = new DefaultRequest(Instant.now(), requestHeaders, HttpMethod.valueOf(method.toUpperCase()), HttpVersion.valueOf(protocol), uri, new InetSocketAddress(remoteHostAndPort.getHostText(), remoteHostAndPort.getPort()), new InetSocketAddress(localHostAndPort.getHostText(), localHostAndPort.getPort()), serverConfig, new RequestBodyReader() {
@Override
public long getContentLength() {
return requestBody.readableBytes();
}
@Override
public Promise<? extends ByteBuf> read(long maxContentLength, Block onTooLarge) {
return Promise.value(requestBody).route(r -> r.readableBytes() > maxContentLength, onTooLarge.action());
}
@Override
public TransformablePublisher<? extends ByteBuf> readStream(long maxContentLength) {
return Streams.<ByteBuf>yield(r -> {
if (r.getRequestNum() > 0) {
return null;
} else {
return requestBody;
}
});
}
});
if (pathBinding != null) {
handler = Handlers.chain(ctx -> {
ctx.getExecution().get(PathBindingStorage.TYPE).push(pathBinding);
ctx.next();
}, handler);
}
try {
return new DefaultHandlingResult(request, results, responseHeaders, registry, timeout, handler);
} catch (Exception e) {
throw Exceptions.uncheck(e);
} finally {
registry.get(ExecController.class).close();
}
}
Aggregations