Skip to content

Commit 35607a9

Browse files
author
mpv1989
committed
Add connection/handshake retry on same host
1 parent c881a31 commit 35607a9

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ v4.2.2 (xxxx-xx-xx)
77
* added ArangoDB.updateUserDefaultDatabaseAccess(String, Permissions)
88
* added ArangoDB.updateUserDefaultCollectionAccess(String, Permissions)
99
* added ArangoCollection.getDocuments(Collection<String>, Class)
10+
* added connection/handshake retry on same host
1011

1112
v4.2.1 (2017-06-20)
1213
---------------------------

src/main/java/com/arangodb/internal/DefaultHostHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ public class DefaultHostHandler implements HostHandler {
3131
private final List<Host> hosts;
3232
private int current;
3333
private int lastSuccess;
34+
private int iterations;
3435

3536
/**
3637
* @param hosts
3738
*/
3839
public DefaultHostHandler(final List<Host> hosts) {
3940
this.hosts = hosts;
40-
current = lastSuccess = 0;
41+
current = lastSuccess = iterations = 0;
4142
}
4243

4344
@Override
@@ -50,8 +51,9 @@ public Host change() {
5051
current++;
5152
if ((current + 1) > hosts.size()) {
5253
current -= hosts.size();
54+
iterations++;
5355
}
54-
return current != lastSuccess ? get() : null;
56+
return current != lastSuccess || iterations < 3 ? get() : null;
5557
}
5658

5759
@Override

src/main/java/com/arangodb/internal/velocystream/internal/Connection.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,35 @@ public synchronized void open() throws IOException {
103103
}
104104
socket.connect(new InetSocketAddress(host.getHost(), host.getPort()),
105105
timeout != null ? timeout : ArangoDBConstants.DEFAULT_TIMEOUT);
106+
socket.setKeepAlive(true);
107+
socket.setTcpNoDelay(true);
108+
if (LOGGER.isDebugEnabled()) {
109+
LOGGER.debug(String.format("Connected to %s", socket));
110+
}
111+
112+
outputStream = new BufferedOutputStream(socket.getOutputStream());
113+
inputStream = socket.getInputStream();
114+
115+
if (useSsl != null && useSsl) {
116+
if (LOGGER.isDebugEnabled()) {
117+
LOGGER.debug(String.format("Start Handshake on %s", socket));
118+
}
119+
((SSLSocket) socket).startHandshake();
120+
}
106121
hostHandler.success();
107122
break;
108123
} catch (final IOException e) {
109124
hostHandler.fail();
110125
final Host failedHost = host;
111126
host = hostHandler.change();
112127
if (host != null) {
113-
LOGGER.warn(String.format("Could not connect to %s. Try connecting to %s", failedHost, host));
128+
LOGGER.warn(String.format("Could not connect to %s or SSL Handshake failed. Try connecting to %s",
129+
failedHost, host));
114130
} else {
115131
throw e;
116132
}
117133
}
118134
}
119-
socket.setKeepAlive(true);
120-
socket.setTcpNoDelay(true);
121-
if (LOGGER.isDebugEnabled()) {
122-
LOGGER.debug(String.format("Connected to %s", socket));
123-
}
124-
125-
outputStream = new BufferedOutputStream(socket.getOutputStream());
126-
inputStream = socket.getInputStream();
127-
128-
if (useSsl != null && useSsl) {
129-
if (LOGGER.isDebugEnabled()) {
130-
LOGGER.debug(String.format("Start Handshake on %s", socket));
131-
}
132-
((SSLSocket) socket).startHandshake();
133-
}
134135
sendProtocolHeader();
135136
executor = Executors.newSingleThreadExecutor();
136137
executor.submit(new Callable<Void>() {

0 commit comments

Comments
 (0)