spring-cloud / spring-cloud-circuitbreaker

Spring Cloud Circuit Breaker API and Implementations
Apache License 2.0
328 stars 109 forks source link

How share SecurityContext with Circuitbreaker + Bulkhead #167

Closed daromnik closed 1 year ago

daromnik commented 1 year ago

Resilience4j version: 1.7.0 Spring Boot 2.7.5 Java version: 17

Hello.

I am using these dependensies:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-bulkhead</artifactId>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
</dependency>

My requests went through the CircuitBreaker and what would be passed to the SecurityContext I wrote a bean:

@Bean
    public Customizer<Resilience4JCircuitBreakerFactory> defaultCircuitBreakerCustomizer() {
        return factory -> factory
                .configureExecutorService(new DelegatingSecurityContextExecutorService(Executors.newCachedThreadPool()));
    }

Everything worked well. But then I needed to use Bulkhead and SecurityContext stopped being transmitted, because Bulkhead uses its own FixedThreadPoolBulkhead. Question: how can I configure Bulkhead so that it passes SecurityContext as before?

ryanjbaxter commented 1 year ago

I think you need to look into a context propegator

resilience4j.thread-pool-bulkhead:
  instances:
    backendA:
      maxThreadPoolSize: 23
      coreThreadPoolSize: 20
      queueCapacity: 100
      writableStackTraceEnabled: false
      contextPropagators:
        - com.config.CustomContextPropagator

I am going to close this issue in favor of the one you opened in the Resilience4j project https://github.com/resilience4j/resilience4j/issues/1915