From 8f5c1715c399fa190f7be51c07b99efefb0da98a Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 5 Mar 2024 12:14:49 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20RetryPolicy=E3=82=92common=E3=83=91?= =?UTF-8?q?=E3=83=83=E3=82=B1=E3=83=BC=E3=82=B8=E3=81=AE=E3=82=82=E3=81=AE?= =?UTF-8?q?=E3=81=AB=E7=B5=B1=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbharu/owl/broker/service/RetryPolicy.kt | 31 ------------------- .../owl/broker/service/RetryPolicyFactory.kt | 5 ++- .../common/retry/ExponentialRetryPolicy.kt | 11 +++++++ .../usbharu/owl/common/retry/RetryPolicy.kt | 3 ++ 4 files changed, 18 insertions(+), 32 deletions(-) delete mode 100644 broker/src/main/kotlin/dev/usbharu/owl/broker/service/RetryPolicy.kt create mode 100644 common/src/main/kotlin/dev/usbharu/owl/common/retry/ExponentialRetryPolicy.kt diff --git a/broker/src/main/kotlin/dev/usbharu/owl/broker/service/RetryPolicy.kt b/broker/src/main/kotlin/dev/usbharu/owl/broker/service/RetryPolicy.kt deleted file mode 100644 index fb4c667..0000000 --- a/broker/src/main/kotlin/dev/usbharu/owl/broker/service/RetryPolicy.kt +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2024 usbharu - * - * 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 - * - * http://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. - */ - -package dev.usbharu.owl.broker.service - -import java.time.Instant -import kotlin.math.pow -import kotlin.math.roundToLong - -interface RetryPolicy { - fun nextRetry(now: Instant, attempt: Int): Instant -} - -class ExponentialRetryPolicy(private val firstRetrySeconds: Int = 30) : RetryPolicy { - override fun nextRetry(now: Instant, attempt: Int): Instant = - now.plusSeconds(firstRetrySeconds.toDouble().pow(attempt + 1.0).roundToLong()) - -} \ No newline at end of file diff --git a/broker/src/main/kotlin/dev/usbharu/owl/broker/service/RetryPolicyFactory.kt b/broker/src/main/kotlin/dev/usbharu/owl/broker/service/RetryPolicyFactory.kt index 86ad27b..3b4d851 100644 --- a/broker/src/main/kotlin/dev/usbharu/owl/broker/service/RetryPolicyFactory.kt +++ b/broker/src/main/kotlin/dev/usbharu/owl/broker/service/RetryPolicyFactory.kt @@ -16,8 +16,11 @@ package dev.usbharu.owl.broker.service +import dev.usbharu.owl.common.retry.ExponentialRetryPolicy +import dev.usbharu.owl.common.retry.RetryPolicy + interface RetryPolicyFactory { - fun factory(name:String):RetryPolicy + fun factory(name: String): RetryPolicy } class DefaultRetryPolicyFactory(private val map: Map) : RetryPolicyFactory { diff --git a/common/src/main/kotlin/dev/usbharu/owl/common/retry/ExponentialRetryPolicy.kt b/common/src/main/kotlin/dev/usbharu/owl/common/retry/ExponentialRetryPolicy.kt new file mode 100644 index 0000000..60180dd --- /dev/null +++ b/common/src/main/kotlin/dev/usbharu/owl/common/retry/ExponentialRetryPolicy.kt @@ -0,0 +1,11 @@ +package dev.usbharu.owl.common.retry + +import java.time.Instant +import kotlin.math.pow +import kotlin.math.roundToLong + +class ExponentialRetryPolicy(private val firstRetrySeconds: Int = 30) : RetryPolicy { + override fun nextRetry(now: Instant, attempt: Int): Instant = + now.plusSeconds(firstRetrySeconds.toDouble().pow(attempt + 1.0).roundToLong()) + +} \ No newline at end of file diff --git a/common/src/main/kotlin/dev/usbharu/owl/common/retry/RetryPolicy.kt b/common/src/main/kotlin/dev/usbharu/owl/common/retry/RetryPolicy.kt index 9042917..04a73a0 100644 --- a/common/src/main/kotlin/dev/usbharu/owl/common/retry/RetryPolicy.kt +++ b/common/src/main/kotlin/dev/usbharu/owl/common/retry/RetryPolicy.kt @@ -16,5 +16,8 @@ package dev.usbharu.owl.common.retry +import java.time.Instant + interface RetryPolicy { + fun nextRetry(now: Instant, attempt: Int): Instant } \ No newline at end of file