Polish cluster token server handler related logic

- Enlarge retry timeout to 2s by default
- Improve remote address representation

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2019-01-04 14:36:46 +08:00
parent e4d0f4c6ab
commit 1b68d0c9a0
3 changed files with 12 additions and 5 deletions

View File

@ -53,7 +53,7 @@ public class NettyTransportServer implements ClusterTokenServer {
private static final int DEFAULT_EVENT_LOOP_THREADS = Math.max(1,
SystemPropertyUtil.getInt("io.netty.eventLoopThreads", Runtime.getRuntime().availableProcessors() * 2));
private static final int MAX_RETRY_TIMES = 3;
private static final int RETRY_SLEEP_MS = 1000;
private static final int RETRY_SLEEP_MS = 2000;
private final int port;
@ -103,7 +103,7 @@ public class NettyTransportServer implements ClusterTokenServer {
@Override
public void operationComplete(ChannelFuture future) {
if (future.cause() != null) {
RecordLog.info("[NettyTransportServer] Token server start failed (port=" + port + ")",
RecordLog.info("[NettyTransportServer] Token server start failed (port=" + port + "), failedTimes: " + failedTimes.get(),
future.cause());
currentState.compareAndSet(SERVER_STATUS_STARTING, SERVER_STATUS_OFF);
int failCount = failedTimes.incrementAndGet();

View File

@ -15,20 +15,21 @@
*/
package com.alibaba.csp.sentinel.cluster.server.config;
import com.alibaba.csp.sentinel.cluster.ClusterConstants;
/**
* @author Eric Zhao
* @since 1.4.0
*/
public class ServerTransportConfig {
public static final int DEFAULT_PORT = 8730;
public static final int DEFAULT_IDLE_SECONDS = 600;
private int port;
private int idleSeconds;
public ServerTransportConfig() {
this(DEFAULT_PORT, DEFAULT_IDLE_SECONDS);
this(ClusterConstants.DEFAULT_CLUSTER_SERVER_PORT, DEFAULT_IDLE_SECONDS);
}
public ServerTransportConfig(int port, int idleSeconds) {

View File

@ -15,6 +15,8 @@
*/
package com.alibaba.csp.sentinel.cluster.server.handler;
import java.net.InetSocketAddress;
import com.alibaba.csp.sentinel.cluster.ClusterConstants;
import com.alibaba.csp.sentinel.cluster.request.ClusterRequest;
import com.alibaba.csp.sentinel.cluster.response.ClusterResponse;
@ -105,6 +107,10 @@ public class TokenServerHandler extends ChannelInboundHandlerAdapter {
}
private String getRemoteAddress(ChannelHandlerContext ctx) {
return ctx.channel().remoteAddress().toString();
if (ctx.channel().remoteAddress() == null) {
return null;
}
InetSocketAddress inetAddress = (InetSocketAddress) ctx.channel().remoteAddress();
return inetAddress.getAddress().getHostAddress() + ":" + inetAddress.getPort();
}
}