All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.ishow.starter.rpc.basic.CustomClient Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package cn.ishow.starter.rpc.basic;

import cn.ishow.starter.rpc.context.RpcContext;
import feign.Client;
import feign.Request;
import feign.Response;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author chongyin
 * @create 2022/5/10 下午7:31
 * @description 支持动态修改请求耗时
 */
public class CustomClient implements Client {
    private final Client delegate;
    private final RpcContext context;

    public CustomClient(Client delegate, RpcContext context) {
        this.delegate = delegate;
        this.context = context;
    }

    @Override
    public Response execute(Request request, Request.Options options) throws IOException {
        return delegate.execute(request, selectOptions(options));
    }

    /**
     * 获取http请求相关参数
     *
     * @param options
     * @return
     */
    private Request.Options selectOptions(Request.Options options) {
        Request.Options primaryOptions = context.getRequestOptions();
        if (primaryOptions == null) {
            return options;
        }
        if (primaryOptions.connectTimeout() != -1L
                && primaryOptions.readTimeout() != -1L) {
            return primaryOptions;
        }

        return new Request.Options(primaryOptions.connectTimeout() != -1L ? primaryOptions.connectTimeout() : options.connectTimeout()
                , TimeUnit.MILLISECONDS,
                primaryOptions.readTimeout() != -1L ? primaryOptions.readTimeout() : options.readTimeout(),
                TimeUnit.MILLISECONDS, true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy