use of ratpack.exec.Promise in project ratpack by ratpack.
the class Pac4jAuthenticator method createClients.
private Promise<Clients> createClients(Context ctx, PathBinding pathBinding) throws Exception {
String boundTo = pathBinding.getBoundTo();
PublicAddress publicAddress = ctx.get(PublicAddress.class);
String absoluteCallbackUrl = publicAddress.get(b -> b.maybeEncodedPath(boundTo).maybeEncodedPath(path)).toASCIIString();
Iterable<? extends Client<?, ?>> result = clientsProvider.get(ctx);
@SuppressWarnings("rawtypes") List<Client> clients;
if (result instanceof List) {
clients = Types.cast(result);
} else {
clients = ImmutableList.copyOf(result);
}
return Promise.value(new Clients(absoluteCallbackUrl, clients));
}
use of ratpack.exec.Promise 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.exec.Promise in project ratpack by ratpack.
the class TimedMethodInterceptor method invoke.
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
String timerTag = buildTimerTag(invocation.getMethod().getAnnotation(Timed.class), invocation.getMethod());
final Timer timer = metricRegistry.timer(timerTag);
final Timer.Context timerContext = timer.time();
Object result;
try {
result = invocation.proceed();
if (result instanceof Promise<?>) {
result = ((Promise<?>) result).time(duration -> timer.update(duration.getNano(), TimeUnit.NANOSECONDS));
} else {
timerContext.stop();
}
} catch (Exception e) {
timerContext.stop();
throw e;
}
return result;
}
use of ratpack.exec.Promise 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);
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;
Execution.fork().onStart(execInit).onComplete(e -> {
if (counter.decrementAndGet() == 0) {
d.success(results);
}
}).start(e -> promises.get(finalI).result(t -> {
results.set(finalI, t);
}));
}
});
}
use of ratpack.exec.Promise in project ratpack by ratpack.
the class DefaultParallelBatch method forEach.
@Override
public Operation forEach(BiAction<? super Integer, ? super T> consumer) {
AtomicReference<Throwable> error = new AtomicReference<>();
AtomicBoolean done = new AtomicBoolean();
AtomicInteger wip = new AtomicInteger();
return Promise.async(d -> {
int i = 0;
Iterator<? extends Promise<T>> iterator = promises.iterator();
while (iterator.hasNext()) {
Promise<T> promise = iterator.next();
final int finalI = i++;
wip.incrementAndGet();
if (!iterator.hasNext()) {
done.set(true);
}
Execution.fork().onStart(execInit).onComplete(e -> {
if (wip.decrementAndGet() == 0 && done.get()) {
Throwable t = error.get();
if (t == null) {
d.success(null);
} else {
d.error(t);
}
}
}).start(e -> {
if (error.get() == null) {
promise.result(t -> {
if (t.isError()) {
Throwable thisError = t.getThrowable();
if (!error.compareAndSet(null, thisError)) {
Throwable firstError = error.get();
if (firstError != thisError) {
firstError.addSuppressed(thisError);
}
}
} else {
consumer.execute(finalI, t.getValue());
}
});
}
});
}
if (i == 0) {
d.success(null);
}
}).operation();
}
Aggregations