Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
bdd6d9d
Configure backoffs from application settings
reactivestudio Nov 8, 2026
b4a3312
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 8, 2026
3362251
Depend on the Spring module of backoff
reactivestudio Nov 8, 2026
db030e6
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 8, 2026
18ac90d
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
aad15d7
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
8dc40fe
Register backoffs through registrars in the auto-configuration
reactivestudio Nov 9, 2026
1473225
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
c6172a6
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
1bdab69
Register backoff beans after every registrar
reactivestudio Nov 9, 2026
9885960
Customize the backoff registry through beans
reactivestudio Nov 9, 2026
fd5ced3
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
1ca8bce
Cover backoff beans replacing the settings
reactivestudio Nov 9, 2026
d4fee59
Apply settings and beans through the standard customizer
reactivestudio Nov 9, 2026
59a8b4d
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
e6faefb
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
2b662e5
Use the default backoff name in the auto-configuration tests
reactivestudio Nov 9, 2026
520c952
Fill the backoff registry in the auto-configuration
reactivestudio Nov 9, 2026
8d74ef0
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
93dd9a4
Name the validation of the default backoff
reactivestudio Nov 9, 2026
6ad4632
Pick backoff algorithms from factory beans in the auto-configuration
reactivestudio Nov 9, 2026
740c103
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
251d17e
Say that the name of the default backoff is reserved
reactivestudio Nov 9, 2026
c6c4e0b
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 9, 2026
e2b5d5e
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 10, 2026
4817c16
Fill the backoff registry through the properties registrar
reactivestudio Nov 10, 2026
9c24cbd
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 10, 2026
8e92384
Bind backoff settings by the key of their type in the auto-configuration
reactivestudio Nov 10, 2026
421e221
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 10, 2026
25b077d
Put the misspelled field under its type in the auto-configuration test
reactivestudio Nov 10, 2026
3ef32e7
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 10, 2026
1dd0b7f
Describe relaxed backoff names in the settings guide
reactivestudio Nov 10, 2026
c91d1cc
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 10, 2026
78d0fb9
Wrap the start failures paragraph of the settings guide
reactivestudio Nov 10, 2026
935a788
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 10, 2026
238d2c1
Name the backoff factory beans of the auto-configuration by their type
reactivestudio Nov 10, 2026
bd771ef
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 10, 2026
2237a7a
Bind backoff settings through properties in the auto-configuration
reactivestudio Nov 10, 2026
0f3999c
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
reactivestudio Nov 10, 2026
1ba224f
Merge branch 'resilience-backoff-spring-boot' into resilience-backoff…
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
Pick backoff algorithms from factory beans in the auto-configuration
  • Loading branch information
reactivestudio committed Nov 9, 2026
commit 6ad46328879795259aff01b90b86cf846f686f0c
41 changes: 24 additions & 17 deletions architecture/pattern/resilience/backoff/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ to a custom `BackOff`, so set `maxAttempts` there and in the error handler of Sp
semantic.resilience.backoff:
default:
type: exponential
initial-delay: 1s
delay-limit: 1m
multiplier: 2
args:
initial-delay: 1s
delay-limit: 1m
multiplier: 2
jitter: 0.5
instances:
payments:
type: fixed
delay: 5s
args:
delay: 5s
jitter: 0.3
orders:
initial-delay: 200ms
args:
initial-delay: 200ms
```

```kotlin
Expand All @@ -69,15 +72,19 @@ registry.getOrDefault("inventory") // the default block for a name with no setti
registry.default
```

With nothing set, the `default` block has the values shown above except `jitter`, and `delay` is `1s`. Jitter
applies only where `jitter` is set; `jitter: 0` on an instance turns off the jitter of `default`. An instance
takes the fields it leaves out from `default`, including `type`. Any `Backoff` bean is available by its bean
name and replaces the settings of an instance with that name; a bean named `default` replaces the default
block. To change the registry from code, declare a `BackoffRegistryCustomizer` bean: it gets the filled
registry, settings and beans alike, and may go through `names`, `register` and `replace`. Customizers run in
`@Order`.

Wrong settings stop the application at start, with the name of the instance, even when a bean replaces it:
an unknown field or type, a value the `Backoff` being built refuses, a field of an instance that its type does
not use, such as `delay` on an exponential one, and an instance named `default`. The `default` block may hold
the fields of both types, since instances of either inherit them.
`type` picks a `BackoffFactory` bean and `args` bind to its config class. `fixed` takes `delay` (`1s`) and
`exponential` takes `initial-delay` (`1s`), `delay-limit` (`1m`) and `multiplier` (`2`). To add an algorithm,
declare a `BackoffFactory` bean with its own `type`.

With nothing set, `type` is `exponential` and there is no jitter. An instance takes `type` and `jitter` from
`default` when it leaves them out, and the `args` of `default` when its type is the same as the type of
`default`. `jitter: 0` on an instance turns off the jitter of `default`.

Any `Backoff` bean is available by its bean name and replaces the settings of an instance with that name; a bean
named `default` replaces the default block. To change the registry from code, declare a
`BackoffRegistryCustomizer` bean: it gets the filled registry, settings and beans alike, and may go through
`names`, `register` and `replace`. Customizers run in `@Order`.

Wrong settings stop the application at start, with the name of the instance, even when a bean replaces it: an
unknown `type` or arg, a value that cannot be converted or that the `Backoff` being built refuses, and an
instance named `default`. An unknown field outside `args` stops it as well.

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 @@ -10,22 +10,33 @@ import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffRegistry
import pro.vlprojects.semantic.resilience.backoff.spring.boot.DEFAULT_BACKOFF_NAME
import pro.vlprojects.semantic.resilience.backoff.spring.boot.config.properties.BackoffConfigurationProperties
import pro.vlprojects.semantic.resilience.backoff.spring.boot.config.properties.BackoffProperties
import pro.vlprojects.semantic.resilience.backoff.spring.boot.config.properties.BackoffType
import pro.vlprojects.semantic.resilience.backoff.spring.boot.factory.BackoffFactory
import pro.vlprojects.semantic.resilience.backoff.spring.boot.factory.ExponentialBackoffFactory
import pro.vlprojects.semantic.resilience.backoff.spring.boot.factory.FixedBackoffFactory

@AutoConfiguration
@EnableConfigurationProperties(BackoffConfigurationProperties::class)
public class BackoffAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public fun fixedBackoffFactory(): FixedBackoffFactory = FixedBackoffFactory()

@Bean
@ConditionalOnMissingBean
public fun exponentialBackoffFactory(): ExponentialBackoffFactory = ExponentialBackoffFactory()

@Bean
@ConditionalOnMissingBean
public fun backoffRegistry(
properties: BackoffConfigurationProperties,
factories: List<BackoffFactory<*>>,
backoffs: Map<String, Backoff>,
customizers: ObjectProvider<BackoffRegistryCustomizer>,
): BackoffRegistry {
val registry = BackoffRegistry()

initBackoffRegistry(registry, properties)
initBackoffRegistry(registry, properties, factories)
backoffs.forEach { (name, backoff) -> registry.replace(name, backoff) }
customizers
.orderedStream()
Expand All @@ -34,29 +45,23 @@ public class BackoffAutoConfiguration {
return registry
}

private fun initBackoffRegistry(registry: BackoffRegistry, properties: BackoffConfigurationProperties) {
val factory = BackoffFactory(properties.default)
private fun initBackoffRegistry(
registry: BackoffRegistry,
properties: BackoffConfigurationProperties,
factories: List<BackoffFactory<*>>,
) {
val service = BackoffConfigurationService(properties.default, factories)

validateDefault(factory)
properties.instances.forEach { (name, instance) -> registerBackoff(registry, factory, name, instance) }
registerBackoff(registry, factory, DEFAULT_BACKOFF_NAME, BackoffProperties())
properties.instances.forEach { (name, instance) -> registerBackoff(registry, service, name, instance) }
registerBackoff(registry, service, DEFAULT_BACKOFF_NAME, BackoffProperties())
}

private fun validateDefault(factory: BackoffFactory) = BackoffType.entries
.map { type -> BackoffProperties(type = type) }
.forEach { instance -> createBackoff(factory, DEFAULT_BACKOFF_NAME, instance) }

private fun registerBackoff(
registry: BackoffRegistry,
factory: BackoffFactory,
service: BackoffConfigurationService,
name: String,
properties: BackoffProperties,
) = createBackoff(factory, name, properties)
) = service
.create(name, properties)
.let { backoff -> registry.register(name, backoff) }

private fun createBackoff(factory: BackoffFactory, name: String, properties: BackoffProperties) = try {
factory.create(properties)
} catch (failure: IllegalArgumentException) {
throw IllegalArgumentException("The backoff '$name' is misconfigured: ${failure.message}", failure)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import pro.vlprojects.semantic.resilience.backoff.spring.boot.BackoffRegistry
import pro.vlprojects.semantic.resilience.backoff.spring.boot.DEFAULT_BACKOFF_NAME
import pro.vlprojects.semantic.resilience.backoff.spring.boot.config.BackoffAutoConfiguration
import pro.vlprojects.semantic.resilience.backoff.spring.boot.config.BackoffRegistryCustomizer
import pro.vlprojects.semantic.resilience.backoff.spring.boot.factory.BackoffFactory
import java.time.Duration

class BackoffAutoConfigurationTests {
Expand All @@ -44,12 +45,12 @@ class BackoffAutoConfigurationTests {
runner
.withPropertyValues(
"$PREFIX.default.type=exponential",
"$PREFIX.default.initial-delay=2s",
"$PREFIX.default.delay-limit=1m",
"$PREFIX.default.multiplier=3",
"$PREFIX.default.args.initial-delay=2s",
"$PREFIX.default.args.delay-limit=1m",
"$PREFIX.default.args.multiplier=3",
"$PREFIX.instances.payments.type=fixed",
"$PREFIX.instances.payments.delay=5s",
"$PREFIX.instances.orders.initial-delay=200ms",
"$PREFIX.instances.payments.args.delay=5s",
"$PREFIX.instances.orders.args.initial-delay=200ms",
).run { context ->
val registry = context.getRegistry()

Expand All @@ -63,7 +64,7 @@ class BackoffAutoConfigurationTests {
fun `a backoff bean is more important than the settings`() {
runner
.withUserConfiguration(PaymentsBackoffConfiguration::class.java)
.withPropertyValues("$PREFIX.instances.payments.initial-delay=1ms")
.withPropertyValues("$PREFIX.instances.payments.args.initial-delay=1ms")
.run { context ->
assertSame(context.getBean("payments"), context.getRegistry()["payments"])
}
Expand Down Expand Up @@ -111,7 +112,7 @@ class BackoffAutoConfigurationTests {
PaymentsBackoffConfiguration::class.java,
ReplacingCustomizerConfiguration::class.java,
)
.withPropertyValues("$PREFIX.instances.orders.initial-delay=1ms")
.withPropertyValues("$PREFIX.instances.orders.args.initial-delay=1ms")
.run { context ->
val registry = context.getRegistry()

Expand All @@ -136,6 +137,18 @@ class BackoffAutoConfigurationTests {
.run { context -> assertNotNull(context.startupFailure) }
}

@Test
fun `a factory bean adds a type`() {
runner
.withUserConfiguration(OnceBackoffFactoryConfiguration::class.java)
.withPropertyValues(
"$PREFIX.instances.orders.type=once",
"$PREFIX.instances.orders.args.delay=7s",
).run { context ->
assertEquals(Duration.ofSeconds(7), context.getRegistry()["orders"].calculate(1))
}
}

@Test
fun `an unknown field stops the start`() {
runner
Expand All @@ -153,21 +166,14 @@ class BackoffAutoConfigurationTests {
@Test
fun `an invalid default stops the start and names it`() {
runner
.withPropertyValues("$PREFIX.default.multiplier=0.5")
.run { context -> assertTrue("'default'" in context.readStartupFailureMessages()) }
}

@Test
fun `an invalid default field of the type in no use stops the start`() {
runner
.withPropertyValues("$PREFIX.default.delay=-1s")
.withPropertyValues("$PREFIX.default.args.multiplier=0.5")
.run { context -> assertTrue("'default'" in context.readStartupFailureMessages()) }
}

@Test
fun `a field the type of an instance does not use stops the start and names the instance`() {
fun `an arg the type of an instance does not have stops the start and names the instance`() {
runner
.withPropertyValues("$PREFIX.instances.payments.delay=5s")
.withPropertyValues("$PREFIX.instances.payments.args.delay=5s")
.run { context -> assertTrue("'payments'" in context.readStartupFailureMessages()) }
}

Expand Down Expand Up @@ -253,6 +259,24 @@ class BackoffAutoConfigurationTests {
fun payments(registry: BackoffRegistry) = registry.default
}

@Configuration(proxyBeanMethods = false)
class OnceBackoffFactoryConfiguration {

@Bean
fun onceBackoffFactory() = OnceBackoffFactory()
}

class OnceBackoffFactory : BackoffFactory<OnceBackoffFactory.Config> {

override val type = "once"

override val configClass = Config::class.java

override fun create(config: Config) = FixedBackoff(config.delay)

data class Config(val delay: Duration = Duration.ofSeconds(1))
}

private companion object {

const val PREFIX = "semantic.resilience.backoff"
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.