Loss of ThreadLocal Information in OpenFeign with Resilience4j Circuit Breaker
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 25/100
調査の方向性
まず、説明されている2つの拡張ポイントである FeignCircuitBreakerInvocationHandler#asSupplier と Resilience4JCircuitBreakerFactory#configureExecutorService を読みます。コンテキスト伝播の動作を比較し、何も実装する前にサポートされている API を明確にします。非同期の circuit breaker 実行中にリクエストの ThreadLocal 情報を保持しつつ、タスク間に漏洩させないための合意済みのアプローチが決まれば完了とします。
索引モデルが issue の本文から書いたものです。
説明
Is your feature request related to a problem? Please describe.
Issue Summary:
I encountered an issue while using Spring Cloud OpenFeign. When configuring circuit breaking (resilience4j), the FeignCircuitBreakerInvocationHandler submits our Feign client to be processed by an asynchronous thread, resulting in the loss of ThreadLocal information in the request context. Do you have any suggestions for resolving this issue?
Business Scenario:
In a Spring Web project, we have a custom RequestInterceptor that retrieves information stored in ThreadLocal before making a request. This information is used to populate RequestHeader and is then passed to downstream services. However, after introducing resilience4j as a circuit breaker in OpenFeign, resilience4j executes requests in a thread pool, causing ThreadLocal information to be lost.
Describe the solution you'd like
Proposal:
Therefore, I propose adding a hook (template method) within the org.springframework.cloud.openfeign.FeignCircuitBreakerInvocationHandler#asSupplier method in OpenFeign. This hook would allow the application service to retrieve the ThreadLocal object and, within the Supplier, set the ThreadLocal to the executing thread's ThreadLocal, similar to how it is done with RequestContextHolder. Is this approach feasible?
Below is sample code, assuming SubjectContext is a hook
private Supplier<Object> asSupplier(final Method method, final Object[] args) {
final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
final Thread caller = Thread.currentThread();
// Sample code
**SubjectContext context = SubjectContext.getContext();**
return () -> {
boolean isAsync = caller != Thread.currentThread();
try {
if (isAsync) {
RequestContextHolder.setRequestAttributes(requestAttributes);
**SubjectContext.setContext(context);**
}
return dispatch.get(method).invoke(args);
}
catch (RuntimeException throwable) {
throw throwable;
}
catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
finally {
if (isAsync) {
RequestContextHolder.resetRequestAttributes();
**SubjectContext.clear();**
}
}
};
}
Describe alternatives you've considered
I have also considered using a hook provided by resilience4j during application startup, specifically within org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory#configureExecutorService. By passing a custom thread pool object here, I could potentially propagate the ThreadLocal information. Sample code is as follows:
@Configurable
@AllArgsConstructor
public class CircuitBreakerConfiguration implements ApplicationRunner {
private final Resilience4JCircuitBreakerFactory factory;
@Override
public void run(ApplicationArguments args) throws Exception {
ContextThreadPoolExecutor contextThreadPoolExecutor =
new ContextThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(1024));
// **change ThreadPoolExecutor**
factory.configureExecutorService(contextThreadPoolExecutor);
}
public static class ContextThreadPoolExecutor extends ThreadPoolExecutor {
public ContextThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
public void execute(Runnable command) {
super.execute(wrap(command));
}
private static Runnable wrap(Runnable runnable) {
**SubjectContext context = SubjectContext.getContext();**
return () -> {
**SubjectContext.setContext(context);**
try {
runnable.run();
} finally {
**SubjectContext.clear();**
}
};
}
}
}
However, I find it less elegant, and I believe resolving it within the asSupplier() method of OpenFeign would be more versatile and applicable to a broader range of scenarios.
Additional context
spring-cloud-openfeign-3.1.2
- 主要言語
- Java
- スター
- 1.3k
- フォーク
- 835
- 平均マージ
- 23時間 57分
- マージ済み PR(30日)
- 25
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
spring-cloud/spring-cloud-openfeign のほかの issue
-
question
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
spring-cloud/spring-cloud-openfeign#1392 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
FeignClientsConfiguration$MicrometerConfiguration.class cannot be opened because it does not existオープンfeedback-provided
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
spring-cloud/spring-cloud-openfeign#1379 · コメント 5 件 ·
メンテナーはふだん 1 日以内に返信
-
waiting-for-triage
難易度 3/5 1〜2日 初心者へのやさしさ 57/100
spring-cloud/spring-cloud-openfeign#1344 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
waiting-for-triage
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
spring-cloud/spring-cloud-openfeign#1343 ·
メンテナーはふだん 1 日以内に返信
-
waiting-for-triage
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
spring-cloud/spring-cloud-openfeign#1340 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
spring-cloud/spring-cloud-openfeign の issue をすべて見る
似ている issue
-
[Bug] [Go] Ack returns nil when the server answers with a non-OK status対応中かも @leizhiyuan が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
apache/rocketmq-clients#1465 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
pdfium-drift
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
Stirling-Tools/JPDFium#188 ·
メンテナーはふだん 1 日以内に返信
-
security
難易度 1/5 1時間未満 初心者へのやさしさ 70/100
IBM/ibm-quarkus-ai-langchain4j-workshop#160 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
openremote/openremote#3487 · コメント 1 件 ·