chore: Add PekkoManagedBlocker to reduce memory.
This commit is contained in:
parent
ae210b17d2
commit
4d8aa26404
1 changed files with 14 additions and 10 deletions
|
|
@ -173,19 +173,23 @@ object MonitorableThreadFactory {
|
||||||
val doNothing: Thread.UncaughtExceptionHandler =
|
val doNothing: Thread.UncaughtExceptionHandler =
|
||||||
new Thread.UncaughtExceptionHandler() { def uncaughtException(thread: Thread, cause: Throwable) = () }
|
new Thread.UncaughtExceptionHandler() { def uncaughtException(thread: Thread, cause: Throwable) = () }
|
||||||
|
|
||||||
|
private class PekkoManagedBlocker[T](thunk: => T)
|
||||||
|
extends AtomicReference[Option[T]](None) with ForkJoinPool.ManagedBlocker {
|
||||||
|
final override def block(): Boolean = {
|
||||||
|
set(Some(thunk))
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
final override def isReleasable: Boolean = get().isDefined // Exception intended if None
|
||||||
|
}
|
||||||
|
|
||||||
private[pekko] class PekkoForkJoinWorkerThread(_pool: ForkJoinPool)
|
private[pekko] class PekkoForkJoinWorkerThread(_pool: ForkJoinPool)
|
||||||
extends ForkJoinWorkerThread(_pool)
|
extends ForkJoinWorkerThread(_pool)
|
||||||
with BlockContext {
|
with BlockContext {
|
||||||
override def blockOn[T](thunk: => T)(implicit permission: CanAwait): T = {
|
final override def blockOn[T](thunk: => T)(implicit permission: CanAwait): T = {
|
||||||
val result = new AtomicReference[Option[T]](None)
|
val blocker = new PekkoManagedBlocker(thunk)
|
||||||
ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker {
|
ForkJoinPool.managedBlock(blocker)
|
||||||
def block(): Boolean = {
|
blocker.get.get // Exception intended if None
|
||||||
result.set(Some(thunk))
|
|
||||||
true
|
|
||||||
}
|
|
||||||
def isReleasable = result.get.isDefined
|
|
||||||
})
|
|
||||||
result.get.get // Exception intended if None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue