Skip to content

Commit

Permalink
change: shutdown listener in 3 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Mar 12, 2024
1 parent 19bff58 commit bbfb746
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

public class ProxyInstance implements ProxyServer {
Expand Down Expand Up @@ -175,7 +172,12 @@ public void handleCommand(@NotNull String input) {
@Override
public CompletableFuture<Void> reloadConfig() {
return CompletableFuture.runAsync(() -> {
closeListeners();
try {
closeListeners();
} catch (ExecutionException | InterruptedException | TimeoutException e) {
LOGGER.error(e);
Thread.currentThread().interrupt();
}
LOGGER.info("Loading config");
try {
ProxyConfigInstance.init();
Expand Down Expand Up @@ -238,17 +240,17 @@ public ProxyConfig getConfig() {
return proxyConfig;
}

public void closeListeners() {
public void closeListeners() throws ExecutionException, InterruptedException, TimeoutException {
if (connectionListener != null) {
LOGGER.info("Closing listeners");
connectionListener.closeFutures();
connectionListener.closeFutures().get(3, TimeUnit.MINUTES);
}
}

public void fullyCloseListeners() {
public void fullyCloseListeners() throws ExecutionException, InterruptedException, TimeoutException {
if (connectionListener != null) {
LOGGER.info("Closing listeners");
connectionListener.close();
connectionListener.close().get(3, TimeUnit.MINUTES);
connectionListener = null;
}
}
Expand All @@ -264,13 +266,14 @@ public void stop() {
} catch (IOException e) {
LOGGER.warn("Failed to close plugin loader", e);
}
fullyCloseListeners();
LOGGER.info("Shutting down executor");
worker.shutdownNow();
try {
fullyCloseListeners();
LOGGER.info("Shutting down executor");
worker.shutdownNow();
//noinspection ResultOfMethodCallIgnored
worker.awaitTermination(3, TimeUnit.MINUTES);
} catch (InterruptedException e) {
} catch (InterruptedException | ExecutionException | TimeoutException e) {
LOGGER.error(e);
Thread.currentThread().interrupt();
}
LOGGER.info("Goodbye!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;

public class ConnectionListener {
Expand Down Expand Up @@ -193,21 +196,29 @@ protected void initChannel(@NotNull Channel ch) {
.connect(serverInfo.getHost(), serverInfo.getPort());
}

public void closeFutures() {
for (ChannelFuture future : futures) {
if (future.channel().isActive() || future.channel().isOpen()) {
LOGGER.info("Closing future/listener: {}", future.channel().toString());
future.channel().close().syncUninterruptibly();
public @NotNull CompletableFuture<Void> closeFutures() {
return CompletableFuture.runAsync(() -> {
for (ChannelFuture future : futures) {
if (future.channel().isActive() || future.channel().isOpen()) {
LOGGER.info("Closing future/listener: {}", future.channel().toString());
future.channel().close().syncUninterruptibly();
}
}
}
futures.clear();
futures.clear();
});
}

public void close() {
closeFutures();
LOGGER.info("Shutting down event loop");
clientWorkerGroup.shutdownGracefully().syncUninterruptibly();
workerGroup.shutdownGracefully().syncUninterruptibly();
bossGroup.shutdownGracefully().syncUninterruptibly();
public @NotNull CompletableFuture<Void> close() {
return CompletableFuture.runAsync(() -> {
try {
closeFutures().get(3, TimeUnit.MINUTES);
} catch (InterruptedException | TimeoutException | ExecutionException e) {
LOGGER.error(e);
}
LOGGER.info("Shutting down event loop");
clientWorkerGroup.shutdownGracefully().syncUninterruptibly();
workerGroup.shutdownGracefully().syncUninterruptibly();
bossGroup.shutdownGracefully().syncUninterruptibly();
});
}
}

0 comments on commit bbfb746

Please sign in to comment.