Closed Mistborn94 closed 7 months ago
@Mistborn94 a PR would be quite welcome!
Not su if it is related but currently, the ThreadPoolBulkhead is not auto closing resources.
Tested by using Spring Cloud Circuit Breaker in a batch job, the batch hangs and never finishes as the bulkhead threads are not closed. When configure to use semaphore works fine.
More info here: https://github.com/resilience4j/resilience4j/issues/1199
Pull request has been up for a while now https://github.com/spring-cloud/spring-cloud-circuitbreaker/pull/164
Describe the bug When using a timelimiter + semaphore bulkhead, if the timelimiter's time is exceeded, the underlying thread executing is not interrupted but the bulkhead permit is released. This means if the restricted section inside the bulkhead is consistently timing out, more concurrent requests are submitted than what the bulkhead should allow.
This happens because the Resilience4JBulkheadProvider uses CompletableFuture.supplyAsync and Completable Futures can't be interrupted.
From the CompletableFuture.cancel docs:
Additional Article on CompletableFuture and interrupts: https://nurkiewicz.com/2015/03/completablefuture-cant-be-interrupted.html
Affected Versions 2.x and 3.x
Sample Sample repository: https://github.com/Mistborn94/spring-r4j-bulkhead-demo
The sample repository contains two test classes that are almost identical: BulkheadDisabledTests and BulkheadEnabledTests. BulkheadDisabledTests passes but BulkheadEnabledTests fails because the thread is not interrupted.
Possible solution
Bulkhead.decorateSupplier
instead ofBulkhead.decorateCompletionStage
to create the bulkhead and then use an executor service with a normal future (possibly the same one that is already used in theResilience4JCircuitBreaker
) for the time limiter.I would be willing to submit a pull request for this