Refactor DefaultClientConfig for readability.

This commit is contained in:
KomachiSion 2022-09-07 09:59:34 +08:00
parent b36e6a50ab
commit fb71b4ccd9
3 changed files with 40 additions and 29 deletions

View File

@ -63,7 +63,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
* *
* @param builder builder of DefaultClientConfig builder. * @param builder builder of DefaultClientConfig builder.
*/ */
public DefaultClientConfig(Builder builder) { private DefaultClientConfig(Builder builder) {
this.name = builder.name; this.name = builder.name;
this.retryTimes = builder.retryTimes; this.retryTimes = builder.retryTimes;
this.timeOutMills = builder.timeOutMills; this.timeOutMills = builder.timeOutMills;
@ -101,7 +101,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
@Override @Override
public long connectionKeepAlive() { public long connectionKeepAlive() {
return Long.parseLong(System.getProperty(GrpcConstants.NACOS_CLIENT_GRPC_CONNECT_KEEP_ALIVE_TIME, return Long.parseLong(System.getProperty(GrpcConstants.NACOS_CLIENT_GRPC_CONNECT_KEEP_ALIVE_TIME,
String.valueOf(this.channelKeepAlive))); String.valueOf(this.connectionKeepAlive)));
} }
@Override @Override
@ -163,6 +163,10 @@ public class DefaultClientConfig implements GrpcClientConfig {
return this.labels; return this.labels;
} }
public static Builder newBuilder() {
return new Builder();
}
public static class Builder { public static class Builder {
private String name; private String name;
@ -193,7 +197,16 @@ public class DefaultClientConfig implements GrpcClientConfig {
private Map<String, String> labels = new HashMap<>(); private Map<String, String> labels = new HashMap<>();
public Builder(Properties properties) { private Builder() {
}
/**
* Set config from properties.
*
* @param properties properties
* @return Builder
*/
public Builder fromProperties(Properties properties) {
if (properties.contains(GrpcConstants.NACOS_CLIENT_GRPC_NAME)) { if (properties.contains(GrpcConstants.NACOS_CLIENT_GRPC_NAME)) {
this.name = properties.getProperty(GrpcConstants.NACOS_CLIENT_GRPC_NAME); this.name = properties.getProperty(GrpcConstants.NACOS_CLIENT_GRPC_NAME);
} }
@ -243,15 +256,13 @@ public class DefaultClientConfig implements GrpcClientConfig {
this.healthCheckTimeOut = Long.parseLong( this.healthCheckTimeOut = Long.parseLong(
properties.getProperty(GrpcConstants.NACOS_CLIENT_GRPC_HEALTHCHECK_TIMEOUT)); properties.getProperty(GrpcConstants.NACOS_CLIENT_GRPC_HEALTHCHECK_TIMEOUT));
} }
} return this;
public Builder() {
} }
/** /**
* set client name. * set client name.
*/ */
public Builder name(String name) { public Builder setName(String name) {
this.name = name; this.name = name;
return this; return this;
} }
@ -259,7 +270,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set retryTimes. * set retryTimes.
*/ */
public Builder retryTimes(int retryTimes) { public Builder setRetryTimes(int retryTimes) {
this.retryTimes = retryTimes; this.retryTimes = retryTimes;
return this; return this;
} }
@ -267,7 +278,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set timeOutMills. * set timeOutMills.
*/ */
public Builder timeOutMills(long timeOutMills) { public Builder setTimeOutMills(long timeOutMills) {
this.timeOutMills = timeOutMills; this.timeOutMills = timeOutMills;
return this; return this;
} }
@ -275,7 +286,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set connectionKeepAlive. * set connectionKeepAlive.
*/ */
public Builder connectionKeepAlive(long connectionKeepAlive) { public Builder setConnectionKeepAlive(long connectionKeepAlive) {
this.connectionKeepAlive = connectionKeepAlive; this.connectionKeepAlive = connectionKeepAlive;
return this; return this;
} }
@ -283,7 +294,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set threadPoolKeepAlive. * set threadPoolKeepAlive.
*/ */
public Builder threadPoolKeepAlive(Long threadPoolKeepAlive) { public Builder setThreadPoolKeepAlive(Long threadPoolKeepAlive) {
this.threadPoolKeepAlive = threadPoolKeepAlive; this.threadPoolKeepAlive = threadPoolKeepAlive;
return this; return this;
} }
@ -291,7 +302,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set threadPoolCoreSize. * set threadPoolCoreSize.
*/ */
public Builder threadPoolCoreSize(Integer threadPoolCoreSize) { public Builder setThreadPoolCoreSize(Integer threadPoolCoreSize) {
if (!Objects.isNull(threadPoolCoreSize)) { if (!Objects.isNull(threadPoolCoreSize)) {
this.threadPoolCoreSize = threadPoolCoreSize; this.threadPoolCoreSize = threadPoolCoreSize;
} }
@ -301,7 +312,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set threadPoolMaxSize. * set threadPoolMaxSize.
*/ */
public Builder threadPoolMaxSize(Integer threadPoolMaxSize) { public Builder setThreadPoolMaxSize(Integer threadPoolMaxSize) {
if (!Objects.isNull(threadPoolMaxSize)) { if (!Objects.isNull(threadPoolMaxSize)) {
this.threadPoolMaxSize = threadPoolMaxSize; this.threadPoolMaxSize = threadPoolMaxSize;
} }
@ -311,7 +322,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set serverCheckTimeOut. * set serverCheckTimeOut.
*/ */
public Builder serverCheckTimeOut(Long serverCheckTimeOut) { public Builder setServerCheckTimeOut(Long serverCheckTimeOut) {
this.serverCheckTimeOut = serverCheckTimeOut; this.serverCheckTimeOut = serverCheckTimeOut;
return this; return this;
} }
@ -319,7 +330,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set threadPoolQueueSize. * set threadPoolQueueSize.
*/ */
public Builder threadPoolQueueSize(int threadPoolQueueSize) { public Builder setThreadPoolQueueSize(int threadPoolQueueSize) {
this.threadPoolQueueSize = threadPoolQueueSize; this.threadPoolQueueSize = threadPoolQueueSize;
return this; return this;
} }
@ -327,7 +338,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set maxInboundMessageSize. * set maxInboundMessageSize.
*/ */
public Builder maxInboundMessageSize(int maxInboundMessageSize) { public Builder setMaxInboundMessageSize(int maxInboundMessageSize) {
this.maxInboundMessageSize = maxInboundMessageSize; this.maxInboundMessageSize = maxInboundMessageSize;
return this; return this;
} }
@ -335,7 +346,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set channelKeepAlive. * set channelKeepAlive.
*/ */
public Builder channelKeepAlive(int channelKeepAlive) { public Builder setChannelKeepAlive(int channelKeepAlive) {
this.channelKeepAlive = channelKeepAlive; this.channelKeepAlive = channelKeepAlive;
return this; return this;
} }
@ -343,7 +354,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set healthCheckRetryTimes. * set healthCheckRetryTimes.
*/ */
public Builder healthCheckRetryTimes(int healthCheckRetryTimes) { public Builder setHealthCheckRetryTimes(int healthCheckRetryTimes) {
this.healthCheckRetryTimes = healthCheckRetryTimes; this.healthCheckRetryTimes = healthCheckRetryTimes;
return this; return this;
} }
@ -351,7 +362,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set healthCheckTimeOut. * set healthCheckTimeOut.
*/ */
public Builder healthCheckTimeOut(long healthCheckTimeOut) { public Builder setHealthCheckTimeOut(long healthCheckTimeOut) {
this.healthCheckTimeOut = healthCheckTimeOut; this.healthCheckTimeOut = healthCheckTimeOut;
return this; return this;
} }
@ -359,7 +370,7 @@ public class DefaultClientConfig implements GrpcClientConfig {
/** /**
* set labels. * set labels.
*/ */
public Builder labels(Map<String, String> labels) { public Builder setLabels(Map<String, String> labels) {
this.labels.putAll(labels); this.labels.putAll(labels);
return this; return this;
} }

View File

@ -89,8 +89,8 @@ public abstract class GrpcClient extends RpcClient {
*/ */
public GrpcClient(String name, Integer threadPoolCoreSize, Integer threadPoolMaxSize, Map<String, String> labels) { public GrpcClient(String name, Integer threadPoolCoreSize, Integer threadPoolMaxSize, Map<String, String> labels) {
super(); super();
GrpcClientConfig config = new DefaultClientConfig.Builder().name(name).threadPoolCoreSize(threadPoolCoreSize) GrpcClientConfig config = DefaultClientConfig.newBuilder().setName(name).setThreadPoolCoreSize(threadPoolCoreSize)
.threadPoolMaxSize(threadPoolMaxSize).labels(labels).build(); .setThreadPoolMaxSize(threadPoolMaxSize).setLabels(labels).build();
rpcClientConfig = config; rpcClientConfig = config;
this.clientConfig = config; this.clientConfig = config;
init(); init();
@ -113,7 +113,7 @@ public abstract class GrpcClient extends RpcClient {
* @param name . * @param name .
*/ */
public GrpcClient(String name) { public GrpcClient(String name) {
this(new DefaultClientConfig.Builder().name(name).build()); this(DefaultClientConfig.newBuilder().setName(name).build());
} }
/** /**
@ -122,7 +122,7 @@ public abstract class GrpcClient extends RpcClient {
* @param properties . * @param properties .
*/ */
public GrpcClient(Properties properties) { public GrpcClient(Properties properties) {
this(new DefaultClientConfig.Builder(properties).build()); this(DefaultClientConfig.newBuilder().fromProperties(properties).build());
} }
protected ThreadPoolExecutor createGrpcExecutor(String serverIp) { protected ThreadPoolExecutor createGrpcExecutor(String serverIp) {
@ -233,8 +233,8 @@ public abstract class GrpcClient extends RpcClient {
} catch (Exception e) { } catch (Exception e) {
LoggerUtils.printIfErrorEnabled(LOGGER, "[{}]Handle server request exception: {}", LoggerUtils.printIfErrorEnabled(LOGGER, "[{}]Handle server request exception: {}",
grpcConn.getConnectionId(), payload.toString(), e.getMessage()); grpcConn.getConnectionId(), payload.toString(), e.getMessage());
Response errResponse = ErrorResponse.build(NacosException.CLIENT_ERROR, Response errResponse = ErrorResponse
"Handle server request error"); .build(NacosException.CLIENT_ERROR, "Handle server request error");
errResponse.setRequestId(request.getRequestId()); errResponse.setRequestId(request.getRequestId());
sendResponse(errResponse); sendResponse(errResponse);
} }
@ -312,8 +312,8 @@ public abstract class GrpcClient extends RpcClient {
return null; return null;
} }
BiRequestStreamGrpc.BiRequestStreamStub biRequestStreamStub = BiRequestStreamGrpc.newStub( BiRequestStreamGrpc.BiRequestStreamStub biRequestStreamStub = BiRequestStreamGrpc
newChannelStubTemp.getChannel()); .newStub(newChannelStubTemp.getChannel());
GrpcConnection grpcConn = new GrpcConnection(serverInfo, grpcExecutor); GrpcConnection grpcConn = new GrpcConnection(serverInfo, grpcExecutor);
grpcConn.setConnectionId(((ServerCheckResponse) response).getConnectionId()); grpcConn.setConnectionId(((ServerCheckResponse) response).getConnectionId());

View File

@ -46,7 +46,7 @@ public class GrpcClientTest {
@Before @Before
public void setUp() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { public void setUp() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
GrpcClientConfig clientConfig = new DefaultClientConfig.Builder().name("testClient").build(); GrpcClientConfig clientConfig = DefaultClientConfig.newBuilder().setName("testClient").build();
grpcClient = spy(new GrpcClient(clientConfig) { grpcClient = spy(new GrpcClient(clientConfig) {
@Override @Override
public int rpcPortOffset() { public int rpcPortOffset() {