Skip to content

Commit

Permalink
Add support for turning on tcp keep alive.
Browse files Browse the repository at this point in the history
Change-Id: Ic561f87fe262782a607e86ac8b2e0efb69d2c46e
Reviewed-on: http://review.couchbase.org/81810
Reviewed-by: Matt Ingenthron <[email protected]>
Tested-by: Matt Ingenthron <[email protected]>
  • Loading branch information
youngm authored and ingenthr committed Aug 3, 2017
1 parent 8fe2e33 commit c232307
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
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 @@ -124,6 +124,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
10 changes: 10 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -400,6 +406,10 @@ public boolean shouldOptimize() {
return shouldOptimize;
}

public boolean getKeepAlive() {
return keepAlive;
}

@Override
public boolean useNagleAlgorithm() {
return useNagle;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/spy/memcached/DefaultConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,15 @@ public boolean useNagleAlgorithm() {
return false;
}

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

/*
* (non-Javadoc)
*
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -349,8 +350,10 @@ protected List<MemcachedNode> 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);
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

0 comments on commit c232307

Please sign in to comment.