Skip to content

Commit

Permalink
FEATURE: Add TCP connection keep-alive option.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Sep 14, 2023
1 parent edd4541 commit 6d418fa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ MemcachedNode createMemcachedNode(String name,
*/
boolean useNagleAlgorithm();

/**
* If true, keep alive will be used on connected sockets.
*
* <p>
* See {@link java.net.Socket#setKeepAlive(boolean)} for more information.
* </p>
*/
boolean getKeepAlive();

/**
* Observers that should be established at the time of connection
* instantiation.
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ConnectionFactoryBuilder {
private boolean isDaemon = true;
private boolean shouldOptimize = false;
private boolean useNagle = false;
private boolean keepAlive = false;
//private long maxReconnectDelay =
//DefaultConnectionFactory.DEFAULT_MAX_RECONNECT_DELAY;
private long maxReconnectDelay = 1;
Expand Down Expand Up @@ -467,6 +468,9 @@ public Map<APIType, ReadPriority> getAPIReadPriority() {
}
/* ENABLE_REPLICATION end */

public void setKeepAlive(boolean on) {
keepAlive = on;
}
/**
* Get the ConnectionFactory set up with the provided parameters.
*/
Expand Down Expand Up @@ -591,6 +595,11 @@ public boolean useNagleAlgorithm() {
return useNagle;
}

@Override
public boolean getKeepAlive() {
return keepAlive;
}

@Override
public long getMaxReconnectDelay() {
return maxReconnectDelay;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,10 @@ public Transcoder<Object> getDefaultCollectionTranscoder() {
public boolean useNagleAlgorithm() {
return false;
}

@Override
public boolean getKeepAlive() {
return false;
}
public boolean shouldOptimize() {
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ private MemcachedNode makeMemcachedNode(String name,
SocketChannel ch = SocketChannel.open();
ch.configureBlocking(false);
ch.socket().setTcpNoDelay(!connFactory.useNagleAlgorithm());
ch.socket().setKeepAlive(connFactory.getKeepAlive());
ch.socket().setReuseAddress(true);
/* The codes above can be replaced by the codes below since java 1.7 */
// ch.setOption(StandardSocketOptions.TCP_NODELAY, !f.useNagleAlgorithm());
Expand Down Expand Up @@ -1307,6 +1308,7 @@ private void attemptReconnects() {
ch = SocketChannel.open();
ch.configureBlocking(false);
ch.socket().setTcpNoDelay(!connFactory.useNagleAlgorithm());
ch.socket().setKeepAlive(connFactory.getKeepAlive());
ch.socket().setReuseAddress(true);
/* The codes above can be replaced by the codes below since java 1.7 */
// ch.setOption(StandardSocketOptions.TCP_NODELAY, !f.useNagleAlgorithm());
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/net/spy/memcached/ClientBaseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public boolean useNagleAlgorithm() {
return inner.useNagleAlgorithm();
}

@Override
public boolean getKeepAlive() {
return inner.getKeepAlive();
}

@Override
public Collection<ConnectionObserver> getInitialObservers() {
return observers;
Expand Down

0 comments on commit 6d418fa

Please sign in to comment.