You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's currently possible to turn on gzip compression by creating a customizer and manually flipping the compression properties, but best HTTP practices are to make compression available whenever the client can accept it, and there doesn't appear to be any downside to doing so. I suggest enabling compression in the default container configurations.
and there doesn't appear to be any downside to doing so
There is a downside to enabling compression by default: the decreased bandwidth usage will come at the cost of increased CPU usage. Doesn't mean it's the wrong thing to do, but it's something to be aware of. It's perhaps telling that, AFAIK, neither Tomcat nor Jetty enables compression by default.
@wilkinsona Good point: so I guess we can still have an opinion, but let's make sure there's an easy escape hatch.
@chrylis Normally we wouldn't use @Value for something like this: it would be a property of ServerProperties (which is already shared between containers).
@wilkinsona Fair enough that there is some downside, but the overhead of gzip compared with the overhead of all the reflection and conversion Spring does on every request seems pretty minimal to me. (I'd love to see the proposed MVC Resources support for precompressed statics.)
@dsyerorg.springframework.boot.autoconfigure.web.ServerProperties appears to be Tomcat-specific. Does it just get adapted into something else if Jetty is used instead? And would not it not make sense for compression to be settable like the server port or resource caching?
(I see after some searching that apparently the server.port and spring.resources.cachePeriod properties are being set by completely different mechanisms, and I can't find where server.port is handled. Sorry for asking for so much hand-holding, but I'm apparently slow on figuring out how the Spring Boot magic is assembled.)
ServerProperties has a property port and declares a prefix="server" in its @ConfigurationPoperties. That's why server.port=9000 gets bound to it (and used by Jetty and Tomcat eventually). It's pretty simple. Read about @ConfigurationProperties in the docs that you love so much and if they don't make sense we can try and fix them.
As you have noticed, there is a Tomcat nested class in ServerPoperties that is used for tomcat-specific features. There isn't an equivalent for Jetty because we don't have as many fetaures externalized for Jetty (no-one asked yet)..
I think I have most of it figured out, and I have a sketch working. Tomcat requires specifying the content types that are eligible for compression, and its defaults are unsuitable (they don't even include application/json). The external interface for this is a single comma-separated String, and there's no built-in interface to add types.
My approach in the past for this sort of configuration question has been to define a custom @Qualifier annotation that can be placed on (in this case) a Collection<String> contentTypesToAdd, collect all of the matching beans in the @Configuration, and collate them. I haven't seen this additive sort of configuration knob used in the autoconfiguration; would using it be suitable for the project?
Never mind that last question; they're just strings, so the idiomatic way is to add an annotation that's collected off components or Configurations as they're registered. Looking through how to best handle that.
Activity
dsyer commentedon Apr 16, 2014
Maybe you could show us the right way to do it? Pull request welcome.
chrylis commentedon Apr 17, 2014
Where's the proper place to put the
@Valuefor a property that would apply to multiple configurers (in this case, Tomcat and Jetty)?wilkinsona commentedon Apr 17, 2014
There is a downside to enabling compression by default: the decreased bandwidth usage will come at the cost of increased CPU usage. Doesn't mean it's the wrong thing to do, but it's something to be aware of. It's perhaps telling that, AFAIK, neither Tomcat nor Jetty enables compression by default.
dsyer commentedon Apr 17, 2014
@wilkinsona Good point: so I guess we can still have an opinion, but let's make sure there's an easy escape hatch.
@chrylis Normally we wouldn't use
@Valuefor something like this: it would be a property ofServerProperties(which is already shared between containers).chrylis commentedon Apr 17, 2014
@wilkinsona Fair enough that there is some downside, but the overhead of gzip compared with the overhead of all the reflection and conversion Spring does on every request seems pretty minimal to me. (I'd love to see the proposed MVC Resources support for precompressed statics.)
@dsyer
org.springframework.boot.autoconfigure.web.ServerPropertiesappears to be Tomcat-specific. Does it just get adapted into something else if Jetty is used instead? And would not it not make sense for compression to be settable like the server port or resource caching?(I see after some searching that apparently the
server.portandspring.resources.cachePeriodproperties are being set by completely different mechanisms, and I can't find whereserver.portis handled. Sorry for asking for so much hand-holding, but I'm apparently slow on figuring out how the Spring Boot magic is assembled.)dsyer commentedon Apr 17, 2014
ServerPropertieshas a propertyportand declares aprefix="server"in its@ConfigurationPoperties. That's whyserver.port=9000gets bound to it (and used by Jetty and Tomcat eventually). It's pretty simple. Read about@ConfigurationPropertiesin the docs that you love so much and if they don't make sense we can try and fix them.As you have noticed, there is a
Tomcatnested class inServerPopertiesthat is used for tomcat-specific features. There isn't an equivalent for Jetty because we don't have as many fetaures externalized for Jetty (no-one asked yet)..chrylis commentedon Apr 19, 2014
I think I have most of it figured out, and I have a sketch working. Tomcat requires specifying the content types that are eligible for compression, and its defaults are unsuitable (they don't even include
application/json). The external interface for this is a single comma-separatedString, and there's no built-in interface to add types.My approach in the past for this sort of configuration question has been to define a custom
@Qualifierannotation that can be placed on (in this case) aCollection<String> contentTypesToAdd, collect all of the matching beans in the@Configuration, and collate them. I haven't seen this additive sort of configuration knob used in the autoconfiguration; would using it be suitable for the project?chrylis commentedon Apr 21, 2014
Never mind that last question; they're just strings, so the idiomatic way is to add an annotation that's collected off components or
Configurations as they're registered. Looking through how to best handle that.chrylis commentedon Apr 22, 2014
Looks like there's reconsideration of compression support in
DispatcherServletfor 4.1, so this support looks redundant unless it's rejected.