update at 2021-02-07 22:11:59 by ehlxr
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-02-07 22:11:59 +08:00
parent 5c8712914b
commit e929cd180f
16 changed files with 230 additions and 96 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>did-parent</artifactId>

View File

@@ -84,7 +84,7 @@ public abstract class AbstractClient implements Client {
@Override
public Result<SdkProto> invokeSync(long timeoutMillis) {
final Channel channel = channelFuture.channel();
if (channel.isActive()) {
if (channel.isOpen() && channel.isActive()) {
final SdkProto sdkProto = new SdkProto();
final int rqid = sdkProto.getRqid();
try {
@@ -115,7 +115,7 @@ public abstract class AbstractClient implements Client {
}
} else {
NettyUtil.closeChannel(channel);
return Result.fail(NettyUtil.parseRemoteAddr(channel));
return Result.fail("channel " + NettyUtil.parseRemoteAddr(channel) + "is not active!");
}
}
@@ -133,37 +133,36 @@ public abstract class AbstractClient implements Client {
if (channelFuture.isSuccess()) {
return;
}
// 代码执行到些说明发送失败,需要释放资源
logger.error("send a request command to channel <{}> failed.",
NettyUtil.parseRemoteAddr(channel), channelFuture.cause());
REPONSE_MAP.remove(rqid);
responseFuture.setCause(channelFuture.cause());
responseFuture.putResponse(null);
try {
responseFuture.executeInvokeCallback();
} catch (Exception e) {
logger.warn("excute callback in writeAndFlush addListener, and callback throw", e);
} finally {
responseFuture.release();
}
logger.warn("send a request command to channel <{}> failed.",
NettyUtil.parseRemoteAddr(channel), channelFuture.cause());
responseFuture.executeInvokeCallback();
responseFuture.release();
});
} catch (Exception e) {
responseFuture.release();
logger.warn("send a request to channel <{}> Exception",
NettyUtil.parseRemoteAddr(channel), e);
throw new Exception(NettyUtil.parseRemoteAddr(channel), e);
String msg = String.format("send a request to channel <%s> Exception",
NettyUtil.parseRemoteAddr(channel));
logger.error(msg, e);
throw new Exception(msg, e);
}
} else {
String info = String.format("invokeAsyncImpl tryAcquire semaphore timeout, %dms, waiting thread " +
String msg = String.format("invokeAsyncImpl tryAcquire semaphore timeout, %dms, waiting thread " +
"nums: %d semaphoreAsyncValue: %d",
timeoutMillis, this.asyncSemaphore.getQueueLength(), this.asyncSemaphore.availablePermits());
logger.warn(info);
throw new Exception(info);
logger.error(msg);
throw new Exception(msg);
}
} else {
NettyUtil.closeChannel(channel);
throw new Exception(NettyUtil.parseRemoteAddr(channel));
throw new Exception(String.format("channel %s is not active!", NettyUtil.parseRemoteAddr(channel)));
}
}

View File

@@ -57,11 +57,12 @@ public class SdkClient extends AbstractClient {
});
try {
channelFuture = bootstrap.connect(host, port).sync();
channelFuture.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> {
logger.warn("client channel close.", channelFuture.cause());
});
channelFuture = bootstrap.connect(host, port)
.sync()
.channel()
.closeFuture()
.addListener((ChannelFutureListener) channelFuture ->
logger.warn("client channel close.", channelFuture.cause()));
InetSocketAddress address = (InetSocketAddress) channelFuture.channel().remoteAddress();
logger.info("SdkClient start success, host is {}, port is {}", address.getHostName(), address.getPort());

View File

@@ -31,14 +31,14 @@ public class DidSdkTest {
@Test
public void didSdkTest() throws Exception {
// 测试同步请求关注rqid是否对应
// 测试同步请求
for (int i = 0; i < NUM; i++) {
Result<SdkProto> resultProto = client.invokeSync();
System.out.println(resultProto);
}
System.out.println("invokeync test finish");
// 测试异步请求关注rqid是否对应
// 测试异步请求
final CountDownLatch countDownLatch = new CountDownLatch(NUM);
for (int i = 0; i < NUM; i++) {
client.invokeAsync(responseFuture -> {