Ohad Benita
Ohad Benita

Reputation: 543

Spring boot - bad TCP connection handling

We're using a REST web application written in Spring Boot & Java. After some time of system usage and checking the open ports to the server, it seems like we have a leak in handling the connections

Netstat -nao | grep 4567

This return the attached output even when no browser is open and no connection is actively made to the server I'd be glad for any assistance on this because it seems that Spring is mishandling the connections or it might be a mis-configuration on our side.

Upvotes: 3

Views: 3232

Answers (1)

Ohad Benita
Ohad Benita

Reputation: 543

The issue's been the fact the HttpResponse hasn't been closed, to fix this I've used HttpClientUtils.closeQuietly, see below :

HttpResponse response = null;
    try {
        response = client.execute(createHttpRequest(url, timeOut));
        StringBuilder result = new StringBuilder();
        try (BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) {
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
        }
        return result;
    } catch (org.apache.http.conn.HttpHostConnectException e) {
        throw new HostUnreachableException(e);
    } finally {
        HttpClientUtils.closeQuietly(response);
    }

Upvotes: 2

Related Questions