Skip to content

Commit b617559

Browse files
authored
Merge pull request #94 from ebean-orm/feature/lambda-check-min
Change the Lambda check time gap to use maxInactiveMillis + 1 minute
2 parents 6179c0d + fda9ef1 commit b617559

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
final class ConnectionPool implements DataSourcePool {
2727

2828
private static final String APPLICATION_NAME = "ApplicationName";
29-
private static final long LAMBDA_MILLIS = 60_000;
3029
private final ReentrantLock heartbeatLock = new ReentrantLock(false);
3130
private final ReentrantLock notifyLock = new ReentrantLock(false);
3231
/**
@@ -62,6 +61,7 @@ final class ConnectionPool implements DataSourcePool {
6261
private final Properties clientInfo;
6362
private final String applicationName;
6463
private final DataSource source;
64+
private final int lambdaTrimMillis;
6565
private long nextTrimTime;
6666
private long nextLambdaTrimTime;
6767

@@ -103,6 +103,7 @@ final class ConnectionPool implements DataSourcePool {
103103
this.initSql = params.getInitSql();
104104
this.transactionIsolation = params.getIsolationLevel();
105105
this.maxInactiveMillis = 1000 * params.getMaxInactiveTimeSecs();
106+
this.lambdaTrimMillis = Math.max(maxInactiveMillis + 60_000, 300_000);
106107
this.maxAgeMillis = 60000L * params.getMaxAgeMinutes();
107108
this.leakTimeMinutes = params.getLeakTimeMinutes();
108109
this.captureStackTrace = params.isCaptureStackTrace();
@@ -125,7 +126,8 @@ final class ConnectionPool implements DataSourcePool {
125126
if (!params.isOffline()) {
126127
init();
127128
}
128-
this.nextLambdaTrimTime = System.currentTimeMillis() + trimPoolFreqMillis + LAMBDA_MILLIS;
129+
this.nextTrimTime = System.currentTimeMillis() + trimPoolFreqMillis;
130+
this.nextLambdaTrimTime = nextTrimTime + lambdaTrimMillis;
129131
}
130132

131133
private void init() {
@@ -340,9 +342,11 @@ private void notifyUp() {
340342

341343
void checkLambdaIdle() {
342344
if (System.currentTimeMillis() > nextLambdaTrimTime) {
345+
// means that the usual background trimIdleConnections() has not been run recently
346+
// which we interpret as a lambda being invoked after coming back from suspension
343347
var timeGapSeconds = (System.currentTimeMillis() - nextTrimTime) / 1000;
344348
var status = status(false);
345-
Log.info("DataSource [{0}] detected lambda restore, trimming idle connections - timeGap {1}s {2}", name, timeGapSeconds, status);
349+
Log.info("DataSource [{0}] lambda trim idle connections - timeGap {1}s {2}", name, timeGapSeconds, status);
346350
trimIdleConnections();
347351
}
348352
}
@@ -355,7 +359,7 @@ private void trimIdleConnections() {
355359
try {
356360
queue.trim(maxInactiveMillis, maxAgeMillis);
357361
nextTrimTime = System.currentTimeMillis() + trimPoolFreqMillis;
358-
nextLambdaTrimTime = nextTrimTime + LAMBDA_MILLIS;
362+
nextLambdaTrimTime = nextTrimTime + lambdaTrimMillis;
359363
} catch (Exception e) {
360364
Log.error("Error trying to trim idle connections - message:" + e.getMessage(), e);
361365
}

0 commit comments

Comments
 (0)