diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..ba5a4b1 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: gradle + directory: "/" + schedule: + interval: daily \ No newline at end of file diff --git a/.github/workflows/pre-merge.yml b/.github/workflows/pre-merge.yml new file mode 100644 index 0000000..85d8367 --- /dev/null +++ b/.github/workflows/pre-merge.yml @@ -0,0 +1,44 @@ +name: PR Checks +on: + push: + branches: + - main + pull_request: + branches: + - '*' + +jobs: + tests: + name: Run tests + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v2 + - name: Run tests + uses: gradle/gradle-build-action@v2 + with: + arguments: test + publish: + name: Check that the publish plugin works + runs-on: ubuntu-latest + needs: [tests, lint] + steps: + - name: Checkout Repo + uses: actions/checkout@v2 + - name: Check that the publish plugin works + env: + ORG_GRADLE_PROJECT_signingKey: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGKEY }} + ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGPASSWORD }} + uses: gradle/gradle-build-action@v2 + with: + arguments: publishToMavenLocal + lint: + name: Check that the code is formatted + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v2 + - name: Check that the code is formatted + uses: gradle/gradle-build-action@v2 + with: + arguments: spotlessCheck \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd00d92 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c53f736 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2022 Wolt Enterprises Oy + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..aaab880 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,104 @@ +plugins { + kotlin("jvm") version "1.9.21" + kotlin("plugin.serialization") version "1.9.23" + id("com.diffplug.spotless") version "6.25.0" + id("maven-publish") + id("signing") +} + +group = "com.wolt" + +version = "1.0-SNAPSHOT" + +repositories { mavenCentral() } + +java { + withSourcesJar() + withJavadocJar() +} + +dependencies { + val ktorVersion = "2.3.9" + api("io.ktor:ktor-server-core-jvm:${ktorVersion}") + implementation("io.ktor:ktor-server-netty-jvm:${ktorVersion}") + implementation("io.ktor:ktor-server-content-negotiation:${ktorVersion}") + implementation("io.ktor:ktor-serialization-kotlinx-json:${ktorVersion}") + + implementation("io.github.oshai:kotlin-logging-jvm:5.1.0") + + testImplementation("io.ktor:ktor-server-status-pages:2.3.9") + testImplementation("io.ktor:ktor-server-test-host:${ktorVersion}") + + testImplementation("io.mockk:mockk:1.13.11") + testImplementation("org.jetbrains.kotlin:kotlin-test") +} + +tasks.test { useJUnitPlatform() } + +kotlin { jvmToolchain(21) } + +spotless { + kotlin { + ktfmt("0.47") + ktlint() + } + kotlinGradle { ktfmt("0.47").kotlinlangStyle() } +} + +publishing { + publications { + create("mavenJava") { + from(components["kotlin"]) + groupId = project.group.toString() + artifactId = project.name + version = project.version.toString() + artifact(tasks.kotlinSourcesJar) + artifact(tasks.named("javadocJar")) + + pom { + name.set("Ktor Idempotency Plugin") + description.set("A Ktor plugin for handling idempotency in HTTP requests") + url.set("https://github.com/woltapp/ktor-idempotency") + + name.set("Your Project Name") + description.set("A description of your project") + url.set("https://your.project.url") + licenses { + license { + name.set("The MIT License") + url.set("https://github.com/woltapp/ktor-idempotency/blob/main/LICENSE") + } + } + developers { + developer { + id.set("muatik") + name.set("Mustafa Atik") + email.set("mustafa.atik@wolt.com") + } + } + scm { + connection.set("scm:git:https://github.com/woltapp/ktor-idempotency.git") + developerConnection.set( + "scm:git:https://github.com/woltapp/ktor-idempotency.git" + ) + url.set("https://github.com/woltapp/ktor-idempotency") + } + } + } + } + + repositories { + maven { + name = "sonatype" + url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/") + credentials(PasswordCredentials::class) + } + } +} + +signing { + val signingKey: String? by project + val signingPassword: String? by project + useInMemoryPgpKeys(signingKey, signingPassword) + sign(publishing.publications["mavenJava"]) +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..7fc6f1f --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..6bfc5c2 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue May 28 20:29:37 CEST 2024 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..d0863c0 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,3 @@ +plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0" } + +rootProject.name = "ktor-idempotency" diff --git a/src/main/kotlin/com/wolt/utils/ktor/idempotency/CleanUpWorker.kt b/src/main/kotlin/com/wolt/utils/ktor/idempotency/CleanUpWorker.kt new file mode 100644 index 0000000..55ad8d8 --- /dev/null +++ b/src/main/kotlin/com/wolt/utils/ktor/idempotency/CleanUpWorker.kt @@ -0,0 +1,43 @@ +package com.wolt.utils.ktor.idempotency + +import io.github.oshai.kotlinlogging.KotlinLogging +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import java.time.Duration +import java.time.OffsetDateTime + +class CleanUpWorker( + private val scope: CoroutineScope, + private val jitter: Duration, + private val interval: Duration, + private val idempotentResponseRepository: IdempotentResponseRepository, + private val storedResponseTTL: Duration, +) { + private val logger = KotlinLogging.logger {} + + fun start() { + val job = + scope.launch(Dispatchers.IO, start = CoroutineStart.LAZY) { + while (isActive) { + delay(sleepDuration().toMillis()) + try { + idempotentResponseRepository.deleteExpiredResponses( + OffsetDateTime.now().minus(storedResponseTTL), + ) + } catch (e: Exception) { + logger.error(e) { "Cannot clean up expired responses" } + } + } + } + job.start() + } + + private fun sleepDuration(): Duration { + val jitter = (0L..jitter.toMillis()).random() + return interval.plus(Duration.ofMillis(jitter)) + } +} diff --git a/src/main/kotlin/com/wolt/utils/ktor/idempotency/EventListener.kt b/src/main/kotlin/com/wolt/utils/ktor/idempotency/EventListener.kt new file mode 100644 index 0000000..73047ca --- /dev/null +++ b/src/main/kotlin/com/wolt/utils/ktor/idempotency/EventListener.kt @@ -0,0 +1,25 @@ +package com.wolt.utils.ktor.idempotency + +interface EventListener { + fun onEvent(event: Event) +} + +data class Event( + val eventType: EventType, + val resource: String, + val idempotencyKey: IdempotencyKey, +) + +enum class EventType { + /** + * Event type for when a stored response is returned to the client skipping + * the actual request processing. + */ + STORED_RESPONSE_USED, + + /** + * Event type for when a stored response is expired and proceeding with the + * actual request processing. + */ + STORED_RESPONSE_EXPIRED, +} diff --git a/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotencyPlugin.kt b/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotencyPlugin.kt new file mode 100644 index 0000000..bbb1212 --- /dev/null +++ b/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotencyPlugin.kt @@ -0,0 +1,229 @@ +package com.wolt.utils.ktor.idempotency + +import io.github.oshai.kotlinlogging.KLogger +import io.github.oshai.kotlinlogging.KotlinLogging +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.content.OutgoingContent +import io.ktor.http.content.TextContent +import io.ktor.server.application.ApplicationCall +import io.ktor.server.application.createRouteScopedPlugin +import io.ktor.server.application.hooks.ResponseBodyReadyForSend +import io.ktor.server.http.content.HttpStatusCodeContent +import io.ktor.server.request.httpMethod +import io.ktor.server.request.uri +import io.ktor.server.response.respond +import io.ktor.server.response.respondBytes +import io.ktor.server.response.respondText +import io.ktor.util.toByteArray +import io.ktor.util.toMap +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import java.time.OffsetDateTime + +val IdempotencyPlugin = + createRouteScopedPlugin( + name = "IdempotencyPlugin", + createConfiguration = ::PluginConfiguration, + ) { + + val responseRepository = + pluginConfig.idempotentResponseRepository + ?: throw IllegalArgumentException("IdempotentRequestRepository must be provided") + + CleanUpWorker( + scope = pluginConfig.cleanUpWorkerScope, + jitter = pluginConfig.cleanUpWorkerJitter, + interval = pluginConfig.cleanUpWorkerInterval, + idempotentResponseRepository = responseRepository, + storedResponseTTL = pluginConfig.storedResponseTTL, + ).start() + + val logger = KotlinLogging.logger {} + + onCall { call -> + try { + interceptRequest(call, logger, pluginConfig, responseRepository) + } catch (e: Exception) { + logger.error { "Cannot intercept request: $e to handle idempotency" } + if (pluginConfig.failOnError) { + throw e + } + } + } + + on(ResponseBodyReadyForSend) { call, content -> + try { + storeResponse(call, logger, content, pluginConfig) + } catch (e: Exception) { + logger.error { "Cannot store response: $e" } + if (pluginConfig.failOnError) { + throw e + } + } + } + } + +private suspend fun interceptRequest( + call: ApplicationCall, + logger: KLogger, + pluginConfig: PluginConfiguration, + responseRepository: IdempotentResponseRepository, +) { + logger.info { "Intercepting request: ${call.request.httpMethod} ${call.request.uri}" } + call.attributes.put(PluginConfiguration.attributeKey, false) + if (call.request.httpMethod !in pluginConfig.idempotentHttpMethods) { + return + } + val idempotencyKey = getIdempotencyKey(call) ?: return + + logger.debug { "Idempotency key: $idempotencyKey" } + val requestIdentity = getRequestIdentity(call) + val idempotencyRecord = responseRepository.getResponseOrLock(requestIdentity, idempotencyKey) ?: return + + if (idempotencyRecord.isInProgress) { + call.attributes.put(PluginConfiguration.attributeKey, true) + call.respondText( + text = "There is currently another in-progress request with this Idempotency Key.", + contentType = ContentType.Text.Plain, + status = HttpStatusCode.Conflict, + ) + return + } + + val storedResponse = Json.decodeFromString(String(idempotencyRecord.response)) + + if (storedResponse.validUntil.isBefore(OffsetDateTime.now())) { + pluginConfig.eventListener?.onEvent( + Event( + eventType = EventType.STORED_RESPONSE_EXPIRED, + resource = requestIdentity, + idempotencyKey = idempotencyKey, + ), + ) + return + } + + call.attributes.put(PluginConfiguration.attributeKey, true) + + sendStoredResponse(logger, idempotencyKey, storedResponse, call) + + pluginConfig.eventListener?.onEvent( + Event( + eventType = EventType.STORED_RESPONSE_USED, + resource = requestIdentity, + idempotencyKey = idempotencyKey, + ), + ) +} + +private suspend fun sendStoredResponse( + logger: KLogger, + idempotencyKey: IdempotencyKey, + storedResponse: IdempotentResponse, + call: ApplicationCall, +) { + logger.debug { "Returning stored response for idempotent request: $idempotencyKey" } + + logger.debug { "Stored response: $storedResponse" } + + storedResponse.headers.forEach { (key, values) -> + values.forEach { value -> + call.response.headers.append(key, value) + } + } + + val contentType = + storedResponse.contentType?.let { + ContentType(storedResponse.contentType.contentType, storedResponse.contentType.contentSubType) + } + + val status = HttpStatusCode.fromValue(storedResponse.status) + + when (storedResponse.responseType) { + SupportedResponseTypes.TextContent -> + call.respondText( + text = storedResponse.content?.let { String(storedResponse.content) } ?: "", + contentType = ContentType.Application.Json, + status = status, + ) + + SupportedResponseTypes.ReadChannelContent, SupportedResponseTypes.ByteArrayContent -> + call.respondBytes( + bytes = storedResponse.content!!, + contentType = contentType, + status = status, + ) + + SupportedResponseTypes.HttpStatusCodeContent -> call.respond(status) + } +} + +private suspend fun storeResponse( + call: ApplicationCall, + logger: KLogger, + content: OutgoingContent, + pluginConfig: PluginConfiguration, +) { + val isStoredResponse = call.attributes[PluginConfiguration.attributeKey] + if (isStoredResponse) { + return + } + + if (call.request.httpMethod !in pluginConfig.idempotentHttpMethods) { + return + } + + val idempotencyKey = getIdempotencyKey(call) ?: return + val requestIdentity = getRequestIdentity(call) + + val status = + when (content) { + is OutgoingContent.ByteArrayContent -> content.status?.value ?: call.response.status()?.value + is HttpStatusCodeContent -> content.status.value + else -> null + } ?: HttpStatusCode.OK.value + + val headers = call.response.headers.allValues().toMap() + + val responseType = + when (content) { + is TextContent -> SupportedResponseTypes.TextContent + is OutgoingContent.ByteArrayContent -> SupportedResponseTypes.ByteArrayContent + is HttpStatusCodeContent -> SupportedResponseTypes.HttpStatusCodeContent + is OutgoingContent.ReadChannelContent -> SupportedResponseTypes.ReadChannelContent + else -> throw Exception("Unsupported content type") + } + + val response = + IdempotentResponse( + status = status, + headers = headers, + responseType = responseType, + contentType = IdempotentContentTypes.fromContentType(content.contentType), + content = getStoredResponseContent(content), + createdAt = OffsetDateTime.now(), + validUntil = OffsetDateTime.now().plus(pluginConfig.storedResponseTTL), + ) + + val serialisedResponse = Json.encodeToString(response) + logger.debug { "Storing response for idempotent request: $idempotencyKey" } + pluginConfig.idempotentResponseRepository?.storeResponse( + requestIdentity, + idempotencyKey, + serialisedResponse.toByteArray(), + ) +} + +private fun getRequestIdentity(call: ApplicationCall) = "${call.request.httpMethod.value} ${call.request.uri}" + +private suspend fun getStoredResponseContent(content: OutgoingContent) = + when (content) { + is OutgoingContent.ByteArrayContent -> content.bytes() + is OutgoingContent.ReadChannelContent -> content.readFrom().toByteArray() + else -> null + } + +private const val IDEMPOTENCY_KEY_HEADER = "Idempotency-Key" + +fun getIdempotencyKey(call: ApplicationCall): IdempotencyKey? = call.request.headers[IDEMPOTENCY_KEY_HEADER]?.let(::IdempotencyKey) diff --git a/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotentResponseRepository.kt b/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotentResponseRepository.kt new file mode 100644 index 0000000..0b9a45b --- /dev/null +++ b/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotentResponseRepository.kt @@ -0,0 +1,65 @@ +package com.wolt.utils.ktor.idempotency + +import io.ktor.http.ContentType +import kotlinx.serialization.Serializable +import java.time.OffsetDateTime + +interface IdempotentResponseRepository { + fun storeResponse( + resource: String, + idempotencyKey: IdempotencyKey, + response: ByteArray, + ) + + fun getResponseOrLock( + resource: String, + idempotencyKey: IdempotencyKey, + ): IdempotencyResponse? + + fun deleteExpiredResponses(lastValidDate: OffsetDateTime) +} + +data class IdempotencyResponse( + val isInProgress: Boolean, + val response: ByteArray, +) + +@JvmInline +value class IdempotencyKey(val value: String) { + override fun toString(): String { + return value + } +} + +@Serializable +data class IdempotentResponse( + val status: Int, + val headers: Map>, + val responseType: SupportedResponseTypes, + val contentType: IdempotentContentTypes?, + @Serializable(with = OffsetDateTimeSerializer::class) + val createdAt: OffsetDateTime, + @Serializable(with = OffsetDateTimeSerializer::class) + val validUntil: OffsetDateTime, + val content: ByteArray?, +) + +enum class SupportedResponseTypes { + TextContent, + ByteArrayContent, + ReadChannelContent, + HttpStatusCodeContent, +} + +@Serializable +data class IdempotentContentTypes( + val contentType: String, + val contentSubType: String, +) { + companion object { + fun fromContentType(contentType: ContentType?): IdempotentContentTypes? = + contentType?.let { + IdempotentContentTypes(contentType.toString(), contentType.contentSubtype) + } + } +} diff --git a/src/main/kotlin/com/wolt/utils/ktor/idempotency/OffsetDateTimeSerializer.kt b/src/main/kotlin/com/wolt/utils/ktor/idempotency/OffsetDateTimeSerializer.kt new file mode 100644 index 0000000..310cfb5 --- /dev/null +++ b/src/main/kotlin/com/wolt/utils/ktor/idempotency/OffsetDateTimeSerializer.kt @@ -0,0 +1,24 @@ +package com.wolt.utils.ktor.idempotency + +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializer +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import java.time.OffsetDateTime +import java.time.format.DateTimeFormatter + +@OptIn(ExperimentalSerializationApi::class) +@Serializer(forClass = OffsetDateTime::class) +object OffsetDateTimeSerializer : KSerializer { + private val formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME + + override fun serialize( + encoder: Encoder, + value: OffsetDateTime, + ) { + encoder.encodeString(formatter.format(value)) + } + + override fun deserialize(decoder: Decoder): OffsetDateTime = OffsetDateTime.parse(decoder.decodeString(), formatter) +} diff --git a/src/main/kotlin/com/wolt/utils/ktor/idempotency/PluginConfiguration.kt b/src/main/kotlin/com/wolt/utils/ktor/idempotency/PluginConfiguration.kt new file mode 100644 index 0000000..74de7ec --- /dev/null +++ b/src/main/kotlin/com/wolt/utils/ktor/idempotency/PluginConfiguration.kt @@ -0,0 +1,66 @@ +package com.wolt.utils.ktor.idempotency + +import io.ktor.http.HttpMethod +import io.ktor.util.AttributeKey +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import java.time.Duration + +class PluginConfiguration { + var idempotentResponseRepository: IdempotentResponseRepository? = null + + /** + * If true, the plugin will throw an exception if an error occurs during the processing of the request. + * If false, the plugin will log the error and continue processing the request. + * + * Default is true. It is recommended to set this to true in production environments to ensure + * that duplicate requests are not processed. While in adoption phase, it is recommended to set this + * to false. + */ + var failOnError = true + + /** + * The time-to-live for stored responses. After this time, the stored response will be considered + * expired and will not be returned for subsequent requests but processed as a normal request. + * + * Default is 7 days. + */ + var storedResponseTTL: Duration = Duration.ofDays(7) + + /** + * The plugin will call this listener for events such as + * stored response is retrieved and returned, or it is expired. + */ + var eventListener: EventListener? = null + + /** + * The plugin will only store responses for requests with these HTTP methods. + */ + var idempotentHttpMethods = + setOf( + HttpMethod.Post, + HttpMethod.Put, + HttpMethod.Delete, + HttpMethod.Patch, + ) + + /** + * The coroutine scope for the worker that cleans up expired responses. + */ + var cleanUpWorkerScope: CoroutineScope = CoroutineScope(Dispatchers.IO + CoroutineName("idempotencyCleanUpExpiredResponses")) + + /** + * The interval at which the worker will clean up expired responses. + */ + var cleanUpWorkerInterval: Duration = Duration.ofMinutes(10) + + /** + * The jitter to add to the interval to prevent all workers from running at the same time. + */ + var cleanUpWorkerJitter: Duration = Duration.ofMinutes(10) + + companion object { + val attributeKey = AttributeKey("isIdempotentResponse") + } +} diff --git a/src/test/kotlin/IdempotencyPluginTests.kt b/src/test/kotlin/IdempotencyPluginTests.kt new file mode 100644 index 0000000..2e77cd0 --- /dev/null +++ b/src/test/kotlin/IdempotencyPluginTests.kt @@ -0,0 +1,561 @@ + +package com.example + +import com.wolt.utils.ktor.idempotency.Event +import com.wolt.utils.ktor.idempotency.EventListener +import com.wolt.utils.ktor.idempotency.EventType +import com.wolt.utils.ktor.idempotency.IdempotencyKey +import com.wolt.utils.ktor.idempotency.IdempotencyPlugin +import com.wolt.utils.ktor.idempotency.IdempotentResponse +import com.wolt.utils.ktor.idempotency.SupportedResponseTypes +import io.ktor.client.request.delete +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.patch +import io.ktor.client.request.post +import io.ktor.client.request.put +import io.ktor.client.statement.HttpResponse +import io.ktor.client.statement.bodyAsText +import io.ktor.http.HttpMethod +import io.ktor.http.HttpStatusCode +import io.ktor.http.HttpStatusCode.Companion.OK +import io.ktor.server.application.call +import io.ktor.server.plugins.statuspages.StatusPages +import io.ktor.server.response.respond +import io.ktor.server.response.respondBytes +import io.ktor.server.response.respondFile +import io.ktor.server.response.respondText +import io.ktor.server.routing.delete +import io.ktor.server.routing.get +import io.ktor.server.routing.patch +import io.ktor.server.routing.post +import io.ktor.server.routing.put +import io.ktor.server.testing.ApplicationTestBuilder +import io.ktor.server.testing.testApplication +import io.mockk.mockk +import io.mockk.spyk +import io.mockk.verify +import kotlinx.coroutines.async +import kotlinx.coroutines.runBlocking +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import java.io.File +import java.time.Duration +import java.time.OffsetDateTime +import java.util.UUID +import kotlin.test.assertNull + +class IdempotencyPluginTests { + private lateinit var testEventListener: TestEventListener + private lateinit var repository: InMemoryResponseRepository + private lateinit var service: DummyService + + @Test + fun shouldUseStoredResponseForTextResponse() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/respondWithText" + + val response = sendPostRequest(path, idempotencyKey) + assertOkResponse(response, "Hello, world!") + + val response2 = sendPostRequest(path, idempotencyKey) + assertOkResponse(response2, "Hello, world!") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForCustomHeader() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/customHeader" + + val response = sendPatchRequest(path, idempotencyKey) + assertOkResponse(response, "Hello, world!") + + val response2 = sendPatchRequest(path, idempotencyKey) + assertOkResponse(response2, "Hello, world!") + assertEquals(response.headers["X-Custom-Header"], "value") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForOnlyStatus() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/onlyStatus" + + val response = sendPutRequest(path, idempotencyKey) + assertResponse(response, HttpStatusCode.Created, "") + + val response2 = sendPutRequest(path, idempotencyKey) + assertResponse(response2, HttpStatusCode.Created, "") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForStatusWithText() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/statusWithText" + + val response = sendDeleteRequest(path, idempotencyKey) + assertResponse(response, HttpStatusCode.Created, "created") + + val response2 = sendDeleteRequest(path, idempotencyKey) + assertResponse(response2, HttpStatusCode.Created, "created") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForNotFound() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/notfound" + + val response = sendPostRequest(path, idempotencyKey) + assertResponse(response, HttpStatusCode.NotFound, "") + + val response2 = sendPostRequest(path, idempotencyKey) + assertResponse(response2, HttpStatusCode.NotFound, "") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForInternalServerError() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/internalError" + + val response = sendPostRequest(path, idempotencyKey) + assertResponse(response, HttpStatusCode.InternalServerError, "") + + val response2 = sendPostRequest(path, idempotencyKey) + assertResponse(response2, HttpStatusCode.InternalServerError, "") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForException() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/exception" + + val response = sendPutRequest(path, idempotencyKey) + assertResponse(response, HttpStatusCode.InternalServerError, "Internal Server Error") + + val response2 = sendPutRequest(path, idempotencyKey) + assertResponse(response2, HttpStatusCode.InternalServerError, "Internal Server Error") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForBytes() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/bytes" + + val response = sendPostRequest(path, idempotencyKey) + assertResponse(response, OK, "byte content") + + val response2 = sendPostRequest(path, idempotencyKey) + assertResponse(response2, OK, "byte content") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForFile() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/file" + + val response = sendPostRequest(path, idempotencyKey) + assertResponse(response, OK, "test file content") + + val response2 = sendPostRequest(path, idempotencyKey) + assertResponse(response2, OK, "test file content") + + assertServiceCalledOnlyOnce() + } + + @Test + fun shouldUseStoredResponseForDifferentIdempotencyKeys() = + testApplication { + setupModules(this) + val idempotencyKey1 = UUID.randomUUID().toString() + val idempotencyKey2 = UUID.randomUUID().toString() + val path = "/test/respondWithText" + + val response = sendPostRequest(path, idempotencyKey1) + assertOkResponse(response, "Hello, world!") + assertServiceCalled(timesInTotal = 1) + + val response2 = sendPostRequest(path, idempotencyKey2) + assertOkResponse(response2, "Hello, world!") + assertServiceCalled(timesInTotal = 2) + } + + @Test + fun shouldUseStoredResponseForDifferentPaths() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path1 = "/test/respondWithText" + val path2 = "/test/customHeader" + + val response = sendPostRequest(path1, idempotencyKey) + assertOkResponse(response, "Hello, world!") + assertServiceCalled(timesInTotal = 1) + + val response2 = sendPatchRequest(path2, idempotencyKey) + assertOkResponse(response2, "Hello, world!") + assertServiceCalled(timesInTotal = 2) + } + + @Test + fun shouldUseStoredResponseForDifferentMethods() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/respondWithText" + + val response = sendGetRequest(path, idempotencyKey) + assertOkResponse(response, "Hello, world for get request!") + assertServiceCalled(timesInTotal = 1) + + val response2 = sendPostRequest("/test/12/respondWithText?q=v", idempotencyKey) + assertOkResponse(response2, "Hello, world!") + assertServiceCalled(timesInTotal = 2) + } + + @Test + fun shouldNotUseStoredResponseWhenExpired() = + testApplication { + setupModules(this) + + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/respondWithText" + insertExpiredResponse(idempotencyKey, path) + + val response = sendPostRequest(path, idempotencyKey) + assertOkResponse(response, "Hello, world!") + assertServiceCalled(timesInTotal = 1) + } + + @Test + fun shouldNotStoreAgainWhenStoredResponseIsUsed() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/respondWithText" + + val response = sendPostRequest(path, idempotencyKey) + assertOkResponse(response, "Hello, world!") + assertServiceCalled(timesInTotal = 1) + verify(exactly = 1) { repository.storeResponse("POST $path", any(), any()) } + + val response2 = sendPostRequest(path, idempotencyKey) + assertOkResponse(response2, "Hello, world!") + assertServiceCalled(timesInTotal = 1) + // storeResponse should not be called again + verify(exactly = 1) { repository.storeResponse("POST $path", any(), any()) } + } + + @Test + fun shouldEmitEventWhenStoredResponseIsUsed() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/respondWithText" + + val response = sendPostRequest(path, idempotencyKey) + assertOkResponse(response, "Hello, world!") + assertNoEventTriggered() + + val response2 = sendPostRequest(path, idempotencyKey) + assertOkResponse(response2, "Hello, world!") + assertTrue(testEventListener.hasEvent(EventType.STORED_RESPONSE_USED, "POST $path", idempotencyKey)) + } + + @Test + fun shouldEmitEventWhenStoredResponseIsExpired() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/respondWithText" + insertExpiredResponse(idempotencyKey, path) + + val response = sendPostRequest(path, idempotencyKey) + assertOkResponse(response, "Hello, world!") + assertTrue(testEventListener.hasEvent(EventType.STORED_RESPONSE_EXPIRED, "POST $path", idempotencyKey)) + } + + @Test + fun shouldIgnoreGetRequestsBecauseItIsNotConfigured() = + testApplication { + setupModules(this) + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/respondWithText" + + val response1 = sendGetRequest(path, idempotencyKey) + val response2 = sendGetRequest(path, idempotencyKey) + + assertOkResponse(response1, "Hello, world for get request!") + assertOkResponse(response2, "Hello, world for get request!") + + verify(exactly = 0) { repository.storeResponse(any(), any(), any()) } + assertNoEventTriggered() + assertServiceCalled(timesInTotal = 2) + } + + @Test + fun shouldCleanUpExpiredResponses() = + testApplication { + repository = InMemoryResponseRepository() + install(IdempotencyPlugin) { + idempotentResponseRepository = repository + cleanUpWorkerInterval = Duration.ofMillis(1) + cleanUpWorkerJitter = Duration.ofMillis(1) + } + + val idempotencyKey = UUID.randomUUID().toString() + val path = "/test/respondWithText" + + sendPostRequest(path, idempotencyKey) + + // override the response to make it expired + insertExpiredResponse(idempotencyKey, path) + Thread.sleep(Duration.ofMillis(5)) + + val storedResponse = repository.getResponseOrLock("POST $path", IdempotencyKey(idempotencyKey)) + + assertNull(storedResponse) + } + + @Test + fun shouldExecuteOnlyOneParallelQuery() = + runBlocking { + testApplication { + setupModules(this) + val path = "/test/respondWithText" + + val testTimes = 100 + for (testTime in 0 until testTimes) { + val idempotencyKey = UUID.randomUUID().toString() + val response1Deferred = async { sendPostRequest(path, idempotencyKey) } + val response2Deferred = async { sendPostRequest(path, idempotencyKey) } + + response1Deferred.await() + response2Deferred.await() + } + + assertServiceCalled(testTimes) + } + } + + private fun insertExpiredResponse( + idempotencyKey: String, + path: String, + method: HttpMethod = HttpMethod.Post, + ): IdempotentResponse { + val expiredIdempotentResponse = + IdempotentResponse( + status = 200, + headers = emptyMap(), + responseType = SupportedResponseTypes.TextContent, + contentType = null, + createdAt = OffsetDateTime.now(), + validUntil = OffsetDateTime.now().minusSeconds(1), + content = "Hello, world!".toByteArray(), + ) + + repository.storeResponse( + "${method.value} $path", + IdempotencyKey(idempotencyKey), + Json.encodeToString(expiredIdempotentResponse).toByteArray(), + ) + return expiredIdempotentResponse + } + + private fun setupModules(applicationTestBuilder: ApplicationTestBuilder) { + applicationTestBuilder.setupRoutes() + applicationTestBuilder.setupPlugin() + } + + private fun assertServiceCalledOnlyOnce() { + assertServiceCalled(timesInTotal = 1) + } + + private fun assertServiceCalled(timesInTotal: Int = 1) { + verify(exactly = timesInTotal) { service.execute() } + } + + private suspend fun assertOkResponse( + response: HttpResponse, + body: String, + ) { + assertResponse(response, OK, body) + } + + private suspend fun assertResponse( + response: HttpResponse, + status: HttpStatusCode, + body: String, + ) { + assertEquals(response.status, status) + assertEquals(response.bodyAsText(), body) + } + + private fun assertNoEventTriggered() { + assertTrue(testEventListener.isEmpty()) + } + + private suspend fun ApplicationTestBuilder.sendGetRequest( + path: String, + idempotencyKey: String, + ) = client.get(path) { + header("Idempotency-Key", idempotencyKey) + } + + private suspend fun ApplicationTestBuilder.sendPostRequest( + path: String, + idempotencyKey: String, + ) = client.post(path) { + header("Idempotency-Key", idempotencyKey) + } + + private suspend fun ApplicationTestBuilder.sendPutRequest( + path: String, + idempotencyKey: String, + ) = client.put(path) { + header("Idempotency-Key", idempotencyKey) + } + + private suspend fun ApplicationTestBuilder.sendDeleteRequest( + path: String, + idempotencyKey: String, + ) = client.delete(path) { + header("Idempotency-Key", idempotencyKey) + } + + private suspend fun ApplicationTestBuilder.sendPatchRequest( + path: String, + idempotencyKey: String, + ) = client.patch(path) { + header("Idempotency-Key", idempotencyKey) + } + + private fun ApplicationTestBuilder.setupPlugin() { + repository = spyk() + + testEventListener = TestEventListener() + + install(IdempotencyPlugin) { + idempotentResponseRepository = repository + storedResponseTTL = Duration.ofSeconds(3) + eventListener = testEventListener + } + install(StatusPages) { + exception { call, cause -> + call.respondText(text = "Internal Server Error", status = HttpStatusCode.InternalServerError) + } + } + } + + private fun ApplicationTestBuilder.setupRoutes() { + service = mockk(relaxed = true) + routing { + get("/test/respondWithText") { + service.execute() + call.respond("Hello, world for get request!") + } + post("/test/respondWithText") { + service.execute() + call.respond("Hello, world!") + } + post("/test/{id}/respondWithText") { + service.execute() + call.respond("Hello, world!") + } + patch("/test/customHeader") { + service.execute() + call.response.headers.append("X-Custom-Header", "value") + call.respond("Hello, world!") + } + put("/test/onlyStatus") { + service.execute() + call.respond(HttpStatusCode.Created) + } + + delete("/test/statusWithText") { + service.execute() + call.respond(HttpStatusCode.Created, "created") + } + + post("/test/notfound") { + service.execute() + call.respond(HttpStatusCode.NotFound) + } + + post("/test/internalError") { + service.execute() + call.respond(HttpStatusCode.InternalServerError) + } + + put("/test/exception") { + service.execute() + throw Exception("test exception") + } + + post("/test/bytes") { + service.execute() + call.respondBytes { "byte content".toByteArray() } + } + + post("/test/file") { + service.execute() + call.respondFile(File("src/test/resources/test.txt")) + } + } + } +} + +class DummyService { + fun execute() {} +} + +class TestEventListener : EventListener { + private val events = mutableListOf() + + override fun onEvent(event: Event) { + events.add(event) + } + + fun hasEvent( + eventType: EventType, + resource: String, + idempotencyKey: String, + ) = events.any { it.eventType == eventType && it.resource == resource && it.idempotencyKey.value == idempotencyKey } + + fun isEmpty() = events.isEmpty() +} diff --git a/src/test/kotlin/InMemoryResponseRepository.kt b/src/test/kotlin/InMemoryResponseRepository.kt new file mode 100644 index 0000000..47b5717 --- /dev/null +++ b/src/test/kotlin/InMemoryResponseRepository.kt @@ -0,0 +1,41 @@ +package com.example + +import com.wolt.utils.ktor.idempotency.IdempotencyKey +import com.wolt.utils.ktor.idempotency.IdempotencyResponse +import com.wolt.utils.ktor.idempotency.IdempotentResponseRepository +import java.util.concurrent.ConcurrentHashMap + +class InMemoryResponseRepository : IdempotentResponseRepository { + private val responses = ConcurrentHashMap() + + override fun storeResponse( + resource: String, + idempotencyKey: IdempotencyKey, + response: ByteArray, + ) { + println("Storing response for idempotent request: $idempotencyKey") + responses[generateKey(resource, idempotencyKey)] = IdempotencyResponse(isInProgress = false, response = response) + } + + override fun getResponseOrLock( + resource: String, + idempotencyKey: IdempotencyKey, + ): IdempotencyResponse? { + println("Retrieving response for idempotent request: $idempotencyKey") + val record = + responses.putIfAbsent( + generateKey(resource, idempotencyKey), + IdempotencyResponse(isInProgress = true, response = ByteArray(0)), + ) + return record + } + + override fun deleteExpiredResponses(lastValidDate: java.time.OffsetDateTime) { + responses.clear() + } + + private fun generateKey( + resource: String, + idempotencyKey: IdempotencyKey, + ) = "$resource:$idempotencyKey" +} diff --git a/src/test/resources/test.txt b/src/test/resources/test.txt new file mode 100644 index 0000000..2211df3 --- /dev/null +++ b/src/test/resources/test.txt @@ -0,0 +1 @@ +test file content \ No newline at end of file