cn.ishow.starter.rpc.basic.CustomClient Maven / Gradle / Ivy
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);
}
}