Skip to content

Add support for turning on tcp keep alive #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@
<loadproperties srcfile="${ivy.dir}/libraries.properties"/>
<property name="ivy.jar" location="${lib.dir}/ivy-${ivy.version}.jar"/>
<property name="ivy_repo_url"
value="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar" />
value="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar" />
<property name="mvn_repo_url"
value="http://repo2.maven.org/maven2/org/apache/maven/maven-ant-tasks/${mvn.version}/maven-ant-tasks-${mvn.version}.jar"/>
value="https://repo1.maven.org/maven2/org/apache/maven/maven-ant-tasks/${mvn.version}/maven-ant-tasks-${mvn.version}.jar"/>
<property name="mvn.jar" location="${build.dir}/maven-ant-tasks-${mvn.version}.jar" />
<property name="build.ivy.dir" location="${build.dir}/ivy" />
<property name="build.ivy.lib.dir" location="${build.ivy.dir}/lib" />
Expand Down
2 changes: 1 addition & 1 deletion ivy/ivysettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<ivysettings>

<property name="repo.maven.org" value="http://repo1.maven.org/maven2/"
<property name="repo.maven.org" value="https://repo1.maven.org/maven2/"
override="false"/>
<property name="ibiblio.maven.org" value="http://www.ibiblio.org/maven2/"
override="false"/>
Expand Down
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 @@ -141,6 +141,15 @@ MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
*/
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
12 changes: 12 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ConnectionFactoryBuilder {
protected boolean isDaemon = false;
protected boolean shouldOptimize = false;
protected boolean useNagle = false;
protected boolean keepAlive = false;
protected long maxReconnectDelay =
DefaultConnectionFactory.DEFAULT_MAX_RECONNECT_DELAY;

Expand Down Expand Up @@ -107,6 +108,7 @@ public ConnectionFactoryBuilder(ConnectionFactory cf) {
setTimeoutExceptionThreshold(cf.getTimeoutExceptionThreshold());
setTranscoder(cf.getDefaultTranscoder());
setUseNagleAlgorithm(cf.useNagleAlgorithm());
setKeepAlive(cf.getKeepAlive());
setEnableMetrics(cf.enableMetrics());
setListenerExecutorService(cf.getListenerExecutorService());
setAuthWaitTime(cf.getAuthWaitTime());
Expand Down Expand Up @@ -240,6 +242,11 @@ public ConnectionFactoryBuilder setUseNagleAlgorithm(boolean to) {
return this;
}

public ConnectionFactoryBuilder setKeepAlive(boolean on) {
keepAlive = on;
return this;
}

/**
* Convenience method to specify the protocol to use.
*/
Expand Down Expand Up @@ -428,6 +435,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 @@ -431,6 +431,13 @@ public boolean useNagleAlgorithm() {
return false;
}

/*
* (non-Javadoc)
*
* @see net.spy.memcached.ConnectionFactory#getKeepAlive()
*/
public boolean getKeepAlive() { return false; }

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -507,6 +514,6 @@ public String toString() {
+ getReadBufSize() + ", Transcoder: " + getDefaultTranscoder()
+ ", Operation Factory: " + getOperationFactory() + " isDaemon: "
+ isDaemon() + ", Optimized: " + shouldOptimize() + ", Using Nagle: "
+ useNagleAlgorithm() + ", ConnectionFactory: " + getName();
+ useNagleAlgorithm() + ", KeepAlive: " + getKeepAlive() + ", ConnectionFactory: " + getName();
}
}
14 changes: 12 additions & 2 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -424,7 +425,11 @@ protected List<MemcachedNode> createConnections(
this.connectionFactory.createMemcachedNode(sa, ch, bufSize);
qa.setNodeEndPoint(endPoint);
int ops = 0;
ch.socket().setTcpNoDelay(!connectionFactory.useNagleAlgorithm());

Socket socket = ch.socket();

socket.setTcpNoDelay(!connectionFactory.useNagleAlgorithm());
socket.setKeepAlive(connectionFactory.getKeepAlive());

try {
if (ch.connect(sa)) {
Expand Down Expand Up @@ -1312,7 +1317,12 @@ private void attemptReconnects() {

ch = SocketChannel.open();
ch.configureBlocking(false);
ch.socket().setTcpNoDelay(!connectionFactory.useNagleAlgorithm());

Socket socket = ch.socket();

socket.setTcpNoDelay(!connectionFactory.useNagleAlgorithm());
socket.setKeepAlive(connectionFactory.getKeepAlive());

int ops = 0;
SocketAddress sa;
if(node.getNodeEndPoint() != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void testDefaults() throws Exception {
assertFalse(f.isDaemon());
assertFalse(f.shouldOptimize());
assertFalse(f.useNagleAlgorithm());
assertFalse(f.getKeepAlive());
assertEquals(f.getOpQueueMaxBlockTime(),
DefaultConnectionFactory.DEFAULT_OP_QUEUE_MAX_BLOCK_TIME);
assertEquals(f.getAuthWaitTime(),
Expand Down Expand Up @@ -140,6 +141,7 @@ public void connectionEstablished(SocketAddress sa, int reconnectCount) {
.setLocatorType(Locator.CONSISTENT).setOpQueueMaxBlockTime(19)
.setAuthDescriptor(anAuthDescriptor)
.setAuthWaitTime(3000)
.setKeepAlive(true)
.build();

assertEquals(4225, f.getOperationTimeout());
Expand All @@ -156,6 +158,7 @@ public void connectionEstablished(SocketAddress sa, int reconnectCount) {
assertTrue(f.isDaemon());
assertFalse(f.shouldOptimize());
assertTrue(f.useNagleAlgorithm());
assertTrue(f.getKeepAlive());
assertEquals(f.getOpQueueMaxBlockTime(), 19);
assertSame(anAuthDescriptor, f.getAuthDescriptor());
assertEquals(f.getAuthWaitTime(), 3000);
Expand Down