@@ -52,6 +52,11 @@ public class ClientConnectionsShutdown {
52
52
private static final DynamicIntProperty GRACEFUL_CLOSE_TIMEOUT =
53
53
new DynamicIntProperty ("server.outofservice.close.timeout" , 30 );
54
54
55
+ public enum ShutdownType {
56
+ OUT_OF_SERVICE ,
57
+ SHUTDOWN
58
+ }
59
+
55
60
private final ChannelGroup channels ;
56
61
private final EventExecutor executor ;
57
62
private final EurekaClient discoveryClient ;
@@ -81,7 +86,7 @@ private void initDiscoveryListener() {
81
86
// Schedule to gracefully close all the client connections.
82
87
if (ENABLED .get ()) {
83
88
executor .schedule (
84
- () -> gracefullyShutdownClientChannels (false ),
89
+ () -> gracefullyShutdownClientChannels (ShutdownType . OUT_OF_SERVICE ),
85
90
DELAY_AFTER_OUT_OF_SERVICE_MS .get (),
86
91
TimeUnit .MILLISECONDS );
87
92
}
@@ -91,25 +96,23 @@ private void initDiscoveryListener() {
91
96
}
92
97
93
98
public Promise <Void > gracefullyShutdownClientChannels () {
94
- return gracefullyShutdownClientChannels (true );
99
+ return gracefullyShutdownClientChannels (ShutdownType . SHUTDOWN );
95
100
}
96
101
97
- Promise <Void > gracefullyShutdownClientChannels (boolean forceCloseAfterTimeout ) {
102
+ Promise <Void > gracefullyShutdownClientChannels (ShutdownType shutdownType ) {
98
103
// Mark all active connections to be closed after next response sent.
99
104
LOG .warn ("Flagging CLOSE_AFTER_RESPONSE on {} client channels." , channels .size ());
100
105
101
106
// racy situation if new connections are still coming in, but any channels created after newCloseFuture will
102
107
// be closed during the force close stage
103
108
ChannelGroupFuture closeFuture = channels .newCloseFuture ();
104
109
for (Channel channel : channels ) {
105
- ConnectionCloseType .setForChannel (channel , ConnectionCloseType .DELAYED_GRACEFUL );
106
- ChannelPromise closePromise = channel .pipeline ().newPromise ();
107
- channel .attr (ConnectionCloseChannelAttributes .CLOSE_AFTER_RESPONSE ).set (closePromise );
110
+ flagChannelForClose (channel , shutdownType );
108
111
}
109
112
110
113
Promise <Void > promise = executor .newPromise ();
111
114
Runnable cancelTimeoutTask ;
112
- if (forceCloseAfterTimeout ) {
115
+ if (shutdownType == ShutdownType . SHUTDOWN ) {
113
116
ScheduledFuture <?> timeoutTask = executor .schedule (
114
117
() -> {
115
118
LOG .warn ("Force closing remaining {} active client channels." , channels .size ());
@@ -141,4 +144,10 @@ Promise<Void> gracefullyShutdownClientChannels(boolean forceCloseAfterTimeout) {
141
144
142
145
return promise ;
143
146
}
147
+
148
+ protected void flagChannelForClose (Channel channel , ShutdownType shutdownType ) {
149
+ ConnectionCloseType .setForChannel (channel , ConnectionCloseType .DELAYED_GRACEFUL );
150
+ ChannelPromise closePromise = channel .pipeline ().newPromise ();
151
+ channel .attr (ConnectionCloseChannelAttributes .CLOSE_AFTER_RESPONSE ).set (closePromise );
152
+ }
144
153
}
0 commit comments