diff --git a/src/main/java/net/spy/memcached/ConnectionFactory.java b/src/main/java/net/spy/memcached/ConnectionFactory.java index 0c0cde78e..a85a6dda3 100644 --- a/src/main/java/net/spy/memcached/ConnectionFactory.java +++ b/src/main/java/net/spy/memcached/ConnectionFactory.java @@ -124,6 +124,15 @@ MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c, */ boolean useNagleAlgorithm(); + /** + * If true, keep alive will be used on connected sockets. + * + *

+ * See {@link java.net.Socket#setKeepAlive(boolean)} for more information. + *

+ */ + boolean getKeepAlive(); + /** * Observers that should be established at the time of connection * instantiation. diff --git a/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java b/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java index e7c74209d..b3ece2eec 100644 --- a/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java +++ b/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java @@ -61,6 +61,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; @@ -222,6 +223,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. */ @@ -400,6 +406,10 @@ public boolean shouldOptimize() { return shouldOptimize; } + public boolean getKeepAlive() { + return keepAlive; + } + @Override public boolean useNagleAlgorithm() { return useNagle; diff --git a/src/main/java/net/spy/memcached/DefaultConnectionFactory.java b/src/main/java/net/spy/memcached/DefaultConnectionFactory.java index bee0c4e30..16623cd3b 100644 --- a/src/main/java/net/spy/memcached/DefaultConnectionFactory.java +++ b/src/main/java/net/spy/memcached/DefaultConnectionFactory.java @@ -388,6 +388,15 @@ public boolean useNagleAlgorithm() { return false; } + /* + * (non-Javadoc) + * + * @see net.spy.memcached.ConnectionFactory#getKeepAlive() + */ + public boolean getKeepAlive() { + return false; + } + /* * (non-Javadoc) * diff --git a/src/main/java/net/spy/memcached/MemcachedConnection.java b/src/main/java/net/spy/memcached/MemcachedConnection.java index 074461728..47f432fb1 100644 --- a/src/main/java/net/spy/memcached/MemcachedConnection.java +++ b/src/main/java/net/spy/memcached/MemcachedConnection.java @@ -47,6 +47,7 @@ import java.io.IOException; import java.net.ConnectException; import java.net.InetSocketAddress; +import java.net.Socket; import java.net.SocketAddress; import java.net.SocketException; import java.nio.ByteBuffer; @@ -349,8 +350,10 @@ protected List createConnections( MemcachedNode qa = connectionFactory.createMemcachedNode(sa, ch, bufSize); qa.setConnection(this); 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)) { getLogger().info("Connected to %s immediately", qa); diff --git a/src/test/java/net/spy/memcached/ConnectionFactoryBuilderTest.java b/src/test/java/net/spy/memcached/ConnectionFactoryBuilderTest.java index 9c64142b0..30ec29348 100644 --- a/src/test/java/net/spy/memcached/ConnectionFactoryBuilderTest.java +++ b/src/test/java/net/spy/memcached/ConnectionFactoryBuilderTest.java @@ -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(), @@ -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()); @@ -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);