Skip to content
This repository
Browse code

Merge pull request #582 from alcanzar/issue-581

issue-581 -- Support a maximum connect time on a connection
  • Loading branch information...
commit 6285932a67c4731aa6218713daf14767fed8e4f0 2 parents 14f1ea5 + 3220474
Nate McCall authored January 16, 2013
8  core/src/main/java/me/prettyprint/cassandra/connection/ConcurrentHClientPool.java
@@ -71,7 +71,13 @@ public HClient borrowClient() throws HectorException {
71 71
     int currentActiveClients = activeClientsCount.incrementAndGet();
72 72
 
73 73
     try {
74  
-
  74
+      if (cassandraClient.getCassandraHost().getMaxConnectTimeMillis() > 0
  75
+          && System.currentTimeMillis() - cassandraClient.getCreatedTime() > cassandraClient.getCassandraHost().getMaxConnectTimeMillis()) {
  76
+        log.info("Closing connection to {} due to idle time of {} ms", cassandraClient.getCassandraHost().getHost(),
  77
+            System.currentTimeMillis() - cassandraClient.getCreatedTime());
  78
+        cassandraClient.close();
  79
+        cassandraClient = null;
  80
+      }
75 81
       if ( cassandraClient == null ) {
76 82
 
77 83
         if (currentActiveClients <= cassandraHost.getMaxActive()) {
6  core/src/main/java/me/prettyprint/cassandra/connection/client/HClient.java
@@ -17,6 +17,12 @@
17 17
  * 
18 18
  */
19 19
 public interface HClient {
  20
+  /**
  21
+   * Returns the time that this HClient was created.
  22
+   * 
  23
+   * @return the time this client was created
  24
+   */
  25
+  long getCreatedTime();
20 26
 
21 27
   /**
22 28
    * Returns a new Cassandra.Client on each invocation using the underlying
8  core/src/main/java/me/prettyprint/cassandra/connection/client/HThriftClient.java
@@ -36,6 +36,7 @@
36 36
  * <p>
37 37
 */
38 38
 public class HThriftClient implements HClient {
  39
+  private long createdTime = System.currentTimeMillis();
39 40
 
40 41
   private static Logger log = LoggerFactory.getLogger(HThriftClient.class);
41 42
 
@@ -287,4 +288,11 @@ public void setAuthenticated(Map<String, String> credentials) {
287 288
     clearAuthentication();
288 289
     this.credentials.putAll(credentials);
289 290
   }
  291
+
  292
+  /**
  293
+   * {@inheritDoc}
  294
+   */
  295
+  public long getCreatedTime() {
  296
+    return createdTime;
  297
+  }
290 298
 }
14  core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java
@@ -40,6 +40,11 @@
40 40
   public static final long DEFAULT_MAX_WAITTIME_WHEN_EXHAUSTED = -1;
41 41
 
42 42
   public static final boolean DEFAULT_LIFO = true;
  43
+  /**
  44
+   * The default number of milliseconds we'll allow a connection to stay open.  Default value
  45
+   * is negative which means indefinitely.
  46
+   */
  47
+  public static final long DEFAULT_MAX_CONNECT_TIME = -1;
43 48
 
44 49
   private final String host, ip, url;
45 50
   private final int port;
@@ -54,6 +59,7 @@
54 59
   private boolean useThriftFramedTransport = DEFAULT_USE_FRAMED_THRIFT_TRANSPORT;
55 60
   private int maxFrameSize = DEFAULT_MAX_FRAME_SIZE;
56 61
   private boolean useSocketKeepalive;
  62
+  private long maxConnectTimeMillis = DEFAULT_MAX_CONNECT_TIME;
57 63
   //TODO(ran): private FailoverPolicy failoverPolicy = DEFAULT_FAILOVER_POLICY;
58 64
 
59 65
   public CassandraHost(String url) {
@@ -204,5 +210,13 @@ public void setUseSocketKeepalive(boolean useSocketKeepalive) {
204 210
     this.useSocketKeepalive = useSocketKeepalive;
205 211
   }
206 212
 
  213
+  public long getMaxConnectTimeMillis() {
  214
+    return this.maxConnectTimeMillis ;
  215
+  }
  216
+
  217
+  public void setMaxConnectTimeMillis(long maxConnectTimeMillis) {
  218
+    this.maxConnectTimeMillis = maxConnectTimeMillis;
  219
+  }
  220
+
207 221
   
208 222
 }
24  core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java
@@ -51,7 +51,7 @@
51 51
   private boolean useSocketKeepalive = false;
52 52
   private HOpTimer opTimer = new NullOpTimer();
53 53
   private Class<? extends HClientFactory> clientFactoryClass = HThriftClientFactoryImpl.class;
54  
-
  54
+  private long maxConnectTimeMillis = CassandraHost.DEFAULT_MAX_CONNECT_TIME;
55 55
 
56 56
   public CassandraHostConfigurator() {
57 57
     this.hosts = null;
@@ -88,6 +88,7 @@ public void applyConfig(CassandraHost cassandraHost) {
88 88
     cassandraHost.setUseThriftFramedTransport(useThriftFramedTransport);
89 89
     cassandraHost.setMaxFrameSize(maxFrameSize);
90 90
     cassandraHost.setUseSocketKeepalive(useSocketKeepalive);
  91
+    cassandraHost.setMaxConnectTimeMillis(maxConnectTimeMillis);
91 92
 
92 93
     // this is special as it can be passed in as a system property
93 94
     if (cassandraThriftSocketTimeout > 0) {
@@ -361,4 +362,25 @@ public void setClientFactoryClass(String cls) {
361 362
   public Class<? extends HClientFactory> getClientFactoryClass() {
362 363
     return clientFactoryClass;
363 364
   }
  365
+
  366
+  /**
  367
+   * The maximum time in milliseconds that we'll allow a connection to stay open to a host.  A negative
  368
+   * value indicates indefinitely (and is the default).
  369
+   * 
  370
+   * @return the number of milliseconds
  371
+   */
  372
+  public long getMaxConnectTimeMillis() {
  373
+    return maxConnectTimeMillis;
  374
+  }
  375
+
  376
+  /**
  377
+   * Set the maximum time in milliseconds that we'll allow a connection to stay open to a host. A negative
  378
+   * value indicates indefinitely.  This setting is useful if you you need to work around a firewall that 
  379
+   * forcefully closes connections after a fixed amount of time regardless of activity.
  380
+   * 
  381
+   * @param maxConnectTimeMillis the maximum time to use a connection
  382
+   */
  383
+  public void setMaxConnectTimeMillis(long maxConnectTimeMillis) {
  384
+    this.maxConnectTimeMillis = maxConnectTimeMillis;
  385
+  }
364 386
 }

0 notes on commit 6285932

Please sign in to comment.
Something went wrong with that request. Please try again.