From bfa7f2aed010d0ca2b099cb5ab1b585dc9a786c0 Mon Sep 17 00:00:00 2001 From: brido4125 Date: Wed, 13 Sep 2023 16:09:46 +0900 Subject: [PATCH] FEATURE: Add TCP connection keep-alive option. --- .../java/net/spy/memcached/ConnectionFactoryBuilder.java | 6 ++++++ src/main/java/net/spy/memcached/MemcachedConnection.java | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java b/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java index 62a68998f..13b9ac3b1 100644 --- a/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java +++ b/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java @@ -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; } @@ -467,6 +469,9 @@ public Map getAPIReadPriority() { } /* ENABLE_REPLICATION end */ + public void setKeepAlive(boolean on) { + keepAlive = on; + } /** * Get the ConnectionFactory set up with the provided parameters. */ @@ -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 */ diff --git a/src/main/java/net/spy/memcached/MemcachedConnection.java b/src/main/java/net/spy/memcached/MemcachedConnection.java index 351a44b9f..333d69750 100644 --- a/src/main/java/net/spy/memcached/MemcachedConnection.java +++ b/src/main/java/net/spy/memcached/MemcachedConnection.java @@ -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. * @@ -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); @@ -1822,4 +1825,8 @@ public void switchover() { } } /* ENABLE_REPLICATION end */ + + public void setKeepAlive(boolean on) { + keepAlive = on; + } }