Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f0ce0e3

Browse files
committedJun 17, 2016
Defer Tomcat’s session ID generator initialization until it’s needed
By default, Tomcat forces the generation of a session id during startup to ensure that a SecureRandom instance has been initialized. When there is a lack of entropy (as is often the case on a newly booted VPS, for example) this can block for a long time (several minutes in some cases) causing users to incorrectly believe that their application has hung during startup. This is particularly problematic for applications that don't use HTTP sessions as they are paying the startup cost for no benefit. This commit address the problem by configuring a custom SessionIdGenerator that does not initialize itself during startup. Instead, the initialization is now deferred until a request for a session id is made. Closes gh-6174
1 parent bce6bd6 commit f0ce0e3

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
 
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.embedded.tomcat;
18+
19+
import org.apache.catalina.LifecycleException;
20+
import org.apache.catalina.LifecycleState;
21+
import org.apache.catalina.util.StandardSessionIdGenerator;
22+
23+
/**
24+
* A specialization of {@link StandardSessionIdGenerator} that initializes
25+
* {@code SecureRandom} lazily.
26+
*
27+
* @author Andy Wilkinson
28+
*/
29+
class LazySessionIdGenerator extends StandardSessionIdGenerator {
30+
31+
@Override
32+
protected void startInternal() throws LifecycleException {
33+
setState(LifecycleState.STARTING);
34+
}
35+
36+
}

‎spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ private void configureSession(Context context) {
437437
else {
438438
context.addLifecycleListener(new DisablePersistSessionListener());
439439
}
440+
context.addLifecycleListener(new LazySessionIdGeneratorListener());
440441
}
441442

442443
private void configurePersistSession(Manager manager) {
@@ -807,4 +808,19 @@ public void lifecycleEvent(LifecycleEvent event) {
807808

808809
}
809810

811+
private static class LazySessionIdGeneratorListener implements LifecycleListener {
812+
813+
@Override
814+
public void lifecycleEvent(LifecycleEvent event) {
815+
if (event.getType().equals(Lifecycle.START_EVENT)) {
816+
Context context = (Context) event.getLifecycle();
817+
Manager manager = context.getManager();
818+
if (manager != null) {
819+
manager.setSessionIdGenerator(new LazySessionIdGenerator());
820+
}
821+
}
822+
}
823+
824+
}
825+
810826
}

0 commit comments

Comments
 (0)
Please sign in to comment.