Skip to content

Commit 8ac448c

Browse files
authored
Async driver request timeout (#387)
* async driver request timeout * CompletableFutureUtils refactoring * test fixes * test fixes
1 parent b4bf612 commit 8ac448c

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.arangodb.async.internal.utils;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.ScheduledExecutorService;
6+
import java.util.concurrent.ScheduledFuture;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.TimeoutException;
9+
10+
public class CompletableFutureUtils {
11+
12+
private CompletableFutureUtils() {
13+
}
14+
15+
private static final ScheduledExecutorService timeoutScheduler = Executors.newSingleThreadScheduledExecutor(r -> {
16+
Thread t = Executors.defaultThreadFactory().newThread(r);
17+
t.setDaemon(true);
18+
return t;
19+
}
20+
);
21+
22+
public static <T> CompletableFuture<T> orTimeout(CompletableFuture<T> completableFuture, long timeout, TimeUnit unit) {
23+
ScheduledFuture<?> timeoutTask = timeoutScheduler.schedule(() ->
24+
completableFuture.completeExceptionally(new TimeoutException()), timeout, unit);
25+
completableFuture.whenComplete((v, e) -> timeoutTask.cancel(false));
26+
return completableFuture;
27+
}
28+
29+
}

src/main/java/com/arangodb/async/internal/velocystream/VstConnectionAsync.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.arangodb.async.internal.velocystream;
2222

23+
import com.arangodb.async.internal.utils.CompletableFutureUtils;
2324
import com.arangodb.internal.net.HostDescription;
2425
import com.arangodb.internal.velocystream.internal.Chunk;
2526
import com.arangodb.internal.velocystream.internal.Message;
@@ -30,6 +31,7 @@
3031
import java.util.Collection;
3132
import java.util.concurrent.CompletableFuture;
3233
import java.util.concurrent.FutureTask;
34+
import java.util.concurrent.TimeUnit;
3335

3436
/**
3537
* @author Mark Vollmary
@@ -54,7 +56,11 @@ public synchronized CompletableFuture<Message> write(final Message message, fina
5456
});
5557
messageStore.storeMessage(message.getId(), task);
5658
super.writeIntern(message, chunks);
57-
return future;
59+
if (timeout == null || timeout == 0L) {
60+
return future;
61+
} else {
62+
return CompletableFutureUtils.orTimeout(future, timeout, TimeUnit.MILLISECONDS);
63+
}
5864
}
5965

6066
@Override

src/test/resources/arangodb.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ arangodb.hosts=172.28.3.1:8529
22
arangodb.connections.max=1
33
arangodb.acquireHostList=true
44
arangodb.password=test
5+
arangodb.timeout=30000

0 commit comments

Comments
 (0)