Skip to content

Commit e565704

Browse files
authored
[DE-794] default ttl (#553)
* set default connection ttl to 30s * ttl resilience tests * keep default ttl for VST unchanged
1 parent d5eb3a0 commit e565704

File tree

7 files changed

+78
-3
lines changed

7 files changed

+78
-3
lines changed

core/src/main/java/com/arangodb/ArangoDB.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,10 @@ public Builder maxConnections(final Integer maxConnections) {
513513
}
514514

515515
/**
516-
* Set the maximum time to life of a connection. After this time the connection will be closed automatically.
516+
* Set the time to live of an inactive connection. After this time of inactivity the connection will be
517+
* closed automatically.
517518
*
518-
* @param connectionTtl the maximum time to life of a connection in milliseconds
519+
* @param connectionTtl the time to live of a connection in milliseconds
519520
* @return {@link ArangoDB.Builder}
520521
*/
521522
public Builder connectionTtl(final Long connectionTtl) {

core/src/main/java/com/arangodb/internal/ArangoDefaults.java

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public final class ArangoDefaults {
4545
public static final Protocol DEFAULT_PROTOCOL = Protocol.HTTP2_JSON;
4646
public static final String DEFAULT_USER = "root";
4747
public static final Integer DEFAULT_TIMEOUT = 0;
48+
public static final Long DEFAULT_CONNECTION_TTL_HTTP = 30_000L;
4849
public static final Boolean DEFAULT_USE_SSL = false;
4950
public static final Boolean DEFAULT_VERIFY_HOST = true;
5051
public static final Integer DEFAULT_CHUNK_SIZE = 30_000;

core/src/main/java/com/arangodb/internal/config/ArangoConfig.java

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ public void setMaxConnections(Integer maxConnections) {
219219
}
220220

221221
public Long getConnectionTtl() {
222+
if (connectionTtl == null && getProtocol() != Protocol.VST) {
223+
connectionTtl = ArangoDefaults.DEFAULT_CONNECTION_TTL_HTTP;
224+
}
222225
return connectionTtl;
223226
}
224227

resilience-tests/src/test/java/resilience/ClusterTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static void afterAll() throws IOException {
5252
@BeforeEach
5353
void beforeEach() {
5454
enableAllEndpoints();
55+
logs.reset();
5556
}
5657

5758
protected static List<Endpoint> getEndpoints() {

resilience-tests/src/test/java/resilience/SingleServerTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static void afterAll() throws IOException {
3737
@BeforeEach
3838
void beforeEach() {
3939
getEndpoint().enable();
40+
logs.reset();
4041
}
4142

4243
protected static Endpoint getEndpoint() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package resilience.ttl;
2+
3+
import com.arangodb.ArangoDB;
4+
import com.arangodb.ArangoDBAsync;
5+
import com.arangodb.Protocol;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
import resilience.SingleServerTest;
10+
11+
import java.time.Duration;
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.stream.Stream;
14+
15+
import static org.awaitility.Awaitility.await;
16+
17+
/**
18+
* @author Michele Rastelli
19+
*/
20+
class TtlTest extends SingleServerTest {
21+
22+
static Stream<Arguments> args() {
23+
return Stream.of(
24+
Arguments.of(Protocol.HTTP_JSON, "UNREGISTERED"),
25+
Arguments.of(Protocol.HTTP2_JSON, "OUTBOUND GO_AWAY")
26+
);
27+
}
28+
29+
@ParameterizedTest
30+
@MethodSource("args")
31+
void connectionTtl(Protocol p, String expectedLog) {
32+
ArangoDB arangoDB = dbBuilder()
33+
.connectionTtl(1_000L)
34+
.maxConnections(1)
35+
.protocol(p)
36+
.build();
37+
38+
arangoDB.getVersion();
39+
40+
await()
41+
.timeout(Duration.ofSeconds(3))
42+
.until(() -> logs.getLogs().anyMatch(it -> it.getFormattedMessage().contains(expectedLog)));
43+
44+
arangoDB.getVersion();
45+
arangoDB.shutdown();
46+
}
47+
48+
@ParameterizedTest
49+
@MethodSource("args")
50+
void connectionTtlAsync(Protocol p, String expectedLog) throws ExecutionException, InterruptedException {
51+
ArangoDBAsync arangoDB = dbBuilder()
52+
.connectionTtl(1_000L)
53+
.maxConnections(1)
54+
.protocol(p)
55+
.build()
56+
.async();
57+
58+
arangoDB.getVersion().get();
59+
60+
await()
61+
.timeout(Duration.ofSeconds(3))
62+
.until(() -> logs.getLogs().anyMatch(it -> it.getFormattedMessage().contains(expectedLog)));
63+
64+
arangoDB.getVersion().get();
65+
arangoDB.shutdown();
66+
}
67+
68+
}

vst/src/main/java/com/arangodb/vst/internal/VstConnection.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public synchronized void open() throws IOException {
183183
LOGGER.debug("[" + connectionName + "]: Start Callable");
184184

185185
final long openTime = new Date().getTime();
186-
final Long ttlTime = ttl != null ? openTime + ttl : null;
186+
final Long ttlTime = ttl != null && ttl > 0 ? openTime + ttl : null;
187187
final ChunkStore chunkStore = new ChunkStore(messageStore);
188188
while (true) {
189189
if (ttlTime != null && new Date().getTime() > ttlTime && messageStore.isEmpty()) {

0 commit comments

Comments
 (0)