Skip to content

Commit

Permalink
Add constants
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Aug 31, 2023
1 parent b67c4f7 commit 1247a85
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ private void startQosServer(URL url) {

String host = url.getParameter(QOS_HOST);
int port = url.getParameter(QOS_PORT, QosConstants.DEFAULT_PORT);
boolean qosCheck = url.getParameter(QOS_CHECK, false);
boolean acceptForeignIp = Boolean.parseBoolean(url.getParameter(ACCEPT_FOREIGN_IP, "false"));
String acceptForeignIpWhitelist = url.getParameter(ACCEPT_FOREIGN_IP_WHITELIST, StringUtils.EMPTY_STRING);
String anonymousAccessPermissionLevel = url.getParameter(ANONYMOUS_ACCESS_PERMISSION_LEVEL, PermissionLevel.PUBLIC.name());
Expand All @@ -129,7 +128,7 @@ private void startQosServer(URL url) {
server.setAcceptForeignIpWhitelist(acceptForeignIpWhitelist);
server.setAnonymousAccessPermissionLevel(anonymousAccessPermissionLevel);
server.setAnonymousAllowCommands(anonymousAllowCommands);
server.start(qosCheck);
server.start(url);

} catch (Throwable throwable) {
logger.warn(QOS_FAILED_START_SERVER, "", "", "Fail to start qos server: ", throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
package org.apache.dubbo.qos.server;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.qos.api.PermissionLevel;
import org.apache.dubbo.qos.api.QosConfiguration;
import org.apache.dubbo.qos.server.handler.QosProcessHandler;
import org.apache.dubbo.remoting.Constants;
import org.apache.dubbo.rpc.model.FrameworkModel;

import io.netty.bootstrap.ServerBootstrap;
Expand All @@ -36,6 +38,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static org.apache.dubbo.common.constants.LoggerCodeConstants.QOS_FAILED_START_SERVER;
import static org.apache.dubbo.common.constants.QosConstants.QOS_CHECK;

/**
* A server serves for both telnet access and http access
Expand Down Expand Up @@ -89,7 +92,7 @@ public int getPort() {
/**
* start server, bind port
*/
public void start(boolean qosCheck) throws Throwable {
public void start(URL url) throws Throwable {
if (!started.compareAndSet(false, true)) {
return;
}
Expand All @@ -115,8 +118,11 @@ protected void initChannel(Channel ch) throws Exception {
));
}
});
boolean qosCheck = url.getParameter(QOS_CHECK, false);
int retryTimes = url.getParameter(Constants.BIND_RETRY_TIMES, 10);
int retryInterval = url.getParameter(Constants.BIND_RETRY_INTERVAL, 3000);
Throwable lastError = null;
for (int i = 0; i < 10; i++) {
for (int i = 0; i < retryTimes; i++) {
try {
if (StringUtils.isBlank(host)) {
serverBootstrap.bind(port).sync();
Expand All @@ -127,14 +133,13 @@ protected void initChannel(Channel ch) throws Exception {
logger.info("qos-server bind localhost:" + port);
return;
} catch (Throwable throwable) {
logger.error(QOS_FAILED_START_SERVER, "", "", "qos-server can not bind localhost:" + port, throwable);
lastError = throwable;
}
if (qosCheck) {
// If enable qos check, we will retry 10 times and wait 3 seconds between each retry.
logger.error(QOS_FAILED_START_SERVER, "", "", "qos-server can not bind localhost:" + port +
" and will retry 10 times. Current retry times: " + i);
Thread.sleep(3000);
Thread.sleep(retryInterval);
} else {
// If disable qos check, throw exception directly.
throw lastError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public interface Constants {

String BIND_PORT_KEY = "bind.port";

String BIND_RETRY_TIMES = "bind.retry.times";

String BIND_RETRY_INTERVAL = "bind.retry.interval";

String SENT_KEY = "sent";

String DISPATCHER_KEY = "dispatcher";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ protected void initChannel(SocketChannel ch) throws Exception {
}
InetSocketAddress bindAddress = new InetSocketAddress(bindIp, bindPort);
Throwable lastError = null;
for (int i = 0; i < 10; i++) {
int retryTimes = getUrl().getParameter(Constants.BIND_RETRY_TIMES, 10);
int retryInterval = getUrl().getParameter(Constants.BIND_RETRY_INTERVAL, 3000);
for (int i = 0; i < retryTimes; i++) {
try {
ChannelFuture channelFuture = bootstrap.bind(bindAddress);
channelFuture.syncUninterruptibly();
Expand All @@ -153,7 +155,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
"Failed to bind " + getClass().getSimpleName()
+ " on " + bindAddress + ", cause: " + lastError.getMessage() + "will retry 10 times. Current retry times: " + i, lastError);
try {
Thread.sleep(3000);
Thread.sleep(retryInterval);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ protected void doOpen() throws Throwable {

initServerBootstrap(nettyServerHandler);

int retryTimes = getUrl().getParameter(Constants.BIND_RETRY_TIMES, 10);
int retryInterval = getUrl().getParameter(Constants.BIND_RETRY_INTERVAL, 3000);
// bind
Throwable lastError = null;
for (int i = 0; i < 10; i++) {
for (int i = 0; i < retryTimes; i++) {
try {
ChannelFuture channelFuture = bootstrap.bind(getBindAddress());
channelFuture.syncUninterruptibly();
Expand All @@ -120,7 +122,7 @@ protected void doOpen() throws Throwable {
"Failed to bind " + getClass().getSimpleName()
+ " on " + getBindAddress() + ", cause: " + lastError.getMessage() + "will retry 10 times. Current retry times: " + i, lastError);
try {
Thread.sleep(3000);
Thread.sleep(retryInterval);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
Expand Down

0 comments on commit 1247a85

Please sign in to comment.