Skip to content

Commit

Permalink
Solve the port not immediately available on UI server termination pro…
Browse files Browse the repository at this point in the history
…blem.
  • Loading branch information
hptruong93 committed Aug 2, 2018
1 parent 5ca919e commit bcc9043
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/core/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public class Config implements ILoggable {

public static final String RELEASE_VERSION = "5.0.2";
public static final String RELEASE_VERSION = "5.0.3";
protected static final String CONFIG_FILE_NAME = "config.json";
public static final String EXPORTED_CONFIG_FILE_NAME = "exported_" + CONFIG_FILE_NAME;
protected static final String CURRENT_CONFIG_VERSION = "2.6";
Expand Down
25 changes: 8 additions & 17 deletions src/core/webui/server/UIServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.http.ExceptionLogger;
import org.apache.http.impl.nio.bootstrap.HttpServer;
import org.apache.http.impl.nio.bootstrap.ServerBootstrap;
import org.apache.http.impl.nio.reactor.IOReactorConfig;

import core.config.WebUIConfig;
import core.ipc.IPCServiceWithModifablePort;
Expand Down Expand Up @@ -104,8 +105,6 @@

public class UIServer extends IPCServiceWithModifablePort {

private static final int MAX_RETRY = 40;
private static final int RETRY_DELAY_MS = 2000; // Total retry time = RETRY_DELAY_MS * MAX_RETRY.
private static final int TERMINATION_DELAY_SECOND = 2;
private static final int DEFAULT_PORT = WebUIConfig.DEFAULT_SERVER_PORT;

Expand Down Expand Up @@ -239,6 +238,7 @@ protected void start() throws IOException {

ServerBootstrap serverBootstrap = ServerBootstrap.bootstrap()
.setLocalAddress(InetAddress.getByName("localhost"))
.setIOReactorConfig(IOReactorConfig.custom().setSoReuseAddress(true).build())
.setListenerPort(port)
.setServerInfo("Repeat")
.setExceptionLogger(ExceptionLogger.STD_ERR)
Expand Down Expand Up @@ -324,21 +324,12 @@ public Logger getLogger() {
}

private boolean portFree() throws IOException {
for (int i = 0; i < MAX_RETRY; i++) {
try {
ServerSocket socket = new ServerSocket(port);
socket.close();
return true;
} catch (BindException e) {
getLogger().info("Bind exception on port " + port + ". "
+ "Retrying after " + RETRY_DELAY_MS + "ms. "
+ (i * RETRY_DELAY_MS) + " ms passed. "
+ "Max wait time is " + (MAX_RETRY * RETRY_DELAY_MS) + "ms.");
try {
Thread.sleep(RETRY_DELAY_MS);
} catch (InterruptedException e1) {
}
}
try {
ServerSocket socket = new ServerSocket(port);
socket.close();
return true;
} catch (BindException e) {
getLogger().warning("Bind exception on port " + port + ". ");
}
return false;
}
Expand Down

0 comments on commit bcc9043

Please sign in to comment.