Skip to content

Commit

Permalink
Turn on SO_KEEPALIVE by default. Graceful shutdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Oct 8, 2019
1 parent ce1fc84 commit ae71e44
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/one/nio/server/AcceptorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class AcceptorConfig {
public int sendBuf;
public int tos;
public int backlog = 128;
public boolean keepAlive = true;
public boolean noDelay = true;
public boolean tcpFastOpen = true;
public boolean deferAccept;
Expand Down
2 changes: 2 additions & 0 deletions src/one/nio/server/AcceptorThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ final class AcceptorThread extends Thread {
if (config.tos != 0) serverSocket.setTos(config.tos);
if (config.deferAccept) serverSocket.setDeferAccept(true);

serverSocket.setKeepAlive(config.keepAlive);
serverSocket.setNoDelay(config.noDelay);
serverSocket.setTcpFastOpen(config.tcpFastOpen);
serverSocket.setReuseAddr(true, config.reusePort);
Expand All @@ -74,6 +75,7 @@ void reconfigure(AcceptorConfig config) throws IOException {
serverSocket.setTos(config.tos);
}
serverSocket.setDeferAccept(config.deferAccept);
serverSocket.setKeepAlive(config.keepAlive);
serverSocket.setNoDelay(config.noDelay);
serverSocket.setTcpFastOpen(config.tcpFastOpen);
serverSocket.setReuseAddr(true, config.reusePort);
Expand Down
2 changes: 1 addition & 1 deletion src/one/nio/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public synchronized void stop() {
selector.shutdown();
}

workers.gracefulShutdown(30000L);
workers.gracefulShutdown();
}

public void registerShutdownHook() {
Expand Down
10 changes: 8 additions & 2 deletions src/one/nio/server/WorkerPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ void setQueueTime(long queueTime) {
((WaitingSynchronousQueue) getQueue()).setQueueTime(queueTime);
}

void gracefulShutdown(long timeout) {
void gracefulShutdown() {
shutdown();
try {
awaitTermination(timeout, TimeUnit.MILLISECONDS);
awaitTermination(20, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

shutdownNow();
try {
awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

@Override
Expand Down

0 comments on commit ae71e44

Please sign in to comment.