Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f9b4f21
Add named backoffs built from settings
reactivestudio Nov 8, 2026
fd6d218
Merge branch 'master' of https://github.com/VLprojects/semantic into …
reactivestudio Nov 8, 2026
c4d7f67
Name the backoff classes after what they are
reactivestudio Nov 8, 2026
8b3a208
Align backoff properties with Spring Boot conventions
reactivestudio Nov 9, 2026
f899dea
Fill the backoff registry through registrars
reactivestudio Nov 9, 2026
70d43ea
Fill the test registry without shadowing its default
reactivestudio Nov 9, 2026
c90e9a5
Fail on a backoff bean that cannot be created yet
reactivestudio Nov 9, 2026
5806f23
Map backoff properties and drop registrars
reactivestudio Nov 9, 2026
4281162
Reject every invalid default backoff field
reactivestudio Nov 9, 2026
57dd555
Fill the backoff registry through a standard customizer
reactivestudio Nov 9, 2026
0cc5831
Fill the backoff registry before other customizers
reactivestudio Nov 9, 2026
1dd6e84
Simplify the backoff factory and move the default name
reactivestudio Nov 9, 2026
63b7959
Keep only the backoff model and factory here
reactivestudio Nov 9, 2026
de18f98
Cover every field a fixed backoff rejects
reactivestudio Nov 9, 2026
4d81d86
Make backoff algorithms extensible through factory beans
reactivestudio Nov 9, 2026
5062dfd
Fail on duplicate backoff types and keep every form of an arg
reactivestudio Nov 9, 2026
874c325
Name the arg in a backoff binding failure
reactivestudio Nov 9, 2026
c945671
Register backoffs from settings through a properties registrar
reactivestudio Nov 10, 2026
0ec8822
Translate a backoff failure where it is registered
reactivestudio Nov 10, 2026
2bfcd12
Pick a backoff type by the key of its settings
reactivestudio Nov 10, 2026
15e3ec1
Read backoff settings by relaxed names from the property sources
reactivestudio Nov 10, 2026
97a7426
Keep the declared backoff name and reject values on blocks
reactivestudio Nov 10, 2026
dc93840
Reject a value on the backoff instances block
reactivestudio Nov 10, 2026
abb221a
Take the backoff type from the factory bean name and the config class…
reactivestudio Nov 10, 2026
7b07ac9
Resolve the backoff config class behind proxies and hint at naming th…
reactivestudio Nov 10, 2026
0edf966
Bind backoff settings through properties and keep the jitter in the e…
reactivestudio Nov 10, 2026
a10a7a2
Prefix the backoff settings keys with the block name and narrow the f…
reactivestudio Nov 10, 2026
7e74e6f
Pass the factory failure as the cause of the invalid backoff setting
reactivestudio Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Name the backoff classes after what they are
  • Loading branch information
reactivestudio committed Nov 8, 2026
commit c4d7f6755c9f012c91881902a35427c391f72831

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pro.vlprojects.semantic.resilience.backoff.spring.boot

import java.time.Duration

public class BackoffSettings(
public class BackoffInstanceProperties(
public val type: BackoffType? = null,
public val delay: Duration? = null,
public val initialDelay: Duration? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "semantic.resilience.backoff", ignoreUnknownFields = false)
public class BackoffProperties(
public val default: BackoffSettings = BackoffSettings(),
public val instances: Map<String, BackoffSettings> = emptyMap(),
public val default: BackoffInstanceProperties = BackoffInstanceProperties(),
public val instances: Map<String, BackoffInstanceProperties> = emptyMap(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package pro.vlprojects.semantic.resilience.backoff.spring.boot

import pro.vlprojects.semantic.resilience.backoff.Backoff

public class Backoffs(
public class BackoffRegistry(
public val default: Backoff,
private val named: Map<String, Backoff>,
) {

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

public fun getOrDefault(name: String): Backoff = named[name] ?: default
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@ package pro.vlprojects.semantic.resilience.backoff.spring.boot

import pro.vlprojects.semantic.resilience.backoff.Backoff

internal class BackoffsFactory(private val properties: BackoffProperties) {
internal class BackoffRegistryAssembler(private val properties: BackoffProperties) {

private val defaults = ResolvedBackoffSettings().overrideWith(properties.default)
private val defaults = ResolvedBackoffProperties().overrideWith(properties.default)

fun create(beans: Map<String, Backoff>) = properties.instances
fun assemble(beans: Map<String, Backoff>) = properties.instances
.also(::checkDefaultIsNotAnInstance)
.minus(beans.keys)
.mapValues(::createBackoff)
.plus(beans)
.let(::assembleBackoffs)
.let(::createRegistry)

private fun checkDefaultIsNotAnInstance(instances: Map<String, BackoffSettings>) = require(
private fun checkDefaultIsNotAnInstance(instances: Map<String, BackoffInstanceProperties>) = require(
DEFAULT_NAME !in instances,
) {
"The backoff '$DEFAULT_NAME' is not an instance: set it in the '$DEFAULT_NAME' block"
}

private fun createBackoff(instance: Map.Entry<String, BackoffSettings>) = rethrowWithName(instance.key) {
private fun createBackoff(instance: Map.Entry<String, BackoffInstanceProperties>) = rethrowWithName(instance.key) {
defaults
.overrideWith(instance.value)
.also { resolved -> resolved.checkAllFieldsAreUsed(instance.value) }
.createBackoff()
}

private fun assembleBackoffs(named: Map<String, Backoff>): Backoffs {
private fun createRegistry(named: Map<String, Backoff>): BackoffRegistry {
val default = named[DEFAULT_NAME] ?: rethrowWithName(DEFAULT_NAME, defaults::createBackoff)
return Backoffs(default, named + (DEFAULT_NAME to default))

return BackoffRegistry(default, named + (DEFAULT_NAME to default))
}

private fun rethrowWithName(name: String, createBackoff: () -> Backoff) = try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import pro.vlprojects.semantic.resilience.backoff.JitteredBackoff
import pro.vlprojects.semantic.resilience.backoff.extension.jittered
import java.time.Duration

internal class ResolvedBackoffSettings(
internal class ResolvedBackoffProperties(
private val type: BackoffType = BackoffType.EXPONENTIAL,
private val delay: Duration = Duration.ofSeconds(1),
private val initialDelay: Duration = Duration.ofSeconds(1),
Expand All @@ -15,16 +15,16 @@ internal class ResolvedBackoffSettings(
private val jitter: Double = JitteredBackoff.DEFAULT_JITTER,
) {

fun overrideWith(settings: BackoffSettings) = ResolvedBackoffSettings(
type = settings.type ?: type,
delay = settings.delay ?: delay,
initialDelay = settings.initialDelay ?: initialDelay,
delayLimit = settings.delayLimit ?: delayLimit,
multiplier = settings.multiplier ?: multiplier,
jitter = settings.jitter ?: jitter,
fun overrideWith(properties: BackoffInstanceProperties) = ResolvedBackoffProperties(
type = properties.type ?: type,
delay = properties.delay ?: delay,
initialDelay = properties.initialDelay ?: initialDelay,
delayLimit = properties.delayLimit ?: delayLimit,
multiplier = properties.multiplier ?: multiplier,
jitter = properties.jitter ?: jitter,
)

fun checkAllFieldsAreUsed(settings: BackoffSettings) = settings
fun checkAllFieldsAreUsed(properties: BackoffInstanceProperties) = properties
.findFieldsUnusedBy(type)
.takeIf(List<String>::isNotEmpty)
?.let { unused -> throw IllegalArgumentException("The type $type does not use $unused") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import pro.vlprojects.semantic.resilience.backoff.Backoff
import pro.vlprojects.semantic.resilience.backoff.FixedBackoff
import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffInstanceProperties
import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffProperties
import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffSettings
import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffRegistryAssembler
import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffType
import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffsFactory
import java.time.Duration

class BackoffsFactoryTests {
class BackoffRegistryAssemblerTests {

@Test
fun `without settings the default is exponential from a second to a minute`() {
val default = createBackoffs(default = BackoffSettings(jitter = 0.0)).default
val default = assembleRegistry(default = BackoffInstanceProperties(jitter = 0.0)).default

assertEquals(Duration.ofSeconds(1), default.calculate(1))
assertEquals(Duration.ofSeconds(2), default.calculate(2))
Expand All @@ -26,52 +26,55 @@ class BackoffsFactoryTests {

@Test
fun `without settings the pause is jittered by a half`() {
val delays = (1..200).map { createBackoffs().default.calculate(1) }
val delays = (1..200).map { assembleRegistry().default.calculate(1) }

assertTrue(delays.all { it in Duration.ofMillis(500)..Duration.ofSeconds(1) })
assertTrue(delays.distinct().size > 1)
}

@Test
fun `an instance takes another type`() {
val backoffs = createBackoffs(
default = BackoffSettings(jitter = 0.0),
instances = mapOf("payments" to BackoffSettings(type = BackoffType.FIXED, delay = Duration.ofSeconds(5))),
val registry = assembleRegistry(
default = BackoffInstanceProperties(jitter = 0.0),
instances = mapOf(
"payments" to BackoffInstanceProperties(type = BackoffType.FIXED, delay = Duration.ofSeconds(5)),
),
)

assertEquals(Duration.ofSeconds(5), backoffs["payments"].calculate(3))
assertEquals(Duration.ofSeconds(5), registry["payments"].calculate(3))
}

@Test
fun `the fields an instance leaves out come from the default`() {
val backoffs = createBackoffs(
default = BackoffSettings(multiplier = 3.0, jitter = 0.0),
instances = mapOf("orders" to BackoffSettings(initialDelay = Duration.ofMillis(200))),
val registry = assembleRegistry(
default = BackoffInstanceProperties(multiplier = 3.0, jitter = 0.0),
instances = mapOf("orders" to BackoffInstanceProperties(initialDelay = Duration.ofMillis(200))),
)

assertEquals(Duration.ofMillis(200), backoffs["orders"].calculate(1))
assertEquals(Duration.ofMillis(600), backoffs["orders"].calculate(2))
assertEquals(Duration.ofMillis(200), registry["orders"].calculate(1))
assertEquals(Duration.ofMillis(600), registry["orders"].calculate(2))
}

@Test
fun `an instance of the other type inherits the fields of that type from the default`() {
val backoffs = createBackoffs(
default = BackoffSettings(delay = Duration.ofSeconds(7), jitter = 0.0),
instances = mapOf("payments" to BackoffSettings(type = BackoffType.FIXED)),
val registry = assembleRegistry(
default = BackoffInstanceProperties(delay = Duration.ofSeconds(7), jitter = 0.0),
instances = mapOf("payments" to BackoffInstanceProperties(type = BackoffType.FIXED)),
)

assertEquals(Duration.ofSeconds(7), backoffs["payments"].calculate(1))
assertEquals(Duration.ofSeconds(7), registry["payments"].calculate(1))
}

@Test
fun `the jitter wraps a fixed backoff too`() {
val backoffs = createBackoffs(
val registry = assembleRegistry(
instances = mapOf(
"payments" to BackoffSettings(type = BackoffType.FIXED, delay = Duration.ofSeconds(5), jitter = 1.0),
"payments" to
BackoffInstanceProperties(type = BackoffType.FIXED, delay = Duration.ofSeconds(5), jitter = 1.0),
),
)

val delays = (1..200).map { backoffs["payments"].calculate(1) }
val delays = (1..200).map { registry["payments"].calculate(1) }

assertTrue(delays.distinct().size > 1)
assertTrue(delays.all { it <= Duration.ofSeconds(5) })
Expand All @@ -81,27 +84,27 @@ class BackoffsFactoryTests {
fun `a bean with the name of an instance replaces its settings`() {
val bean = FixedBackoff.ofSeconds(9)

val backoffs = createBackoffs(
instances = mapOf("payments" to BackoffSettings(jitter = 2.0)),
val registry = assembleRegistry(
instances = mapOf("payments" to BackoffInstanceProperties(jitter = 2.0)),
beans = mapOf("payments" to bean),
)

assertSame(bean, backoffs["payments"])
assertSame(bean, registry["payments"])
}

@Test
fun `a bean named default replaces the default settings`() {
val bean = FixedBackoff.ofSeconds(9)

val backoffs = createBackoffs(beans = mapOf("default" to bean))
val registry = assembleRegistry(beans = mapOf("default" to bean))

assertSame(bean, backoffs.default)
assertSame(bean, registry.default)
}

@Test
fun `a field the type of an instance does not use fails with the name of the instance`() {
val failure = assertThrows<IllegalArgumentException> {
createBackoffs(instances = mapOf("payments" to BackoffSettings(delay = Duration.ofSeconds(5))))
assembleRegistry(instances = mapOf("payments" to BackoffInstanceProperties(delay = Duration.ofSeconds(5))))
}

assertTrue("payments" in failure.message.orEmpty())
Expand All @@ -111,15 +114,15 @@ class BackoffsFactoryTests {
@Test
fun `the default block may hold the fields of both types`() {
val settings =
BackoffSettings(delay = Duration.ofSeconds(5), initialDelay = Duration.ofSeconds(2), jitter = 0.0)
BackoffInstanceProperties(delay = Duration.ofSeconds(5), initialDelay = Duration.ofSeconds(2), jitter = 0.0)

assertEquals(Duration.ofSeconds(2), createBackoffs(default = settings).default.calculate(1))
assertEquals(Duration.ofSeconds(2), assembleRegistry(default = settings).default.calculate(1))
}

@Test
fun `an invalid value fails with the name of the instance`() {
val failure = assertThrows<IllegalArgumentException> {
createBackoffs(instances = mapOf("orders" to BackoffSettings(jitter = 1.5)))
assembleRegistry(instances = mapOf("orders" to BackoffInstanceProperties(jitter = 1.5)))
}

assertTrue("orders" in failure.message.orEmpty())
Expand All @@ -129,31 +132,31 @@ class BackoffsFactoryTests {
@Test
fun `an invalid default fails with its name`() {
val failure = assertThrows<IllegalArgumentException> {
createBackoffs(default = BackoffSettings(multiplier = 0.5))
assembleRegistry(default = BackoffInstanceProperties(multiplier = 0.5))
}

assertTrue("default" in failure.message.orEmpty())
}

@Test
fun `the default is found by its name`() {
val backoffs = createBackoffs()
val registry = assembleRegistry()

assertSame(backoffs.default, backoffs["default"])
assertSame(registry.default, registry["default"])
}

@Test
fun `an instance named default fails`() {
val failure = assertThrows<IllegalArgumentException> {
createBackoffs(instances = mapOf("default" to BackoffSettings()))
assembleRegistry(instances = mapOf("default" to BackoffInstanceProperties()))
}

assertTrue("default" in failure.message.orEmpty())
}

private fun createBackoffs(
default: BackoffSettings = BackoffSettings(),
instances: Map<String, BackoffSettings> = emptyMap(),
private fun assembleRegistry(
default: BackoffInstanceProperties = BackoffInstanceProperties(),
instances: Map<String, BackoffInstanceProperties> = emptyMap(),
beans: Map<String, Backoff> = emptyMap(),
) = BackoffsFactory(BackoffProperties(default, instances)).create(beans)
) = BackoffRegistryAssembler(BackoffProperties(default, instances)).assemble(beans)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import pro.vlprojects.semantic.resilience.backoff.FixedBackoff
import pro.vlprojects.semantic.resilience.backoff.spring.boot.Backoffs
import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffRegistry

class BackoffsTests {
class BackoffRegistryTests {

private val default = FixedBackoff.ofSeconds(1)
private val payments = FixedBackoff.ofSeconds(5)
private val backoffs = Backoffs(default, mapOf("payments" to payments))
private val registry = BackoffRegistry(default, mapOf("payments" to payments))

@Test
fun `a backoff is found by its name`() {
assertSame(payments, backoffs["payments"])
assertSame(payments, backoffs.getOrDefault("payments"))
assertSame(payments, registry["payments"])
assertSame(payments, registry.getOrDefault("payments"))
}

@Test
fun `an unknown name fails and lists the known ones`() {
val failure = assertThrows<NoSuchElementException> { backoffs["orders"] }
val failure = assertThrows<NoSuchElementException> { registry["orders"] }

assertTrue("orders" in failure.message.orEmpty())
assertTrue("payments" in failure.message.orEmpty())
}

@Test
fun `an unknown name can take the default instead`() {
assertSame(default, backoffs.getOrDefault("orders"))
assertSame(default, registry.getOrDefault("orders"))
}
}
Loading