-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Move helper methods to ThreadBlockingHandler
- Loading branch information
Showing
6 changed files
with
78 additions
and
81 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,55 @@ | ||
package cask.internal | ||
|
||
import io.undertow.server.{HttpHandler, HttpServerExchange} | ||
|
||
import java.lang.invoke.{MethodHandles, MethodType} | ||
import java.util.concurrent.{Executor, ExecutorService, ThreadFactory} | ||
import scala.util.control.NonFatal | ||
|
||
final class ThreadBlockingHandler(executor: Executor, | ||
handler: HttpHandler) | ||
extends HttpHandler { | ||
require(executor != null, "executor should not be null") | ||
|
||
override def handleRequest(exchange: HttpServerExchange): Unit = { | ||
exchange.startBlocking() | ||
exchange.dispatch(executor, handler) | ||
} | ||
} | ||
|
||
object ThreadBlockingHandler { | ||
private val lookup = MethodHandles.lookup | ||
|
||
def createNewThreadPerTaskExecutor(threadFactory: ThreadFactory): ExecutorService = { | ||
try { | ||
val executorsClazz = ClassLoader.getSystemClassLoader.loadClass("java.util.concurrent.Executors") | ||
val newThreadPerTaskExecutorMethod = lookup.findStatic( | ||
executorsClazz, | ||
"newThreadPerTaskExecutor", | ||
MethodType.methodType(classOf[ExecutorService], classOf[ThreadFactory])) | ||
newThreadPerTaskExecutorMethod.invoke(threadFactory) | ||
.asInstanceOf[ExecutorService] | ||
} catch { | ||
case NonFatal(e) => | ||
throw new UnsupportedOperationException("Failed to create virtual thread executor", e) | ||
} | ||
} | ||
|
||
/** | ||
* Create a virtual thread factory, returns null when failed. | ||
*/ | ||
def createVirtualThreadFactory(prefix: String): ThreadFactory = | ||
try { | ||
val builderClass = ClassLoader.getSystemClassLoader.loadClass("java.lang.Thread$Builder") | ||
val ofVirtualClass = ClassLoader.getSystemClassLoader.loadClass("java.lang.Thread$Builder$OfVirtual") | ||
val ofVirtualMethod = lookup.findStatic(classOf[Thread], "ofVirtual", MethodType.methodType(ofVirtualClass)) | ||
var builder = ofVirtualMethod.invoke() | ||
val nameMethod = lookup.findVirtual(ofVirtualClass, "name", | ||
MethodType.methodType(ofVirtualClass, classOf[String], classOf[Long])) | ||
val factoryMethod = lookup.findVirtual(builderClass, "factory", MethodType.methodType(classOf[ThreadFactory])) | ||
builder = nameMethod.invoke(builder, prefix + "-virtual-thread-", 0L) | ||
factoryMethod.invoke(builder).asInstanceOf[ThreadFactory] | ||
} catch { | ||
case _: Throwable => null | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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