Skip to content

Commit

Permalink
Merge pull request #18925 from stuartwdouglas/17304
Browse files Browse the repository at this point in the history
Add a custom connection cache to Agroal
  • Loading branch information
stuartwdouglas authored Jul 23, 2021
2 parents 84fc1d0 + 2e040b5 commit 8df4cbb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public AgroalDataSource doCreateDataSource(String dataSourceName) {
if (dataSourceSupport.disableSslSupport) {
agroalConnectionConfigurer.disableSslSupport(resolvedDbKind, dataSourceConfiguration);
}
//we use a custom cache for two reasons:
//fast thread local cache should be faster
//and it prevents a thread local leak
dataSourceConfiguration.connectionPoolConfiguration().connectionCache(new QuarkusConnectionCache());

agroalConnectionConfigurer.setExceptionSorter(resolvedDbKind, dataSourceConfiguration);

Expand Down Expand Up @@ -362,4 +366,5 @@ public void stop() {
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.agroal.runtime;

import org.jboss.threads.JBossThread;

import io.agroal.api.cache.Acquirable;
import io.agroal.api.cache.ConnectionCache;
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.concurrent.FastThreadLocalThread;

class QuarkusConnectionCache implements ConnectionCache {

volatile FastThreadLocal<Acquirable> connectionCache = new FastThreadLocal<>();

@Override
public Acquirable get() {
Thread thread = Thread.currentThread();
if (thread instanceof FastThreadLocalThread || thread instanceof JBossThread) {
//we only want to cache on threads that we control the lifecycle
//which are the vert.x and potentially jboss threads
//JBossThread still works with FastThreadLocal, it is just slower, and for most apps
//this will not be used anyway, as we use VertThread pretty much everywhere if
//Vert.x is present
Acquirable acquirable = connectionCache.get();
return acquirable != null && acquirable.acquire() ? acquirable : null;
}
return null;
}

@Override
public void put(Acquirable acquirable) {
Thread thread = Thread.currentThread();
if (thread instanceof FastThreadLocalThread || thread instanceof JBossThread) {
connectionCache.set(acquirable);
}
}

@Override
public void reset() {
connectionCache = new FastThreadLocal<>();
}
}

0 comments on commit 8df4cbb

Please sign in to comment.