17
17
package org .springframework .boot .web .embedded .netty ;
18
18
19
19
import java .time .Duration ;
20
- import java .util .concurrent .atomic .AtomicLong ;
21
- import java .util .function .BiFunction ;
22
20
import java .util .function .Supplier ;
23
21
24
22
import org .apache .commons .logging .Log ;
25
23
import org .apache .commons .logging .LogFactory ;
26
- import org .reactivestreams .Publisher ;
27
24
import reactor .netty .DisposableServer ;
28
- import reactor .netty .http .server .HttpServerRequest ;
29
- import reactor .netty .http .server .HttpServerResponse ;
30
25
31
26
import org .springframework .boot .web .server .GracefulShutdown ;
32
- import org .springframework .http .server .reactive .ReactorHttpHandlerAdapter ;
33
27
34
28
/**
35
29
* {@link GracefulShutdown} for a Reactor Netty {@link DisposableServer}.
@@ -42,17 +36,12 @@ final class NettyGracefulShutdown implements GracefulShutdown {
42
36
43
37
private final Supplier <DisposableServer > disposableServer ;
44
38
45
- private final Duration lifecycleTimeout ;
46
-
47
39
private final Duration period ;
48
40
49
- private final AtomicLong activeRequests = new AtomicLong ();
50
-
51
41
private volatile boolean shuttingDown ;
52
42
53
- NettyGracefulShutdown (Supplier <DisposableServer > disposableServer , Duration lifecycleTimeout , Duration period ) {
43
+ NettyGracefulShutdown (Supplier <DisposableServer > disposableServer , Duration period ) {
54
44
this .disposableServer = disposableServer ;
55
- this .lifecycleTimeout = lifecycleTimeout ;
56
45
this .period = period ;
57
46
}
58
47
@@ -64,32 +53,19 @@ public boolean shutDownGracefully() {
64
53
if (server == null ) {
65
54
return false ;
66
55
}
67
- if (this .lifecycleTimeout != null ) {
68
- server .disposeNow (this .lifecycleTimeout );
69
- }
70
- else {
71
- server .disposeNow ();
72
- }
73
56
this .shuttingDown = true ;
74
- long end = System .currentTimeMillis () + this .period .toMillis ();
75
57
try {
76
- while (this .activeRequests .get () > 0 && System .currentTimeMillis () < end ) {
77
- try {
78
- Thread .sleep (50 );
79
- }
80
- catch (InterruptedException ex ) {
81
- Thread .currentThread ().interrupt ();
82
- break ;
83
- }
84
- }
85
- long activeRequests = this .activeRequests .get ();
86
- if (activeRequests == 0 ) {
87
- logger .info ("Graceful shutdown complete" );
88
- return true ;
58
+ if (this .period != null ) {
59
+ server .disposeNow (this .period );
89
60
}
90
- if ( logger . isInfoEnabled ()) {
91
- logger . info ( "Grace period elapsed with " + activeRequests + " request(s) still active" );
61
+ else {
62
+ server . disposeNow ( );
92
63
}
64
+ logger .info ("Graceful shutdown complete" );
65
+ return true ;
66
+ }
67
+ catch (IllegalStateException ex ) {
68
+ logger .info ("Grace period elapsed with one ore more requests still active" );
93
69
return false ;
94
70
}
95
71
finally {
@@ -102,15 +78,4 @@ public boolean isShuttingDown() {
102
78
return this .shuttingDown ;
103
79
}
104
80
105
- BiFunction <? super HttpServerRequest , ? super HttpServerResponse , ? extends Publisher <Void >> wrapHandler (
106
- ReactorHttpHandlerAdapter handlerAdapter ) {
107
- if (this .period == null ) {
108
- return handlerAdapter ;
109
- }
110
- return (request , response ) -> {
111
- this .activeRequests .incrementAndGet ();
112
- return handlerAdapter .apply (request , response ).doOnTerminate (() -> this .activeRequests .decrementAndGet ());
113
- };
114
- }
115
-
116
81
}
0 commit comments