Merge pull request #6580 from alvasw/server_remove_redundant_is_stopped_bool

Server: Remove redundant isStopped boolean
This commit is contained in:
Alejandro García 2023-02-15 18:32:57 +00:00 committed by GitHub
commit bed76128b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,7 +27,6 @@ import java.io.IOException;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -45,7 +44,6 @@ class Server implements Runnable {
private final ServerSocket serverSocket; private final ServerSocket serverSocket;
private final int localPort; private final int localPort;
private final Set<Connection> connections = new CopyOnWriteArraySet<>(); private final Set<Connection> connections = new CopyOnWriteArraySet<>();
private final AtomicBoolean isStopped = new AtomicBoolean(false);
private final NetworkProtoResolver networkProtoResolver; private final NetworkProtoResolver networkProtoResolver;
private final Thread serverThread = new Thread(this); private final Thread serverThread = new Thread(this);
@ -97,7 +95,7 @@ class Server implements Runnable {
} }
} }
} catch (IOException e) { } catch (IOException e) {
if (!isStopped.get()) if (isServerActive())
e.printStackTrace(); e.printStackTrace();
} }
} catch (Throwable t) { } catch (Throwable t) {
@ -108,17 +106,14 @@ class Server implements Runnable {
public void shutDown() { public void shutDown() {
log.info("Server shutdown started"); log.info("Server shutdown started");
boolean isServerStopped = isStopped.getAndSet(true); if (isServerActive()) {
serverThread.interrupt();
if (!isServerStopped) {
connections.forEach(connection -> connection.shutDown(CloseConnectionReason.APP_SHUT_DOWN)); connections.forEach(connection -> connection.shutDown(CloseConnectionReason.APP_SHUT_DOWN));
try { try {
if (!serverSocket.isClosed()) { if (!serverSocket.isClosed()) {
serverSocket.close(); serverSocket.close();
} }
serverThread.interrupt();
} catch (SocketException e) { } catch (SocketException e) {
log.debug("SocketException at shutdown might be expected " + e.getMessage()); log.debug("SocketException at shutdown might be expected " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
@ -132,6 +127,6 @@ class Server implements Runnable {
} }
private boolean isServerActive() { private boolean isServerActive() {
return !isStopped.get() && !Thread.currentThread().isInterrupted(); return !serverThread.isInterrupted();
} }
} }