Add once method for Customizer of circuit breaker (#626)

* Add Customizer.once

* Make the logic of Customer.once atomic

* Add document on how to use once method
This commit is contained in:
Toshiaki Maki
2020-01-16 00:19:03 +09:00
committed by Ryan Baxter
parent 61ba39ee11
commit d7767a058a
3 changed files with 84 additions and 0 deletions

View File

@@ -89,3 +89,19 @@ the following documentation:
* link:../../../../spring-cloud-circuitbreaker/current/reference/html/spring-cloud-circuitbreaker.html#configuring-resilience4j-circuit-breakers[Resilience4J]
* link:https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc/circuitbreaker-sentinel.adoc#circuit-breaker-spring-cloud-circuit-breaker-with-sentinel--configuring-sentinel-circuit-breakers[Sentinal]
* link:../../../../spring-cloud-circuitbreaker/current/reference/html/spring-cloud-circuitbreaker.html#configuring-spring-retry-circuit-breakers[Spring Retry]
Some `CircuitBreaker` implementations such as `Resilience4JCircuitBreaker` call `customize` method every time `CircuitBreaker#run` is called.
It can be inefficient. In that case, you can use `CircuitBreaker#once` method. It is useful where calling `customize` many times doesn't make sense,
for example, in case of https://resilience4j.readme.io/docs/circuitbreaker#section-consume-emitted-circuitbreakerevents[consuming Resilience4j's events].
The following example shows the way for each `io.github.resilience4j.circuitbreaker.CircuitBreaker` to consume events.
====
[source,java]
----
Customizer.once(circuitBreaker -> {
circuitBreaker.getEventPublisher()
.onStateTransition(event -> log.info("{}: {}", event.getCircuitBreakerName(), event.getStateTransition()));
}, CircuitBreaker::getName)
----
====

View File

@@ -16,13 +16,39 @@
package org.springframework.cloud.client.circuitbreaker;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
/**
* Customizes the parameterized class.
*
* @author Ryan Baxter
* @author Toshiaki Maki
*/
public interface Customizer<TOCUSTOMIZE> {
void customize(TOCUSTOMIZE tocustomize);
/**
* Create a wrapped customizer that guarantees that the {@link #customize(Object)}
* method of the delegated <code>customizer</code> is called at most once per target.
* @param customizer a customizer to be delegated
* @param keyMapper a mapping function to produce the identifier of the target
* @param <T> the type of the target to customize
* @param <K> the type of the identifier of the target
* @return a wrapped customizer
*/
static <T, K> Customizer<T> once(Customizer<T> customizer,
Function<? super T, ? extends K> keyMapper) {
final ConcurrentMap<K, Boolean> customized = new ConcurrentHashMap<>();
return t -> {
final K key = keyMapper.apply(t);
customized.computeIfAbsent(key, k -> {
customizer.customize(t);
return true;
});
};
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.client.circuitbreaker;
import java.util.concurrent.atomic.AtomicInteger;
import org.assertj.core.api.Assertions;
import org.junit.Test;
public class CustomizerTests {
@Test
public void testCustomizedOnlyOnce() {
AtomicInteger counter = new AtomicInteger(0);
final Customizer<AtomicInteger> customizer = Customizer
.once(AtomicInteger::incrementAndGet, Object::hashCode);
customizer.customize(counter);
customizer.customize(counter);
customizer.customize(counter);
Assertions.assertThat(counter.get()).isEqualTo(1);
AtomicInteger anotherCounter = new AtomicInteger(0);
customizer.customize(anotherCounter);
customizer.customize(anotherCounter);
Assertions.assertThat(counter.get()).isEqualTo(1);
}
}