Skip to content
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

Add keep alive support #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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
11 changes: 11 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 @@ -405,6 +411,11 @@ public boolean useNagleAlgorithm() {
return useNagle;
}

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

@Override
public long getMaxReconnectDelay() {
return maxReconnectDelay;
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
5 changes: 4 additions & 1 deletion 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 @@ -335,7 +336,9 @@ 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)) {
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