Optimized code
continuous-integration/drone/push Build is passing Details

master
ehlxr 2021-01-27 16:04:41 +08:00
parent 251a21ef97
commit 9c102220db
4 changed files with 66 additions and 25 deletions

View File

@ -7,7 +7,9 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -57,6 +59,11 @@ public abstract class AbstractClient implements Client {
});
bootstrap = new Bootstrap();
bootstrap.group(workGroup)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
// .option(ChannelOption.TCP_NODELAY, true)
// .option(ChannelOption.SO_KEEPALIVE, true)
.channel(NioSocketChannel.class);
}
@Override

View File

@ -6,13 +6,12 @@ import io.github.ehlxr.did.client.handler.SdkClientHandler;
import io.github.ehlxr.did.common.Constants;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.util.Objects;
/**
* @author ehlxr
@ -21,44 +20,47 @@ public class SdkClient extends AbstractClient {
private final Logger logger = LoggerFactory.getLogger(SdkClient.class);
public SdkClient(String host, int port) {
this(host, port, Constants.SDK_CLIENT_TIMEOUTMILLIS);
this(host, port, 0);
}
public SdkClient(String host, int port, int timeoutMillis) {
this.host = host;
this.port = port;
this.timeoutMillis = timeoutMillis;
this.host = Objects.isNull(host) ?
"".equals(Constants.getEnv("SDK_HOST")) ? Constants.SERVER_HOST : Constants.getEnv("HTTP_PORT") :
host;
this.port = port <= 0 ?
"".equals(Constants.getEnv("SDK_PORT")) ? Constants.SDK_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT")) :
port;
this.timeoutMillis = timeoutMillis <= 0 ? Constants.SDK_CLIENT_TIMEOUTMILLIS : timeoutMillis;
}
public SdkClient() {
this("".equals(Constants.getEnv("SDK_HOST")) ? Constants.SERVER_HOST : Constants.getEnv("HTTP_PORT"),
"".equals(Constants.getEnv("SDK_PORT")) ? Constants.SDK_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT")));
this(null, 0);
}
public static SdkClientBuilder newBuilder() {
return new SdkClientBuilder();
}
@Override
public void start() {
init();
bootstrap.group(workGroup)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
// .option(ChannelOption.TCP_NODELAY, true)
// .option(ChannelOption.SO_KEEPALIVE, true)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline().addLast("SdkServerDecoder", new SdkClientDecoder(12))
.addLast("SdkServerEncoder", new SdkClientEncoder())
.addLast("SdkClientHandler", new SdkClientHandler());
}
});
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline().addLast("SdkServerDecoder", new SdkClientDecoder(12))
.addLast("SdkServerEncoder", new SdkClientEncoder())
.addLast("SdkClientHandler", new SdkClientHandler());
}
});
try {
channelFuture = bootstrap.connect(host, port).sync();
channelFuture.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> {
logger.warn("client channel close.", channelFuture.cause());
shutdown();
});
InetSocketAddress address = (InetSocketAddress) channelFuture.channel().remoteAddress();
@ -69,4 +71,35 @@ public class SdkClient extends AbstractClient {
}
}
public static final class SdkClientBuilder {
int timeoutMillis;
String host;
int port;
private SdkClientBuilder() {
}
public static SdkClientBuilder aSdkClient() {
return new SdkClientBuilder();
}
public SdkClientBuilder timeoutMillis(int timeoutMillis) {
this.timeoutMillis = timeoutMillis;
return this;
}
public SdkClientBuilder host(String host) {
this.host = host;
return this;
}
public SdkClientBuilder port(int port) {
this.port = port;
return this;
}
public SdkClient build() {
return new SdkClient(host, port, timeoutMillis);
}
}
}

View File

@ -18,7 +18,8 @@ public class DidSdkTest {
@Before
public void init() {
client = new SdkClient("127.0.0.1", 16831, 5000);
// client = new SdkClient("127.0.0.1", 16831, 5000);
client = SdkClient.newBuilder().build();
client.start();
}

View File

@ -15,7 +15,7 @@ import java.util.Objects;
*/
public class SdkServer extends BaseServer {
protected Logger logger = LoggerFactory.getLogger(SdkServer.class);
private int port = "".equals(Constants.getEnv("SDK_PORT")) ? Constants.HTTP_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT"));
private int port = "".equals(Constants.getEnv("SDK_PORT")) ? Constants.SDK_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT"));
public SdkServer(SnowFlake snowFlake) {
this(snowFlake, 0);
@ -25,7 +25,7 @@ public class SdkServer extends BaseServer {
Objects.requireNonNull(snowFlake, "snowflake instance not allow null");
this.snowFlake = snowFlake;
this.port = port == 0 ? this.port : port;
this.port = port <= 0 ? this.port : port;
}
public static SdkServerBuilder newBuilder() {