Skip to content

Commit

Permalink
Merge pull request #607 from hexagonkt/develop
Browse files Browse the repository at this point in the history
Update handlers
  • Loading branch information
jaguililla authored Mar 9, 2023
2 parents cbb7e57 + 2aabe36 commit 6bcfacb
Show file tree
Hide file tree
Showing 32 changed files with 250 additions and 243 deletions.
9 changes: 7 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.jk1.license.render.CsvReportRenderer
import com.github.jk1.license.render.InventoryHtmlReportRenderer
import com.github.jk1.license.render.InventoryMarkdownReportRenderer
import com.github.jk1.license.render.ReportRenderer
import io.gitlab.arturbosch.detekt.Detekt

/*
* Main build script, responsible for:
Expand All @@ -23,11 +24,11 @@ plugins {
id("idea")
id("eclipse")
id("project-report")
id("org.jetbrains.dokka") version("1.7.20")
id("org.jetbrains.dokka") version("1.8.10")
id("com.github.jk1.dependency-license-report") version("2.1")
id("org.jetbrains.kotlinx.binary-compatibility-validator") version("0.13.0")
id("io.gitlab.arturbosch.detekt") version("1.22.0") apply(false)
id("me.champeau.jmh") version("0.6.8") apply(false)
id("me.champeau.jmh") version("0.7.0") apply(false)
}

apply(from = "gradle/certificates.gradle")
Expand Down Expand Up @@ -95,3 +96,7 @@ gradle.taskGraph.whenReady(closureOf<TaskExecutionGraph> {
apiValidation {
validationDisabled = true
}

subprojects {
apply(from = "$rootDir/gradle/detekt.gradle")
}
8 changes: 4 additions & 4 deletions 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.2
version=2.6.3
group=com.hexagonkt
description=The atoms of your platform

Expand Down Expand Up @@ -34,12 +34,12 @@ iconsDirectory=content

# VERSIONS
kotlinVersion=1.8.10
dokkaVersion=1.7.20
dokkaVersion=1.8.10
mockkVersion=1.13.4
junitVersion=5.9.2
gatlingVersion=3.9.1
gatlingVersion=3.9.2
jmhVersion=1.36
mkdocsMaterialVersion=9.0.15
mkdocsMaterialVersion=9.1.1
mermaidDokkaVersion=0.4.4
nativeToolsVersion=0.9.20

Expand Down
1 change: 1 addition & 0 deletions gradle/dokka.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ setUpDokka(dokkaHtmlPartial)
setUpDokka(dokkaJavadoc)

private void setUpDokka(final Object dokkaTask) {
dokkaTask.dependsOn("compileJava", "compileKotlin", "processResources")
dokkaTask.moduleName.set(project.name)
dokkaTask.offlineMode.set(true)
dokkaTask.dokkaSourceSets {
Expand Down
25 changes: 25 additions & 0 deletions gradle/jmh.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Check usage information at: http://hexagonkt.com/gradle/#jmh
*/

apply(plugin: "me.champeau.jmh")

tasks.compileJmhKotlin.kotlinOptions.jvmTarget = tasks.compileKotlin.kotlinOptions.jvmTarget
tasks.compileJmhKotlin.kotlinOptions.apiVersion = tasks.compileKotlin.kotlinOptions.apiVersion

jmh {
final String jhmVersion = findProperty("jhmVersion") ?: "1.36"

jmhVersion.set(jhmVersion)
benchmarkMode.set(["thrpt"])

iterations.set(10)
batchSize.set(1)
fork.set(1)
operationsPerInvocation.set(5)
timeOnIteration.set("1s")

warmup.set("1s")
warmupBatchSize.set(5)
warmupIterations.set(1)
}
4 changes: 1 addition & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
# TODO Use Gradle 8 when Dokka support it
#distributionUrl=https://services.gradle.org/distributions/gradle-8.0.1-all.zip
distributionUrl=https://services.gradle.org/distributions/gradle-7.6.1-all.zip
distributionUrl=https://services.gradle.org/distributions/gradle-8.0.2-all.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 2 additions & 0 deletions handlers/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

plugins {
id("java-library")
id("me.champeau.jmh")
}

apply(from = "../gradle/kotlin.gradle")
apply(from = "../gradle/publish.gradle")
apply(from = "../gradle/dokka.gradle")
apply(from = "../gradle/jmh.gradle")

description = "Handlers to be applied on events processing."
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hexagonkt.http.test
package com.hexagonkt.handlers

import com.hexagonkt.handlers.*
import org.openjdk.jmh.annotations.Benchmark
Expand All @@ -9,6 +9,33 @@ import org.openjdk.jmh.infra.Blackhole
@State(Scope.Benchmark)
open class JmhBenchmark {

data class EventContext<T : Any>(
override val event: T,
override val predicate: (Context<T>) -> Boolean,
override val nextHandlers: List<Handler<T>> = emptyList(),
override val nextHandler: Int = 0,
override val exception: Exception? = null,
override val attributes: Map<*, *> = emptyMap<Any, Any>(),
) : Context<T> {

override fun with(
event: T,
predicate: (Context<T>) -> Boolean,
nextHandlers: List<Handler<T>>,
nextHandler: Int,
exception: Exception?,
attributes: Map<*, *>,
): EventContext<T> =
copy(
event = event,
predicate = predicate,
nextHandlers = nextHandlers,
nextHandler = nextHandler,
exception = exception,
attributes = attributes,
)
}

private fun <T : Any> Handler<T>.process(event: T): T =
process(EventContext(event, predicate)).event

Expand Down
14 changes: 7 additions & 7 deletions handlers/src/main/kotlin/com/hexagonkt/handlers/AfterHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ package com.hexagonkt.handlers
* Their filter is evaluated after the `next` call, not before.
*/
data class AfterHandler<T : Any>(
val afterPredicate: Predicate<T> = { true },
val afterCallback: Callback<T>,
val afterPredicate: (Context<T>) -> Boolean = { true },
override val callback: (Context<T>) -> Context<T>,
) : Handler<T> {

override val predicate: Predicate<T> = { true }
override val predicate: (Context<T>) -> Boolean = { true }

override val callback: Callback<T> = {
val next = it.next().with(predicate = afterPredicate)
try {
if (afterPredicate.invoke(next)) afterCallback(next)
override fun process(context: Context<T>): Context<T> {
val next = context.next().with(predicate = afterPredicate)
return try {
if (afterPredicate.invoke(next)) callback(next)
else next
}
catch (e: Exception) {
Expand Down
17 changes: 10 additions & 7 deletions handlers/src/main/kotlin/com/hexagonkt/handlers/ChainHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ package com.hexagonkt.handlers

data class ChainHandler<T : Any>(
val handlers: List<Handler<T>>,
override val predicate: Predicate<T> = { true },
override val predicate: (Context<T>) -> Boolean = { true },
) : Handler<T> {

override val callback: (Context<T>) -> Context<T> = { it }

constructor(
filter: Predicate<T>,
filter: (Context<T>) -> Boolean,
vararg handlers: Handler<T>,
) : this(handlers.toList(), filter)

constructor(vararg handlers: Handler<T>) : this(handlers.toList(), { true })

override val callback: Callback<T> = {
val nestedContext = it.with(event = it.event, nextHandlers = handlers, nextHandler = 0)
override fun process(context: Context<T>): Context<T> {
val event = context.event
val nestedContext = context.with(event = event, nextHandlers = handlers, nextHandler = 0)
val nestedResult = nestedContext.next()
val followUpContext = nestedResult.with(
predicate = predicate,
nextHandlers = it.nextHandlers,
nextHandler = it.nextHandler
nextHandlers = context.nextHandlers,
nextHandler = context.nextHandler
)
followUpContext.next()
return followUpContext.next()
}
}
15 changes: 12 additions & 3 deletions handlers/src/main/kotlin/com/hexagonkt/handlers/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@ package com.hexagonkt.handlers
*/
interface Context<T : Any> {
val event: T
val predicate: Predicate<T>
val predicate: (Context<T>) -> Boolean
val nextHandlers: List<Handler<T>>
val nextHandler: Int
val exception: Exception?
val attributes: Map<*, *>

fun with(
event: T = this.event,
predicate: Predicate<T> = this.predicate,
predicate: (Context<T>) -> Boolean = this.predicate,
nextHandlers: List<Handler<T>> = this.nextHandlers,
nextHandler: Int = this.nextHandler,
exception: Exception? = this.exception,
attributes: Map<*, *> = this.attributes,
): Context<T>

fun next(): Context<T>
fun next(): Context<T> {
for (index in nextHandler until nextHandlers.size) {
val handler = nextHandlers[index]
val p = handler.predicate
if (p(this))
return handler.process(with(predicate = p, nextHandler = index + 1))
}

return this
}
}
15 changes: 12 additions & 3 deletions handlers/src/main/kotlin/com/hexagonkt/handlers/FilterHandler.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package com.hexagonkt.handlers

data class FilterHandler<T : Any>(
override val predicate: Predicate<T> = { true },
override val callback: Callback<T>,
) : Handler<T>
override val predicate: (Context<T>) -> Boolean = { true },
override val callback: (Context<T>) -> Context<T>,
) : Handler<T> {

override fun process(context: Context<T>): Context<T> =
try {
callback(context)
}
catch (e: Exception) {
context.with(exception = e)
}
}
12 changes: 3 additions & 9 deletions handlers/src/main/kotlin/com/hexagonkt/handlers/Handler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ package com.hexagonkt.handlers
* @param T Event type.
*/
interface Handler<T : Any> {
val predicate: Predicate<T>
val callback: Callback<T>
val predicate: (Context<T>) -> Boolean
val callback: (Context<T>) -> Context<T>

fun process(context: Context<T>): Context<T> =
try {
callback(context)
}
catch (e: Exception) {
context.with(exception = e)
}
fun process(context: Context<T>): Context<T>
}
4 changes: 0 additions & 4 deletions handlers/src/main/kotlin/com/hexagonkt/handlers/Handlers.kt

This file was deleted.

11 changes: 5 additions & 6 deletions handlers/src/main/kotlin/com/hexagonkt/handlers/OnHandler.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.hexagonkt.handlers

data class OnHandler<T : Any>(
override val predicate: Predicate<T> = { true },
val beforeCallback: Callback<T>,
override val predicate: (Context<T>) -> Boolean = { true },
override val callback: (Context<T>) -> Context<T>,
) : Handler<T> {

override val callback: Callback<T> = {
override fun process(context: Context<T>): Context<T> =
try {
beforeCallback(it).next()
callback(context).next()
}
catch (e: Exception) {
it.with(exception = e).next()
context.with(exception = e).next()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.hexagonkt.handlers

/**
* Context for an event.
*
* @param T Event type.
*/
data class EventContext<T : Any>(
override val event: T,
override val predicate: Predicate<T>,
override val predicate: (Context<T>) -> Boolean,
override val nextHandlers: List<Handler<T>> = emptyList(),
override val nextHandler: Int = 0,
override val exception: Exception? = null,
Expand All @@ -16,7 +11,7 @@ data class EventContext<T : Any>(

override fun with(
event: T,
predicate: Predicate<T>,
predicate: (Context<T>) -> Boolean,
nextHandlers: List<Handler<T>>,
nextHandler: Int,
exception: Exception?,
Expand All @@ -30,16 +25,4 @@ data class EventContext<T : Any>(
exception = exception,
attributes = attributes,
)

override fun next(): Context<T> {
for (index in nextHandler until nextHandlers.size) {
val handler = nextHandlers[index]
if (handler.predicate(this))
return handler.process(
with(predicate = handler.predicate, nextHandler = index + 1)
)
}

return this
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hexagonkt.http.server.handlers

import com.hexagonkt.handlers.Callback
import com.hexagonkt.handlers.Context
import com.hexagonkt.http.model.*
import com.hexagonkt.http.model.HttpMethod.*
import com.hexagonkt.http.model.HttpProtocol.HTTP
Expand All @@ -13,8 +13,8 @@ import kotlin.reflect.cast
typealias HttpCallback = HttpServerContext.() -> HttpServerContext
typealias HttpExceptionCallback<T> = HttpServerContext.(T) -> HttpServerContext

internal fun toCallback(handler: HttpCallback): Callback<HttpServerCall> =
{ context -> HttpServerContext(context).handler().context }
internal fun toCallback(block: HttpCallback): (Context<HttpServerCall>) -> Context<HttpServerCall> =
{ context -> HttpServerContext(context).block() }

fun HttpCallback.process(
request: HttpServerRequest,
Expand Down
Loading

0 comments on commit 6bcfacb

Please sign in to comment.