From f0ce0e3e7252f056449fafc83b10e42183a072fa Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 17 Jun 2016 15:11:01 +0100 Subject: [PATCH] =?UTF-8?q?Defer=20Tomcat=E2=80=99s=20session=20ID=20gener?= =?UTF-8?q?ator=20initialization=20until=20it=E2=80=99s=20needed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../tomcat/LazySessionIdGenerator.java | 36 +++++++++++++++++++ ...TomcatEmbeddedServletContainerFactory.java | 16 +++++++++ 2 files changed, 52 insertions(+) create mode 100644 spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/LazySessionIdGenerator.java diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/LazySessionIdGenerator.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/LazySessionIdGenerator.java new file mode 100644 index 0000000000..956f13b62d --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/LazySessionIdGenerator.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.context.embedded.tomcat; + +import org.apache.catalina.LifecycleException; +import org.apache.catalina.LifecycleState; +import org.apache.catalina.util.StandardSessionIdGenerator; + +/** + * A specialization of {@link StandardSessionIdGenerator} that initializes + * {@code SecureRandom} lazily. + * + * @author Andy Wilkinson + */ +class LazySessionIdGenerator extends StandardSessionIdGenerator { + + @Override + protected void startInternal() throws LifecycleException { + setState(LifecycleState.STARTING); + } + +} diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java index 22b581f187..c78714eca7 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java @@ -437,6 +437,7 @@ public class TomcatEmbeddedServletContainerFactory else { context.addLifecycleListener(new DisablePersistSessionListener()); } + context.addLifecycleListener(new LazySessionIdGeneratorListener()); } private void configurePersistSession(Manager manager) { @@ -807,4 +808,19 @@ public class TomcatEmbeddedServletContainerFactory } + private static class LazySessionIdGeneratorListener implements LifecycleListener { + + @Override + public void lifecycleEvent(LifecycleEvent event) { + if (event.getType().equals(Lifecycle.START_EVENT)) { + Context context = (Context) event.getLifecycle(); + Manager manager = context.getManager(); + if (manager != null) { + manager.setSessionIdGenerator(new LazySessionIdGenerator()); + } + } + } + + } + }