Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
He-Pin committed Jan 15, 2025
1 parent 10eb99d commit 38c27d3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
3 changes: 2 additions & 1 deletion build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ object benchmark extends Cross[BenchmarkModule](build.scalaVersions) with RunMod
"-d", duration,
url
).call(stderr = os.Pipe)
serverApp.destroy()
serverApp.destroyForcibly()
Thread.sleep(1000)

println(s"""\n$example result with ${if (vt) "(virtual threads)" else "(platform threads)"}:""")
println(results.out.text())
Expand Down
22 changes: 10 additions & 12 deletions cask/src/cask/main/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cask.main
import cask.endpoints.{WebsocketResult, WsHandler}
import cask.model._
import cask.internal.{DispatchTrie, ThreadBlockingHandler, Util}
import cask.main
import cask.router.{Decorator, EndpointMetadata, EntryPoint, RawDecorator, Result}
import cask.util.Logger
import io.undertow.Undertow
Expand All @@ -12,7 +11,6 @@ import io.undertow.server.handlers.BlockingHandler
import io.undertow.util.HttpString

import java.util.concurrent.Executor
import scala.concurrent.ExecutionContext

/**
* A combination of [[cask.Main]] and [[cask.Routes]], ideal for small
Expand Down Expand Up @@ -49,30 +47,30 @@ abstract class Main{

/**
* The handler that will be used to handle incoming requests. By default,
* when a `null` handler is provided, a default handler will be used,
* when a handler is provided, a default handler will be used,
* otherwise the provided executor will be used to handle requests.
*
* When `cask.virtual-thread.enabled` is set to `true` and running with a JDK
* where virtual threads are supported, then a virtual thread executor will be used.
* */
protected def handlerExecutor(): Executor = {
if (enableVirtualThread) {
Util.createDefaultCaskVirtualThreadExecutor.orNull
} else null
protected def handlerExecutor(): Option[Executor] = {
if (virtualThreadEnabled) {
Util.createDefaultCaskVirtualThreadExecutor
} else None
}

protected def enableVirtualThread: Boolean = {
protected def virtualThreadEnabled: Boolean = {
val enableVirtualThread = System.getProperty(Main.VIRTUAL_THREAD_ENABLED)
enableVirtualThread != null && enableVirtualThread.toBoolean
}

def defaultHandler: HttpHandler = {
val executor = handlerExecutor()
val mainHandler = new Main.DefaultHandler(
dispatchTrie, mainDecorators, debugMode, handleNotFound, handleMethodNotAllowed, handleEndpointError)
if (executor ne null) {
new ThreadBlockingHandler(executor, mainHandler)
} else new BlockingHandler(mainHandler)
handlerExecutor() match {
case None => new BlockingHandler(mainHandler)
case Some(executor) => new ThreadBlockingHandler(executor, mainHandler)
}
}

def handleNotFound(req: Request): Response.Raw = Main.defaultHandleNotFound(req)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ object MinimalApplicationWithLoom extends cask.MainRoutes {
// ForkJoinPool in VirtualThread. If you want to use a separate ForkJoinPool, you can create
// a new ForkJoinPool instance and pass it to `createVirtualThreadExecutor` method.

override protected def handlerExecutor(): Executor = {
if (enableVirtualThread) {
Util.createVirtualThreadExecutor(executor).get
override protected def handlerExecutor(): Option[Executor] = {
if (virtualThreadEnabled) {
Util.createVirtualThreadExecutor(executor)
} else {
executor
Some(executor)
}
}

Expand Down
11 changes: 5 additions & 6 deletions example/staticFilesWithLoom/app/src/StaticFilesWithLoom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import java.util.concurrent.{Executor, Executors}
object StaticFilesWithLoom extends cask.MainRoutes{
private val executor = Executors.newFixedThreadPool(4)

override protected def handlerExecutor(): Executor = {
println("use virtual thread : " + enableVirtualThread)
if (enableVirtualThread) {
Util.createVirtualThreadExecutor(executor).get
override protected def handlerExecutor(): Option[Executor] = {
if (virtualThreadEnabled) {
Util.createVirtualThreadExecutor(executor)
} else {
executor
Some(executor)
}
}

@cask.get("/")
def index() = {
"Hello!"
Expand Down
8 changes: 4 additions & 4 deletions example/todoDbWithLoom/app/src/TodoMvcDbWithLoom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ object TodoMvcDbWithLoom extends cask.MainRoutes {
)

private val executor = Executors.newFixedThreadPool(4)
override protected def handlerExecutor(): Executor = {
if (enableVirtualThread) {
Util.createVirtualThreadExecutor(executor).get
override protected def handlerExecutor(): Option[Executor] = {
if (virtualThreadEnabled) {
Util.createVirtualThreadExecutor(executor)
} else {
executor
Some(executor)
}
}

Expand Down

0 comments on commit 38c27d3

Please sign in to comment.