package com.pharmacopoeia.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Spring MVC 异步支持配置：为流式 SSE 返回（Flux&lt;ServerSentEvent&gt;）提供线程池，
 * 替换默认的 SimpleAsyncTaskExecutor（每请求新建线程，生产负载下不适用）。
 * LLM 流式可能持续数秒到数十秒，每个流式请求在生命周期内占用一个 async 线程，
 * 因此池容量按并发流式用户数估算。
 */
@Configuration
public class WebMvcAsyncConfig implements WebMvcConfigurer {

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("mvc-async-");
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(500);
        executor.setKeepAliveSeconds(60);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        executor.initialize();
        configurer.setTaskExecutor(executor);
        // 流式上限（L1~2s、L2思考~20-30s），给 120s 余量，避免误杀长流
        configurer.setDefaultTimeout(120000L);
    }
}
