package com.cirs.biz.utils;

import io.netty.channel.ChannelOption;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

public class WebClientUtils {

    // 10m
    private static final int BUFFER_SIZE = 1024 * 1024 * 10;

    public static WebClient createWebClient(int bufferSize) {
        HttpClient httpClient = HttpClient.create()
                .option(ChannelOption.SO_RCVBUF, bufferSize)  // 设置接收缓冲区大小
                .option(ChannelOption.SO_SNDBUF, bufferSize); // 设置发送缓冲区大小

        return WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .build();
    }

    public static WebClient createWebClient() {

        return WebClient.builder().exchangeStrategies(ExchangeStrategies.builder()
                .codecs(configurer -> configurer
                        .defaultCodecs()
                        .maxInMemorySize(10 * 1024 * 1024))
                .build()).build();
    }
}
