influxdata / influxdb-client-java

InfluxDB 2 JVM Based Clients
https://influxdata.github.io/influxdb-client-java/
MIT License
431 stars 129 forks source link

feat: add HTTP connection interceptor which TTL configuration #643

Closed bednar closed 9 months ago

bednar commented 10 months ago

Proposed Changes

This PR introduces the ConnectionClosingInterceptor.

This interceptor closes connections that exceed a specified maximum lifetime age (TTL). It's beneficial for scenarios where your application requires establishing new connections to the same host after a predetermined interval. This interceptor is most effective in applications that use a single connection, meaning requests are not made in parallel.

package example;

import java.time.Duration;
import java.util.Collections;

import okhttp3.OkHttpClient;
import okhttp3.Protocol;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.InfluxDBClientOptions;
import com.influxdb.rest.ConnectionClosingInterceptor;

public class InfluxTTLExample {

    public static void main(final String[] args) throws InterruptedException {

        ConnectionClosingInterceptor interceptor = new ConnectionClosingInterceptor(Duration.ofSeconds(60));

        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
                .addNetworkInterceptor(interceptor)
                .protocols(Collections.singletonList(Protocol.HTTP_1_1))
                .eventListenerFactory(call -> interceptor);

        InfluxDBClientOptions options = InfluxDBClientOptions.builder()
                .okHttpClient(okHttpClientBuilder)
                .url("http://localhost:8086")
                .authenticateToken("my-token".toCharArray())
                .build();

        try (InfluxDBClient client = InfluxDBClientFactory.create(options)) {

            //
            // TODO use client
            //
        }
    }
}

Checklist

codecov-commenter commented 10 months ago

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (997599d) 88.39% compared to head (964e316) 88.35%.

Files Patch % Lines
...om/influxdb/rest/ConnectionClosingInterceptor.java 75.00% 2 Missing and 3 partials :warning:

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #643 +/- ## ============================================ - Coverage 88.39% 88.35% -0.04% + Complexity 779 777 -2 ============================================ Files 172 173 +1 Lines 7058 7078 +20 Branches 389 393 +4 ============================================ + Hits 6239 6254 +15 - Misses 697 699 +2 - Partials 122 125 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.