package cn.reghao.tnb.content.app.config;

import org.slf4j.MDC;

import java.util.concurrent.Executor;

/**
 * 解决线程异步场景下 RequestId 的在 logback 中的打印问题
 *
 * @author reghao
 * @date 2025-07-17 16:57:41
 */
public class MdcExecutor implements Executor {
    private final String key = "request_id";
    private final Executor executor;

    public MdcExecutor(Executor executor) {
        this.executor = executor;
    }

    public void execute(Runnable command) {
        final String requestId = MDC.get(key);
        executor.execute(() -> {
            MDC.put(key, requestId);
            try {
                command.run();
            } finally {
                MDC.remove(key);
            }
        });
    }
}
