TTSConfig.java 2.15 KB
package com.xly.tts.config;

import lombok.Data;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Data
@Configuration
@ConfigurationProperties(prefix = "tts")
public class TTSConfig {

    private PythonService python = new PythonService();
    private CommandLine commandLine = new CommandLine();
    private OkHttpConfig okHttp = new OkHttpConfig();

    @Data
    public static class PythonService {
        private boolean enabled = true;
        private String url = "http://127.0.0.1:8000";
        private int timeout = 30000;
    }

    @Data
    public static class CommandLine {
        private boolean enabled = false;
        private String pythonPath = "python";
        private int bufferSize = 4096;
    }

    @Data
    public static class OkHttpConfig {
        private int connectTimeout = 30;
        private int readTimeout = 60;
        private int writeTimeout = 60;
        private int maxRequests = 64;
        private int maxRequestsPerHost = 32;
        private int maxIdleConnections = 5;
        private long keepAliveDuration = 5;
    }

    @Bean
    public OkHttpClient okHttpClient() {
        Dispatcher dispatcher = new Dispatcher();
        dispatcher.setMaxRequests(okHttp.getMaxRequests());
        dispatcher.setMaxRequestsPerHost(okHttp.getMaxRequestsPerHost());

        ConnectionPool connectionPool = new ConnectionPool(
                okHttp.getMaxIdleConnections(),
                okHttp.getKeepAliveDuration(),
                TimeUnit.MINUTES
        );

        return new OkHttpClient.Builder()
                .dispatcher(dispatcher)
                .connectionPool(connectionPool)
                .connectTimeout(okHttp.getConnectTimeout(), TimeUnit.SECONDS)
                .readTimeout(okHttp.getReadTimeout(), TimeUnit.SECONDS)
                .writeTimeout(okHttp.getWriteTimeout(), TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }
}