mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 01:06:50 +00:00
Add a utility method that returns a cancellation-safe result from
`database.runAsync`
This commit is contained in:
1 parent
bb3ad8d116
commit
607c839dda
3 files changed
+28
-4
No files matched your search
+3
-3
@@ -307,7 +307,7 @@ class FoundationDbMessagePublisher {
|
||||
///
|
||||
/// @return a future of a list of [FoundationDbMessageStreamEntry.Message] with a max size of [#maxMessagesPerScan]
|
||||
private CompletableFuture<List<FoundationDbMessageStreamEntry.Message>> getMessagesBatch() {
|
||||
return database.runAsync(transaction ->
|
||||
return FoundationDbUtil.safeRunAsync(database, transaction ->
|
||||
transaction.getRange(beginKeyCursor, endKeyExclusive, maxMessagesPerScan, false, StreamingMode.EXACT).asList()
|
||||
.thenApply(keyValues -> {
|
||||
if (keyValues.size() < maxMessagesPerScan && !terminateOnQueueEmpty) {
|
||||
@@ -433,7 +433,7 @@ class FoundationDbMessagePublisher {
|
||||
return CompletableFuture.failedFuture(new IllegalStateException("Publisher already terminated"));
|
||||
}
|
||||
|
||||
return database.runAsync(transaction -> {
|
||||
return FoundationDbUtil.safeRunAsync(database, transaction -> {
|
||||
transaction.set(presenceKey, FoundationDbMessageStore.getPresenceValue(clock.instant(), streamId));
|
||||
return CompletableFuture.completedFuture(null);
|
||||
});
|
||||
@@ -445,7 +445,7 @@ class FoundationDbMessagePublisher {
|
||||
renewPresenceFuture.cancel(true);
|
||||
|
||||
renewPresenceFuture.whenComplete((_, _) ->
|
||||
database.runAsync(transaction -> transaction.get(presenceKey).thenAccept(presenceValue -> {
|
||||
FoundationDbUtil.safeRunAsync(database, transaction -> transaction.get(presenceKey).thenAccept(presenceValue -> {
|
||||
if (presenceValue == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
+1
-1
@@ -206,7 +206,7 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
///
|
||||
/// @return a [KeySelector] for the first key greater than the current greatest key in the device queue.
|
||||
private CompletableFuture<Optional<KeySelector>> getEndOfQueueKeyExclusive(final Database database) {
|
||||
return database.runAsync(transaction ->
|
||||
return FoundationDbUtil.safeRunAsync(database, transaction ->
|
||||
transaction.getRange(deviceQueueSubspace.range(), 1, true, StreamingMode.EXACT).asList())
|
||||
.<Optional<KeySelector>>thenApply(items -> {
|
||||
if (items.isEmpty()) {
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package org.whispersystems.textsecuregcm.storage.foundationdb;
|
||||
|
||||
import com.apple.foundationdb.Database;
|
||||
import com.apple.foundationdb.Transaction;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FoundationDbUtil {
|
||||
|
||||
/// Returns a cancellation-safe version of the result from [Database#runAsync(Function)]. Since the final stage
|
||||
/// of the result from [Database#runAsync(Function)] is a cleanup stage that closes the transaction, the
|
||||
/// transaction leaks if the future gets cancelled and the cleanup stage is skipped. So, we add another dummy stage
|
||||
/// that serves as the cancellation target and the cleanup can proceed as normal.
|
||||
///
|
||||
/// @param database the FoundationDB database instance
|
||||
/// @param retryable the block of transaction logic to execute
|
||||
/// @param <T> the return type of retryable
|
||||
/// @return a cancellation-safe version of the future returned from [Database#runAsync(Function)]
|
||||
public static <T> CompletableFuture<T> safeRunAsync(final Database database,
|
||||
final Function<? super Transaction, ? extends CompletableFuture<T>> retryable) {
|
||||
return database.runAsync(retryable)
|
||||
.thenApply(Function.identity());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user