Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable groupExecutorService #180 #181

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class Resilience4JCircuitBreakerFactory extends

private ExecutorService executorService = Executors.newCachedThreadPool();

private Function<String, ExecutorService> groupExecutorServiceFactory = group -> Executors.newCachedThreadPool();

private ConcurrentHashMap<String, ExecutorService> executorServices = new ConcurrentHashMap<>();

private Map<String, Customizer<CircuitBreaker>> circuitBreakerCustomizers = new HashMap<>();
Expand Down Expand Up @@ -110,6 +112,14 @@ public void configureExecutorService(ExecutorService executorService) {
this.executorService = executorService;
}

/**
* configure GroupExecutorService.
* @param groupFactory GroupExecutorService Factory
*/
public void configureGroupExecutorService(Function<String, ExecutorService> groupFactory) {
this.groupExecutorServiceFactory = groupFactory;
}

@Override
public org.springframework.cloud.client.circuitbreaker.CircuitBreaker create(String id) {
Assert.hasText(id, "A CircuitBreaker must have an id.");
Expand All @@ -122,7 +132,7 @@ public org.springframework.cloud.client.circuitbreaker.CircuitBreaker create(Str
Assert.hasText(id, "A CircuitBreaker must have an id.");
Assert.hasText(groupName, "A CircuitBreaker must have a group name.");
final ExecutorService groupExecutorService = executorServices.computeIfAbsent(groupName,
group -> Executors.newCachedThreadPool());
groupExecutorServiceFactory);
Resilience4JCircuitBreaker resilience4JCircuitBreaker = create(id, groupName, groupExecutorService);
return tryObservedCircuitBreaker(resilience4JCircuitBreaker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package org.springframework.cloud.circuitbreaker.resilience4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.github.resilience4j.bulkhead.ThreadPoolBulkheadRegistry;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.timelimiter.TimeLimiterRegistry;
import io.micrometer.core.instrument.util.NamedThreadFactory;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -230,4 +233,49 @@ public void runWithDisabledTimeLimiter() {
})).isEqualTo("foobar");
}

/**
* Run the test with grouping and specify thread pool.
*/
@Test
public void runWithCustomGroupThreadPool() {
Resilience4JCircuitBreakerFactory factory = new Resilience4JCircuitBreakerFactory(
CircuitBreakerRegistry.ofDefaults(), TimeLimiterRegistry.ofDefaults(), null);
String groupName = "groupFoo";

// configure GroupExecutorService
factory.configureGroupExecutorService(group -> new ContextThreadPoolExecutor(groupName));

CircuitBreaker cb = factory.create("foo", groupName);
assertThat(cb.run(() -> Thread.currentThread().getName())).startsWith(groupName);
}

/**
* Run tests without grouping and specify thread pool.
*/
@Test
public void runWithCustomNormalThreadPool() {
Resilience4JCircuitBreakerFactory factory = new Resilience4JCircuitBreakerFactory(
CircuitBreakerRegistry.ofDefaults(), TimeLimiterRegistry.ofDefaults(), null);
String threadPoolName = "demo-";

// configure ExecutorService
factory.configureExecutorService(new ContextThreadPoolExecutor(threadPoolName));

CircuitBreaker cb = factory.create("foo");
assertThat(cb.run(() -> Thread.currentThread().getName())).startsWith(threadPoolName);
}

static class ContextThreadPoolExecutor extends ThreadPoolExecutor {

/**
* example ContextThreadPoolExecutor
* @param threadPoolName fixed threadPoolName
*/
ContextThreadPoolExecutor(String threadPoolName) {
super(2, 5, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1024));
this.setThreadFactory(new NamedThreadFactory(threadPoolName));
}

}

}