Skip to content

Use HTTP compression by default #683

Closed

Description

@chrylis

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.

Activity

  1. dsyer commented on Apr 16, 2014

    @dsyer
    Member

    Maybe you could show us the right way to do it? Pull request welcome.

  2. chrylis commented on Apr 17, 2014

    @chrylis
    ContributorAuthor

    Where's the proper place to put the @Value for a property that would apply to multiple configurers (in this case, Tomcat and Jetty)?

  3. wilkinsona commented on Apr 17, 2014

    @wilkinsona
    Member

    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.

  4. dsyer commented on Apr 17, 2014

    @dsyer
    Member

    @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).

  5. chrylis commented on Apr 17, 2014

    @chrylis
    ContributorAuthor

    @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.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.)

  6. dsyer commented on Apr 17, 2014

    @dsyer
    Member

    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)..

  7. chrylis commented on Apr 19, 2014

    @chrylis
    ContributorAuthor

    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?

  8. chrylis commented on Apr 21, 2014

    @chrylis
    ContributorAuthor

    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.

  9. chrylis commented on Apr 22, 2014

    @chrylis
    ContributorAuthor

    Looks like there's reconsideration of compression support in DispatcherServlet for 4.1, so this support looks redundant unless it's rejected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions