Optimized code
This commit is contained in:
@@ -27,6 +27,16 @@ public abstract class AbstractClient implements Client {
|
||||
ChannelFuture channelFuture;
|
||||
Bootstrap bootstrap;
|
||||
|
||||
int timeoutMillis = 2000;
|
||||
|
||||
public int getTimeoutMillis() {
|
||||
return timeoutMillis;
|
||||
}
|
||||
|
||||
public void setTimeoutMillis(int timeoutMillis) {
|
||||
this.timeoutMillis = timeoutMillis;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
asyncResponse = new ConcurrentHashMap<>(16);
|
||||
workGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 10, new ThreadFactory() {
|
||||
@@ -168,4 +178,8 @@ public abstract class AbstractClient implements Client {
|
||||
throw new Exception(NettyUtil.parseRemoteAddr(channel));
|
||||
}
|
||||
}
|
||||
|
||||
public long invoke() throws Exception {
|
||||
return invoke(timeoutMillis);
|
||||
}
|
||||
}
|
||||
|
@@ -15,4 +15,8 @@ public interface Client {
|
||||
void invokeAsync(SdkProto proto, long timeoutMillis, InvokeCallback invokeCallback) throws Exception;
|
||||
|
||||
void invokeOneWay(SdkProto proto, long timeoutMillis) throws Exception;
|
||||
|
||||
default long invoke(long timeoutMillis) throws Exception {
|
||||
return invokeSync(new SdkProto(), timeoutMillis).getDid();
|
||||
}
|
||||
}
|
||||
|
@@ -33,8 +33,8 @@ public class SdkClient extends AbstractClient {
|
||||
public void start() {
|
||||
bootstrap.group(workGroup)
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
||||
// .option(ChannelOption.TCP_NODELAY, true)
|
||||
// .option(ChannelOption.SO_KEEPALIVE, true)
|
||||
.channel(NioSocketChannel.class)
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
@@ -46,7 +46,9 @@ public class SdkClient extends AbstractClient {
|
||||
});
|
||||
|
||||
try {
|
||||
channelFuture = bootstrap.connect((host == null || "".equals(host)) ? Constants.DEFAULT_HOST : host, port == 0 ? Constants.SDKS_PORT : port).sync();
|
||||
channelFuture = bootstrap.connect((host == null || "".equals(host)) ? Constants.DEFAULT_HOST : host,
|
||||
port == 0 ? Constants.SDKS_PORT : port).sync();
|
||||
|
||||
channelFuture.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> {
|
||||
logger.warn("client channel close.", channelFuture.cause());
|
||||
shutdown();
|
||||
|
@@ -1,7 +1,5 @@
|
||||
package cn.ceres.did;
|
||||
|
||||
import cn.ceres.did.client.InvokeCallback;
|
||||
import cn.ceres.did.client.ResponseFuture;
|
||||
import cn.ceres.did.client.SdkClient;
|
||||
import cn.ceres.did.sdk.SdkProto;
|
||||
import org.junit.After;
|
||||
@@ -23,7 +21,7 @@ public class DidSdkPressTest {
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
client = new SdkClient("127.0.0.1",16831);
|
||||
client = new SdkClient("127.0.0.1", 16831);
|
||||
// client = new SdkClient();
|
||||
client.init();
|
||||
client.start();
|
||||
@@ -50,12 +48,7 @@ public class DidSdkPressTest {
|
||||
start = System.currentTimeMillis();
|
||||
for (int i = 0; i < NUM; i++) {
|
||||
final SdkProto sdkProto = new SdkProto();
|
||||
client.invokeAsync(sdkProto, 5000, new InvokeCallback() {
|
||||
@Override
|
||||
public void operationComplete(ResponseFuture responseFuture) {
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
});
|
||||
client.invokeAsync(sdkProto, 5000, responseFuture -> countDownLatch.countDown());
|
||||
}
|
||||
|
||||
// countDownLatch.await(10, TimeUnit.SECONDS);
|
||||
|
@@ -2,6 +2,7 @@ package cn.ceres.did;
|
||||
|
||||
import cn.ceres.did.client.SdkClient;
|
||||
import cn.ceres.did.sdk.SdkProto;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
@@ -12,14 +13,18 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public class DidSdkTest {
|
||||
private static final int NUM = 10;
|
||||
SdkClient client;
|
||||
|
||||
@Test
|
||||
public void didSdkTest() throws Exception {
|
||||
SdkClient client = new SdkClient("127.0.0.1", 16831);
|
||||
@Before
|
||||
public void init() {
|
||||
client = new SdkClient("127.0.0.1", 16831);
|
||||
// SdkClient client = new SdkClient();
|
||||
client.init();
|
||||
client.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void didSdkTest() throws Exception {
|
||||
// 测试同步请求,关注rqid是否对应
|
||||
for (int i = 0; i < NUM; i++) {
|
||||
SdkProto sdkProto = new SdkProto();
|
||||
@@ -44,4 +49,12 @@ public class DidSdkTest {
|
||||
System.out.println("invokeAsync test finish");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvoke() throws Exception {
|
||||
System.out.println(client.invoke());
|
||||
|
||||
client.setTimeoutMillis(3000);
|
||||
System.out.println(client.invoke());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user