Skip to content

Commit

Permalink
Merge pull request #609 from hexagonkt/develop
Browse files Browse the repository at this point in the history
Improve Netty resource usage
  • Loading branch information
jaguililla authored Mar 19, 2023
2 parents 2cdafde + 6386b3f commit 1a9c89a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 57 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.warning.mode=all
org.gradle.console=plain

# Gradle
version=2.6.4
version=2.6.5
group=com.hexagonkt
description=The atoms of your platform

Expand Down
37 changes: 5 additions & 32 deletions gradle/application.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ tasks.register("jpackage") {
final String modules = findProperty("modules") ?: "java.logging"
final String jarAllName = "$name-all-${version}.jar"
final java.nio.file.Path jarAll = buildDir.resolve("libs/$jarAllName")
final java.nio.file.Path jpackageJar = buildDir.resolve("jpackage/$jarAllName")

List<String> command = [
"${System.getenv("JAVA_HOME")}/bin/jpackage",
Expand All @@ -86,7 +87,8 @@ tasks.register("jpackage") {

tmp.mkdirs()
buildDir.resolve(name).toFile().deleteDir()
java.nio.file.Files.copy(jarAll, buildDir.resolve("jpackage/$jarAllName"))
jpackageJar.toFile().delete()
java.nio.file.Files.copy(jarAll, jpackageJar)

exec {
workingDir(buildDir.toFile())
Expand All @@ -102,7 +104,7 @@ tasks.register("tarJpackage", Tar) {

from(buildDir.toPath().resolve(project.name).toFile())
compression = Compression.BZIP2
archiveFileName.set("${project.name}-${project.version}.tbz2")
archiveFileName.set("${project.name}-${project.version}-jpackage.tbz2")
destinationDirectory.set(buildDir.toPath().resolve("distributions").toFile())
}

Expand Down Expand Up @@ -154,35 +156,6 @@ tasks.register("tarJlink", Tar) {

from(buildDir.toPath().resolve(project.name).toFile())
compression = Compression.BZIP2
archiveFileName.set("${project.name}-${project.version}.tbz2")
archiveFileName.set("${project.name}-${project.version}-jlink.tbz2")
destinationDirectory.set(buildDir.toPath().resolve("distributions").toFile())
}

tasks.register("watch") {
group = "application"
description = "Run application in other thread. Allows the possibility to watch source changes."
dependsOn("classes")

doLast {
final JavaExec runTask = run

// NOTE: these two statements are *REQUIRED* to load classpath and main class
runTask.classpath.each { it.toString() }
runTask.mainClass.toString()

ByteArrayOutputStream out = new ByteArrayOutputStream()
exec {
commandLine("jps", "-l")
standardOutput = out
}
out.toString().readLines()
.findAll { it.endsWith(runTask.mainClass.get()) }
.collect { it.split(" ")[0] }
.each { pid -> exec { commandLine("kill", pid) } }

Thread.startDaemon {
runTask.setIgnoreExitValue(true)
runTask.actions.each { action -> action.execute(runTask) }
}
}
}
9 changes: 8 additions & 1 deletion gradle/dokka.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ setUpDokka(dokkaHtmlPartial)
setUpDokka(dokkaJavadoc)

private void setUpDokka(final Object dokkaTask) {
dokkaTask.dependsOn("compileJava", "compileKotlin", "processResources", "processTestResources")
dokkaTask.dependsOn(
"compileJava",
"compileKotlin",
"compileTestKotlin",
"processResources",
"processTestResources"
)

dokkaTask.moduleName.set(project.name)
dokkaTask.offlineMode.set(true)
dokkaTask.dokkaSourceSets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import kotlin.Int.Companion.MAX_VALUE
*/
open class NettyServerAdapter(
private val bossGroupThreads: Int = 1,
private val workerGroupThreads: Int = 0,
private val executorThreads: Int = Jvm.cpuCount * 2,
private val soBacklog: Int = 4 * 1_024,
private val soReuseAddr: Boolean = true,
Expand All @@ -58,7 +57,6 @@ open class NettyServerAdapter(

constructor() : this(
bossGroupThreads = 1,
workerGroupThreads = 0,
executorThreads = Jvm.cpuCount * 2,
soBacklog = 4 * 1_024,
soReuseAddr = true,
Expand All @@ -76,7 +74,6 @@ open class NettyServerAdapter(

override fun startUp(server: HttpServer) {
val bossGroup = groupSupplier(bossGroupThreads)
val workerGroup = groupSupplier(workerGroupThreads)
val executorGroup =
if (executorThreads > 0) DefaultEventExecutorGroup(executorThreads)
else null
Expand All @@ -89,7 +86,7 @@ open class NettyServerAdapter(
.byMethod()
.mapKeys { HttpMethod.valueOf(it.key.toString()) }

val nettyServer = serverBootstrapSupplier(bossGroup, workerGroup)
val nettyServer = serverBootstrapSupplier(bossGroup)
.childHandler(createInitializer(sslSettings, handlers, executorGroup, settings))

val address = settings.bindAddress
Expand All @@ -98,23 +95,18 @@ open class NettyServerAdapter(

nettyChannel = future.channel()
bossEventLoop = bossGroup
workerEventLoop = workerGroup
}
catch (e: Exception) {
bossGroup.shutdownGracefully()
workerGroup.shutdownGracefully()
executorGroup?.shutdownGracefully()
}
}

open fun groupSupplier(it: Int): MultithreadEventLoopGroup =
NioEventLoopGroup(it)

open fun serverBootstrapSupplier(
bossGroup: MultithreadEventLoopGroup,
workerGroup: MultithreadEventLoopGroup,
): ServerBootstrap =
ServerBootstrap().group(bossGroup, workerGroup)
open fun serverBootstrapSupplier(bossGroup: MultithreadEventLoopGroup): ServerBootstrap =
ServerBootstrap().group(bossGroup)
.channel(NioServerSocketChannel::class.java)
.option(ChannelOption.SO_BACKLOG, soBacklog)
.option(ChannelOption.SO_REUSEADDR, soReuseAddr)
Expand Down Expand Up @@ -186,7 +178,6 @@ open class NettyServerAdapter(
override fun options(): Map<String, *> =
fieldsMapOf(
NettyServerAdapter::bossGroupThreads to bossGroupThreads,
NettyServerAdapter::workerGroupThreads to workerGroupThreads,
NettyServerAdapter::executorThreads to executorThreads,
NettyServerAdapter::soBacklog to soBacklog,
NettyServerAdapter::soKeepAlive to soKeepAlive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,25 @@ import io.netty.channel.epoll.EpollServerSocketChannel
*/
class NettyEpollServerAdapter(
bossGroupThreads: Int = 1,
workerGroupThreads: Int = 0,
executorThreads: Int = Jvm.cpuCount * 2,
private val soBacklog: Int = 4 * 1_024,
private val soReuseAddr: Boolean = true,
private val soKeepAlive: Boolean = true,
) : NettyServerAdapter(
bossGroupThreads,
workerGroupThreads,
executorThreads,
soBacklog,
soReuseAddr,
soKeepAlive,
) {

constructor() : this(1, 0, Jvm.cpuCount * 2, 4 * 1_024, true, true)
constructor() : this(1, Jvm.cpuCount * 2, 4 * 1_024, true, true)

override fun groupSupplier(it: Int): MultithreadEventLoopGroup =
EpollEventLoopGroup(it)

override fun serverBootstrapSupplier(
bossGroup: MultithreadEventLoopGroup,
workerGroup: MultithreadEventLoopGroup,
): ServerBootstrap =
ServerBootstrap().group(bossGroup, workerGroup)
override fun serverBootstrapSupplier(bossGroup: MultithreadEventLoopGroup): ServerBootstrap =
ServerBootstrap().group(bossGroup)
.channel(EpollServerSocketChannel::class.java)
.option(EpollChannelOption.SO_REUSEPORT, true)
.option(ChannelOption.SO_BACKLOG, soBacklog)
Expand Down
3 changes: 0 additions & 3 deletions site/pages/gradle.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ Gradle's script for a service or application. It adds these extra tasks:

* buildInfo: add configuration file (`META-INF/build.properties`) with build variables to the
package. It is executed automatically before compiling classes.
* watch: run the application in another thread. Allows the possibility to watch source changes. To
run the application and watch for changes you need to execute this task with the `--continuous`
(`-t`) Gradle flag. Ie: `gw -t watch`.
* jarAll: creates a single JAR with all dependencies, and the application main class set up. This
task is an alternative to the Gradle `installDist` task.
* jlink: create an application distribution based on a jlink generated JRE.
Expand Down

0 comments on commit 1a9c89a

Please sign in to comment.