-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18925 from stuartwdouglas/17304
Add a custom connection cache to Agroal
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...nsions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/QuarkusConnectionCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} | ||
} |