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 13, 2023
1 parent 5a3d4db commit bfa7f2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public class ConnectionFactoryBuilder {
private boolean arcusMigrationCheck = false; // for arcus users
private boolean arcusMigrEnabled = false; // for internal

protected boolean keepAlive = false;

public void internalArcusMigrEnabled(boolean b) {
arcusMigrEnabled = b;
}
Expand Down Expand Up @@ -467,6 +469,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 All @@ -483,6 +488,7 @@ public MemcachedConnection createConnection(String name,
/* ENABLE_MIGRATION if */
c.setArcusMigrEnabled(arcusMigrEnabled);
/* ENABLE_MIGRATION end */
c.setKeepAlive(keepAlive);
return c;
}
/* ENABLE_REPLICATION end */
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public final class MemcachedConnection extends SpyObject {
new DelayedSwitchoverGroups(DELAYED_SWITCHOVER_TIMEOUT_MILLISECONDS);
/* ENABLE_REPLICATION end */

private boolean keepAlive = false;

/**
* Construct a memcached connection.
*
Expand Down Expand Up @@ -631,6 +633,7 @@ private MemcachedNode makeMemcachedNode(String name,
ch.configureBlocking(false);
ch.socket().setTcpNoDelay(!connFactory.useNagleAlgorithm());
ch.socket().setReuseAddress(true);
ch.socket().setKeepAlive(keepAlive);
/* The codes above can be replaced by the codes below since java 1.7 */
// ch.setOption(StandardSocketOptions.TCP_NODELAY, !f.useNagleAlgorithm());
// ch.setOption(StandardSocketOptions.SO_REUSEADDR, true);
Expand Down Expand Up @@ -1822,4 +1825,8 @@ public void switchover() {
}
}
/* ENABLE_REPLICATION end */

public void setKeepAlive(boolean on) {
keepAlive = on;
}
}

0 comments on commit bfa7f2a

Please sign in to comment.