From d250e9c08c1919f94f7e830b2e8e4a55708e0c72 Mon Sep 17 00:00:00 2001 From: Nikolai Obedin Date: Wed, 14 Aug 2024 18:48:38 +0200 Subject: [PATCH] Add cleanUpWorkerEnabled flag to control clean up worker execution Allow disabling clean up worker if a repository's built-in expiration mechanism could be used for it. Closes #14 --- .../utils/ktor/idempotency/IdempotencyPlugin.kt | 16 +++++++++------- .../ktor/idempotency/PluginConfiguration.kt | 6 ++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotencyPlugin.kt b/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotencyPlugin.kt index 4bcaa74..26ffccd 100644 --- a/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotencyPlugin.kt +++ b/src/main/kotlin/com/wolt/utils/ktor/idempotency/IdempotencyPlugin.kt @@ -31,13 +31,15 @@ val IdempotencyPlugin = pluginConfig.idempotentResponseRepository ?: throw IllegalArgumentException("IdempotentRequestRepository must be provided") - CleanUpWorker( - scope = pluginConfig.cleanUpWorkerScope, - jitter = pluginConfig.cleanUpWorkerJitter, - interval = pluginConfig.cleanUpWorkerInterval, - idempotentResponseRepository = responseRepository, - storedResponseTTL = pluginConfig.storedResponseTTL, - ).start() + if (pluginConfig.cleanUpWorkerEnabled) { + CleanUpWorker( + scope = pluginConfig.cleanUpWorkerScope, + jitter = pluginConfig.cleanUpWorkerJitter, + interval = pluginConfig.cleanUpWorkerInterval, + idempotentResponseRepository = responseRepository, + storedResponseTTL = pluginConfig.storedResponseTTL, + ).start() + } val logger = KotlinLogging.logger {} diff --git a/src/main/kotlin/com/wolt/utils/ktor/idempotency/PluginConfiguration.kt b/src/main/kotlin/com/wolt/utils/ktor/idempotency/PluginConfiguration.kt index 74de7ec..a1243cf 100644 --- a/src/main/kotlin/com/wolt/utils/ktor/idempotency/PluginConfiguration.kt +++ b/src/main/kotlin/com/wolt/utils/ktor/idempotency/PluginConfiguration.kt @@ -45,6 +45,12 @@ class PluginConfiguration { HttpMethod.Patch, ) + /** + * Flag that controls execution of clean up worker. + * Clean up worker is enabled by default, but consider disabling it if your response repository can leverage a built-in expiration mechanism (e.g. Redis key expiration time). + */ + var cleanUpWorkerEnabled = true + /** * The coroutine scope for the worker that cleans up expired responses. */