Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
4cdb968e
Commit
4cdb968e
authored
Mar 23, 2020
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Utilise Reactor Netty's new graceful shutdown support
Closes gh-20613
parent
6b47dd06
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
49 deletions
+16
-49
NettyGracefulShutdown.java
...mework/boot/web/embedded/netty/NettyGracefulShutdown.java
+10
-45
NettyWebServer.java
...ringframework/boot/web/embedded/netty/NettyWebServer.java
+6
-4
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyGracefulShutdown.java
View file @
4cdb968e
...
...
@@ -17,19 +17,13 @@
package
org
.
springframework
.
boot
.
web
.
embedded
.
netty
;
import
java.time.Duration
;
import
java.util.concurrent.atomic.AtomicLong
;
import
java.util.function.BiFunction
;
import
java.util.function.Supplier
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.reactivestreams.Publisher
;
import
reactor.netty.DisposableServer
;
import
reactor.netty.http.server.HttpServerRequest
;
import
reactor.netty.http.server.HttpServerResponse
;
import
org.springframework.boot.web.server.GracefulShutdown
;
import
org.springframework.http.server.reactive.ReactorHttpHandlerAdapter
;
/**
* {@link GracefulShutdown} for a Reactor Netty {@link DisposableServer}.
...
...
@@ -42,17 +36,12 @@ final class NettyGracefulShutdown implements GracefulShutdown {
private
final
Supplier
<
DisposableServer
>
disposableServer
;
private
final
Duration
lifecycleTimeout
;
private
final
Duration
period
;
private
final
AtomicLong
activeRequests
=
new
AtomicLong
();
private
volatile
boolean
shuttingDown
;
NettyGracefulShutdown
(
Supplier
<
DisposableServer
>
disposableServer
,
Duration
lifecycleTimeout
,
Duration
period
)
{
NettyGracefulShutdown
(
Supplier
<
DisposableServer
>
disposableServer
,
Duration
period
)
{
this
.
disposableServer
=
disposableServer
;
this
.
lifecycleTimeout
=
lifecycleTimeout
;
this
.
period
=
period
;
}
...
...
@@ -64,32 +53,19 @@ final class NettyGracefulShutdown implements GracefulShutdown {
if
(
server
==
null
)
{
return
false
;
}
if
(
this
.
lifecycleTimeout
!=
null
)
{
server
.
disposeNow
(
this
.
lifecycleTimeout
);
}
else
{
server
.
disposeNow
();
}
this
.
shuttingDown
=
true
;
long
end
=
System
.
currentTimeMillis
()
+
this
.
period
.
toMillis
();
try
{
while
(
this
.
activeRequests
.
get
()
>
0
&&
System
.
currentTimeMillis
()
<
end
)
{
try
{
Thread
.
sleep
(
50
);
}
catch
(
InterruptedException
ex
)
{
Thread
.
currentThread
().
interrupt
();
break
;
}
}
long
activeRequests
=
this
.
activeRequests
.
get
();
if
(
activeRequests
==
0
)
{
logger
.
info
(
"Graceful shutdown complete"
);
return
true
;
if
(
this
.
period
!=
null
)
{
server
.
disposeNow
(
this
.
period
);
}
if
(
logger
.
isInfoEnabled
())
{
logger
.
info
(
"Grace period elapsed with "
+
activeRequests
+
" request(s) still active"
);
else
{
server
.
disposeNow
(
);
}
logger
.
info
(
"Graceful shutdown complete"
);
return
true
;
}
catch
(
IllegalStateException
ex
)
{
logger
.
info
(
"Grace period elapsed with one ore more requests still active"
);
return
false
;
}
finally
{
...
...
@@ -102,15 +78,4 @@ final class NettyGracefulShutdown implements GracefulShutdown {
return
this
.
shuttingDown
;
}
BiFunction
<?
super
HttpServerRequest
,
?
super
HttpServerResponse
,
?
extends
Publisher
<
Void
>>
wrapHandler
(
ReactorHttpHandlerAdapter
handlerAdapter
)
{
if
(
this
.
period
==
null
)
{
return
handlerAdapter
;
}
return
(
request
,
response
)
->
{
this
.
activeRequests
.
incrementAndGet
();
return
handlerAdapter
.
apply
(
request
,
response
).
doOnTerminate
(()
->
this
.
activeRequests
.
decrementAndGet
());
};
}
}
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java
View file @
4cdb968e
...
...
@@ -22,6 +22,8 @@ import java.util.List;
import
java.util.function.BiFunction
;
import
java.util.function.Predicate
;
import
io.netty.channel.group.DefaultChannelGroup
;
import
io.netty.util.concurrent.DefaultEventExecutor
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.reactivestreams.Publisher
;
...
...
@@ -84,16 +86,16 @@ public class NettyWebServer implements WebServer {
Duration
shutdownGracePeriod
)
{
Assert
.
notNull
(
httpServer
,
"HttpServer must not be null"
);
Assert
.
notNull
(
handlerAdapter
,
"HandlerAdapter must not be null"
);
this
.
httpServer
=
httpServer
;
this
.
lifecycleTimeout
=
lifecycleTimeout
;
this
.
handler
=
handlerAdapter
;
if
(
shutdownGracePeriod
!=
null
)
{
this
.
httpServer
=
httpServer
.
channelGroup
(
new
DefaultChannelGroup
(
new
DefaultEventExecutor
()));
NettyGracefulShutdown
gracefulShutdown
=
new
NettyGracefulShutdown
(()
->
this
.
disposableServer
,
lifecycleTimeout
,
shutdownGracePeriod
);
this
.
handler
=
gracefulShutdown
.
wrapHandler
(
handlerAdapter
);
shutdownGracePeriod
);
this
.
shutdown
=
gracefulShutdown
;
}
else
{
this
.
h
andler
=
handlerAdapt
er
;
this
.
h
ttpServer
=
httpServ
er
;
this
.
shutdown
=
new
ImmediateGracefulShutdown
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment