Skip to content

No support of persistent HTTP connections within org.springframework.http.client.SimpleClientHttpRequestFactory (v3.0.4) [SPR-7990] #12645

Closed

Description

@spring-projects-issues

Christian Möller opened SPR-7990 and commented

Hello,

I've found that Spring's org.springframework.http.client.SimpleClientHttpRequestFactory (at least version 3.0.4) does not support persistent HTTP connections. Applying it with RestTemplate yields creation of a lot of HTTP connections (up to some 10000s) under medium load.
WireShark shows that each connection is closed by the client immediately after response has been handled. This happens even if you configure your VM according to the recommendations from here:

http://download.oracle.com/javase/6/docs/technotes/guides/net/properties.html

and here:

http://download.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html

Based on these recommendations and to proof it as a bug I've patched org.springframework.http.client.SimpleClientHttpResponse the following way:

  • Memorizing all kind of streams provided by getBody() method
  • Draining and closing the memorized streams within close() method
  • Especially not disconnecting the HttpURLConnection within close()

This gives you something like this:

class SimpleClientHttpResponse implements ClientHttpResponse {

private final List<InputStream> memorizedStreams = new LinkedList<InputStream>();

[...]

public InputStream getBody() throws IOException {
    final InputStream errorStream = connection.getErrorStream();
    return memorizeOpenedStream(errorStream != null ? errorStream : connection.getInputStream());
}

private InputStream memorizeOpenedStream(final InputStream streamToMemorize) {
    memorizedStreams.add(streamToMemorize);
    return streamToMemorize;
}

public void close() {
    for (InputStream stream : memorizedStreams) {
        drainAndCloseQuietly(stream);
    }
    // NO disconnect of HttpURLConnection!
}

private void drainAndCloseQuietly(final InputStream stream) {
    if (stream == null) return;
    
    byte[] devNull = new byte[256];
    try {
        while (stream.read(devNull) > 0) {}
    } catch (IOException e) {}
    IOUtils.closeQuietly(stream);
}

}

Hopefully this might give you an idea of how to fix it.

Regards

Christian Möller


Affects: 3.0.4

Issue Links:

Activity

  1. spring-projects-issues commented on Sep 19, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Brian Clozel commented

    As you mentioned, the SimpleClientHttpRequestFactory simply relies on java.net.HttpURLConnection, which should support persistent connection by default (the "http.keepalive" setting is enabled by default in the JVM).

    For more specific use cases, other implementations should be used, like the one based on HttpComponents.

  2. added
    status: declined
    type: enhancement
    in: web
    on Jan 11, 2019
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

    in: webstatus: declinedtype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions