Skip to content

Commit

Permalink
Resolve hostname of node again during reconnection
Browse files Browse the repository at this point in the history
This is essential when memcached cluster is built on cloud infra, e.g.
k8s, where nodes move and their ips vary.
  • Loading branch information
bigmarvin committed Aug 31, 2020
1 parent eb5d6ee commit 918325e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,20 @@ private void attemptReconnects() {
ch.configureBlocking(false);
ch.socket().setTcpNoDelay(!connectionFactory.useNagleAlgorithm());
int ops = 0;
if (ch.connect(node.getSocketAddress())) {

SocketAddress socketAddress = node.getSocketAddress();

if (socketAddress instanceof InetSocketAddress) {
// resolve the hostname again during reconnect
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
socketAddress = new InetSocketAddress(inetSocketAddress.getHostName(), inetSocketAddress.getPort());

if (!socketAddress.equals(inetSocketAddress)) {
getLogger().info("node address has changed from %s to %s", inetSocketAddress, socketAddress);
}
}

if (ch.connect(socketAddress)) {
connected(node);
addedQueue.offer(node);
getLogger().info("Immediately reconnected to %s", node);
Expand Down

0 comments on commit 918325e

Please sign in to comment.