spring-cloud / spring-cloud-circuitbreaker

Spring Cloud Circuit Breaker API and Implementations
Apache License 2.0
336 stars 110 forks source link

Why use thread pool isolation as the default isolation method? #119

Closed suanyi001 closed 2 years ago

suanyi001 commented 3 years ago

Problem describe. I uesed spring-cloud-starter-circuitbreaker-resilience4j:2020.0.3 and resilience4j-bulkhead in my project dependencies, When I read the source code, I found the following code in org.springframework.cloud.circuitbreaker.resilience4j.Resilience4jBulkheadProvider:

   private <T> Supplier<CompletionStage<T>> decorateBulkhead(final String id, final Map<String, String> tags, final Supplier<T> supplier) {
        BulkheadConfiguration configuration = (BulkheadConfiguration)this.configurations.computeIfAbsent(id, this.defaultConfiguration);
        if (this.bulkheadRegistry.find(id).isPresent() && !this.threadPoolBulkheadRegistry.find(id).isPresent()) {
            Bulkhead bulkhead = this.bulkheadRegistry.bulkhead(id, configuration.getBulkheadConfig(), tags);
            CompletableFuture<T> asyncCall = CompletableFuture.supplyAsync(supplier);
            return Bulkhead.decorateCompletionStage(bulkhead, () -> {
                return asyncCall;
            });
        } else {
            ThreadPoolBulkhead threadPoolBulkhead = this.threadPoolBulkheadRegistry.bulkhead(id, configuration.getThreadPoolBulkheadConfig(), tags);
            return threadPoolBulkhead.decorateSupplier(supplier);
        }
    }

it makes Thread-pool isolation as the default isolation.And I did not find that semaphore isolation can be configured globally as the default isolation method. Can you provide a way like this.

ryanjbaxter commented 3 years ago

Originally we did not have support for bulkheads and this was added in this release. In order to not break people we kept the ThreadPool the default.

We can switch the default functionality in the main branch at this point.

I am also OK if you want to provide a configuration option to set a global bulkhead configuration.

Would you be interested in contributing that?