SAP / SAP/cloud-sdk-java

Cache evection of HTTP Client lead to not closed HttpClients

Ouverte
#760 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

feature request
Langage dominant
Java
Étoiles
41
Forks
33
Merge moyen
18 h 34 min
PR mergées (30 j)
19

Description

Describe the Bug

The current implementation of caching and evicting an HTTP Client lead to not closed HttpClients.

https://github.com/SAP/cloud-sdk-java/blob/4fb1f6a0a4a2517a10dc4b02c69a6f072d98db8a/cloudplatform/connectivity-apache-httpclient5/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultApacheHttpClient5Cache.java#L42-L47

Technically the HttpClients are CloseableHttpClient and should / need(specifically for pooled connections) to be closed after usage.
(See also this discussion)

In the current code-base, when a HttpClient with a PoolingHttpClientConnectionManager is created, it's not cleaned up properly on eviction.


The following workaround does not work in all cases:

        cache = Caffeine.newBuilder().expireAfterAccess(duration, unit).ticker(ticker).evictionListener((key, value, cause) -> {
            if (value instanceof CloseableHttpClient closeableHttpClient) {
                try {
                    closeableHttpClient.close();
                } catch (final Exception e) {
                    log.warn("Failed to close HttpClient. Ignoring the exception and continue.", e);
                }
            }
        }).build();

There are two basic scenarios:

  1. The client was created and is no longer in use and the eviction time triggers the cleanup. ✅
  2. The client was created and in still in use (long running operation, async operation, ...) and the eviction time triggers the cleanup. Then the evictionListener would kill the connection underneath. ❌
Steps to Reproduce

Code review.

Expected Behavior

Proper closing of HttpClients.

Screenshots

No response

Used Versions

Current state in main.

Code Examples
// Your code here
Stack Trace

No response

Log File
Log file ...
Affected Development Phase

Development

Impact

No Impact

Timeline

No response

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par DefaultApacheHttpClient5Cache.java au niveau de la construction du cache référencée et examinez la discussion dans l'issue 755. Suivez la manière dont les CloseableHttpClients regroupés sont créés, mis en cache, évincés et utilisés pendant des opérations de longue durée ou asynchrones ; le travail est terminé lorsque les clients évincés sont fermés sans interrompre les clients encore utilisés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
java
Domaine
networking
Type d'issue
Bug
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
32/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.