Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

issue-581 -- Support a maximum connect time on a connection #582

Merged
merged 1 commit into from

2 participants

@alcanzar

these are my changes to make connecting through a firewall easier

@zznate
Collaborator

Looks good - thanks a bunch for the figuring out the pull request.

@zznate zznate merged commit 6285932 into hector-client:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
8 core/src/main/java/me/prettyprint/cassandra/connection/ConcurrentHClientPool.java
@@ -71,7 +71,13 @@ public HClient borrowClient() throws HectorException {
int currentActiveClients = activeClientsCount.incrementAndGet();
try {
-
+ if (cassandraClient.getCassandraHost().getMaxConnectTimeMillis() > 0
+ && System.currentTimeMillis() - cassandraClient.getCreatedTime() > cassandraClient.getCassandraHost().getMaxConnectTimeMillis()) {
+ log.info("Closing connection to {} due to idle time of {} ms", cassandraClient.getCassandraHost().getHost(),
+ System.currentTimeMillis() - cassandraClient.getCreatedTime());
+ cassandraClient.close();
+ cassandraClient = null;
+ }
if ( cassandraClient == null ) {
if (currentActiveClients <= cassandraHost.getMaxActive()) {
View
6 core/src/main/java/me/prettyprint/cassandra/connection/client/HClient.java
@@ -17,6 +17,12 @@
*
*/
public interface HClient {
+ /**
+ * Returns the time that this HClient was created.
+ *
+ * @return the time this client was created
+ */
+ long getCreatedTime();
/**
* Returns a new Cassandra.Client on each invocation using the underlying
View
8 core/src/main/java/me/prettyprint/cassandra/connection/client/HThriftClient.java
@@ -36,6 +36,7 @@
* <p>
*/
public class HThriftClient implements HClient {
+ private long createdTime = System.currentTimeMillis();
private static Logger log = LoggerFactory.getLogger(HThriftClient.class);
@@ -287,4 +288,11 @@ public void setAuthenticated(Map<String, String> credentials) {
clearAuthentication();
this.credentials.putAll(credentials);
}
+
+ /**
+ * {@inheritDoc}
+ */
+ public long getCreatedTime() {
+ return createdTime;
+ }
}
View
14 core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java
@@ -40,6 +40,11 @@
public static final long DEFAULT_MAX_WAITTIME_WHEN_EXHAUSTED = -1;
public static final boolean DEFAULT_LIFO = true;
+ /**
+ * The default number of milliseconds we'll allow a connection to stay open. Default value
+ * is negative which means indefinitely.
+ */
+ public static final long DEFAULT_MAX_CONNECT_TIME = -1;
private final String host, ip, url;
private final int port;
@@ -54,6 +59,7 @@
private boolean useThriftFramedTransport = DEFAULT_USE_FRAMED_THRIFT_TRANSPORT;
private int maxFrameSize = DEFAULT_MAX_FRAME_SIZE;
private boolean useSocketKeepalive;
+ private long maxConnectTimeMillis = DEFAULT_MAX_CONNECT_TIME;
//TODO(ran): private FailoverPolicy failoverPolicy = DEFAULT_FAILOVER_POLICY;
public CassandraHost(String url) {
@@ -204,5 +210,13 @@ public void setUseSocketKeepalive(boolean useSocketKeepalive) {
this.useSocketKeepalive = useSocketKeepalive;
}
+ public long getMaxConnectTimeMillis() {
+ return this.maxConnectTimeMillis ;
+ }
+
+ public void setMaxConnectTimeMillis(long maxConnectTimeMillis) {
+ this.maxConnectTimeMillis = maxConnectTimeMillis;
+ }
+
}
View
24 core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java
@@ -51,7 +51,7 @@
private boolean useSocketKeepalive = false;
private HOpTimer opTimer = new NullOpTimer();
private Class<? extends HClientFactory> clientFactoryClass = HThriftClientFactoryImpl.class;
-
+ private long maxConnectTimeMillis = CassandraHost.DEFAULT_MAX_CONNECT_TIME;
public CassandraHostConfigurator() {
this.hosts = null;
@@ -83,6 +83,7 @@ public void applyConfig(CassandraHost cassandraHost) {
cassandraHost.setUseThriftFramedTransport(useThriftFramedTransport);
cassandraHost.setMaxFrameSize(maxFrameSize);
cassandraHost.setUseSocketKeepalive(useSocketKeepalive);
+ cassandraHost.setMaxConnectTimeMillis(maxConnectTimeMillis);
// this is special as it can be passed in as a system property
if (cassandraThriftSocketTimeout > 0) {
@@ -351,4 +352,25 @@ public void setClientFactoryClass(String cls) {
public Class<? extends HClientFactory> getClientFactoryClass() {
return clientFactoryClass;
}
+
+ /**
+ * The maximum time in milliseconds that we'll allow a connection to stay open to a host. A negative
+ * value indicates indefinitely (and is the default).
+ *
+ * @return the number of milliseconds
+ */
+ public long getMaxConnectTimeMillis() {
+ return maxConnectTimeMillis;
+ }
+
+ /**
+ * Set the maximum time in milliseconds that we'll allow a connection to stay open to a host. A negative
+ * value indicates indefinitely. This setting is useful if you you need to work around a firewall that
+ * forcefully closes connections after a fixed amount of time regardless of activity.
+ *
+ * @param maxConnectTimeMillis the maximum time to use a connection
+ */
+ public void setMaxConnectTimeMillis(long maxConnectTimeMillis) {
+ this.maxConnectTimeMillis = maxConnectTimeMillis;
+ }
}
Something went wrong with that request. Please try again.