Skip to content

Commit 86ceed4

Browse files
Align backoff properties with Spring Boot conventions
1 parent c4d7f67 commit 86ceed4

12 files changed

Lines changed: 174 additions & 164 deletions

File tree

‎architecture/pattern/resilience/backoff/spring-boot/autoconfigure/api/semantic-resilience-backoff-spring-boot.api‎

Lines changed: 35 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎architecture/pattern/resilience/backoff/spring-boot/autoconfigure/src/main/kotlin/pro/vlprojects/semantic/resilience/backoff/spring/boot/BackoffProperties.kt‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎architecture/pattern/resilience/backoff/spring-boot/autoconfigure/src/main/kotlin/pro/vlprojects/semantic/resilience/backoff/spring/boot/BackoffRegistry.kt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import pro.vlprojects.semantic.resilience.backoff.Backoff
44

55
public class BackoffRegistry(
66
public val default: Backoff,
7-
private val named: Map<String, Backoff>,
7+
private val backoffs: Map<String, Backoff>,
88
) {
99

10-
public operator fun get(name: String): Backoff = named[name]
11-
?: throw NoSuchElementException("No backoff is named '$name'. Named registry: ${named.keys}")
10+
public operator fun get(name: String): Backoff = backoffs[name]
11+
?: throw NoSuchElementException("No backoff is named '$name'. Named backoffs: ${backoffs.keys}")
1212

13-
public fun getOrDefault(name: String): Backoff = named[name] ?: default
13+
public fun getOrDefault(name: String): Backoff = backoffs[name] ?: default
1414
}

‎architecture/pattern/resilience/backoff/spring-boot/autoconfigure/src/main/kotlin/pro/vlprojects/semantic/resilience/backoff/spring/boot/BackoffRegistryAssembler.kt‎

Lines changed: 0 additions & 45 deletions
This file was deleted.

‎architecture/pattern/resilience/backoff/spring-boot/autoconfigure/src/main/kotlin/pro/vlprojects/semantic/resilience/backoff/spring/boot/BackoffType.kt‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎architecture/pattern/resilience/backoff/spring-boot/autoconfigure/src/main/kotlin/pro/vlprojects/semantic/resilience/backoff/spring/boot/ResolvedBackoffProperties.kt‎

Lines changed: 0 additions & 40 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package pro.vlprojects.semantic.resilience.backoff.spring.boot.config
2+
3+
import pro.vlprojects.semantic.resilience.backoff.Backoff
4+
import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffRegistry
5+
import pro.vlprojects.semantic.resilience.backoff.spring.boot.config.properties.BackoffConfigurationProperties
6+
import pro.vlprojects.semantic.resilience.backoff.spring.boot.config.properties.BackoffProperties
7+
8+
internal class BackoffPropertiesMapper(private val properties: BackoffConfigurationProperties) {
9+
10+
fun mapToRegistry(beans: Map<String, Backoff>) = properties.instances
11+
.also(::checkDefaultIsNotAnInstance)
12+
.minus(beans.keys)
13+
.mapValues(::createBackoff)
14+
.plus(beans)
15+
.let(::createRegistry)
16+
17+
private fun checkDefaultIsNotAnInstance(instances: Map<String, BackoffProperties>) = require(
18+
DEFAULT_NAME !in instances,
19+
) {
20+
"The backoff '$DEFAULT_NAME' is not an instance: set it in the '$DEFAULT_NAME' block"
21+
}
22+
23+
private fun createBackoff(instance: Map.Entry<String, BackoffProperties>) = rethrowWithName(instance.key) {
24+
properties.default.createBackoff(instance.value)
25+
}
26+
27+
private fun createRegistry(backoffs: Map<String, Backoff>): BackoffRegistry {
28+
val default = backoffs[DEFAULT_NAME] ?: rethrowWithName(DEFAULT_NAME, properties.default::createBackoff)
29+
30+
return BackoffRegistry(default, backoffs + (DEFAULT_NAME to default))
31+
}
32+
33+
private fun rethrowWithName(name: String, createBackoff: () -> Backoff) = try {
34+
createBackoff()
35+
} catch (failure: IllegalArgumentException) {
36+
throw IllegalArgumentException("The backoff '$name' is misconfigured: ${failure.message}", failure)
37+
}
38+
39+
private companion object {
40+
41+
const val DEFAULT_NAME = "default"
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pro.vlprojects.semantic.resilience.backoff.spring.boot.config.properties
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties
4+
5+
@ConfigurationProperties(prefix = "semantic.resilience.backoff", ignoreUnknownFields = false)
6+
public class BackoffConfigurationProperties(
7+
public val default: DefaultBackoffProperties = DefaultBackoffProperties(),
8+
public val instances: Map<String, BackoffProperties> = emptyMap(),
9+
)

architecture/pattern/resilience/backoff/spring-boot/autoconfigure/src/main/kotlin/pro/vlprojects/semantic/resilience/backoff/spring/boot/BackoffInstanceProperties.kt renamed to architecture/pattern/resilience/backoff/spring-boot/autoconfigure/src/main/kotlin/pro/vlprojects/semantic/resilience/backoff/spring/boot/config/properties/BackoffProperties.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package pro.vlprojects.semantic.resilience.backoff.spring.boot
1+
package pro.vlprojects.semantic.resilience.backoff.spring.boot.config.properties
22

33
import java.time.Duration
44

5-
public class BackoffInstanceProperties(
5+
public class BackoffProperties(
66
public val type: BackoffType? = null,
77
public val delay: Duration? = null,
88
public val initialDelay: Duration? = null,
@@ -11,7 +11,12 @@ public class BackoffInstanceProperties(
1111
public val jitter: Double? = null,
1212
) {
1313

14-
internal fun findFieldsUnusedBy(type: BackoffType) = when (type) {
14+
internal fun checkAllFieldsAreUsedBy(type: BackoffType) = this
15+
.findFieldsUnusedBy(type)
16+
.takeIf(List<String>::isNotEmpty)
17+
?.let { unused -> throw IllegalArgumentException("The type $type does not use $unused") }
18+
19+
private fun findFieldsUnusedBy(type: BackoffType) = when (type) {
1520
BackoffType.FIXED -> listOfNotNull(
1621
initialDelay?.let { "initial-delay" },
1722
delayLimit?.let { "delay-limit" },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pro.vlprojects.semantic.resilience.backoff.spring.boot.config.properties
2+
3+
public enum class BackoffType {
4+
5+
FIXED,
6+
EXPONENTIAL,
7+
}

0 commit comments

Comments
 (0)