private void drain() { ExecResult<? extends T> result = ref.get(); if (result == null) { return; } Downstream<? super T> next = listeners.poll(); while (next != null) { next.accept(result); next = listeners.poll(); } }
/** * Convert a {@link CompletableFuture} into a promise. * <p> * @param future the {@link CompletableFuture} to convert into a {@link Promise} * @param <T> The type of the promised value * @return a {@link Promise} that will be consumed on the current execution thread. * @since 1.6 */ static <T> Promise<T> toPromise(CompletableFuture<T> future) { return async(downstream -> downstream.accept(future)); }
public Promise<ResultSet> execute(Statement statement) { return Promise.of(upstream -> { ResultSetFuture resultSetFuture = session.executeAsync(statement); upstream.accept(resultSetFuture); }); }
/** * Creates a new promise for the eventual value. * * @return a new promise for the eventual value */ public Promise<T> promise() { return Promise.async(downstream -> { ExecResult<? extends T> result = ref.get(); if (result == null) { listeners.add(downstream); drain(); } else { downstream.accept(result); } }); }
@Override public void connect(Downstream<? super T> downstream) throws Exception { Cached<? extends T> cached = this.ref.get(); if (needsFetch(cached)) { Promise.<T>async(d -> { waiting.add(d); tryDrain(); }).result(downstream::accept); } else { downstream.accept(cached.result); } }
Downstream<? super T> downstream = waiting.poll(); while (downstream != null) { downstream.accept(cached.result); downstream = waiting.poll();
private void receiveResult(Downstream<? super T> downstream, ExecResult<T> result) { Duration ttl = Duration.ofSeconds(0); try { ttl = ttlFunc.apply(result); } catch (Throwable e) { if (result.isError()) { //noinspection ThrowableResultOfMethodCallIgnored result.getThrowable().addSuppressed(e); } else { result = ExecResult.of(Result.error(e)); } } Instant expiresAt; if (ttl.isNegative()) { expiresAt = null; // eternal upstream = null; // release } else if (ttl.isZero()) { expiresAt = clock.instant().minus(Duration.ofSeconds(1)); } else { expiresAt = clock.instant().plus(ttl); } ref.set(new Cached<>(result, expiresAt)); pending.set(false); downstream.accept(result); tryDrain(); }
).thenAcceptAsync(v -> continuation.resume(() -> downstream.accept(v)), eventLoop)