Skip to content

feat(flagd): add http connector for In-process resolver #1299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<module>providers/configcat</module>
<module>providers/statsig</module>
<module>providers/multiprovider</module>
<module>tools/flagd-http-connector</module>
</modules>

<scm>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
org.org.slf4j.simpleLogger.defaultLogLevel=debug
org.slf4j.simpleLogger.defaultLogLevel=debug

io.grpc.level=trace
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.org.slf4j.simpleLogger.defaultLogLevel=debug
org.slf4j.simpleLogger.defaultLogLevel=debug
org.slf4j.simpleLogger.showDateTime=

io.grpc.level=trace
35 changes: 35 additions & 0 deletions tools/flagd-http-connector/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Changelog

## [0.1.2](https://github.com/open-feature/java-sdk-contrib/compare/dev.openfeature.contrib.tools.junitopenfeature-v0.1.1...dev.openfeature.contrib.tools.junitopenfeature-v0.1.2) (2024-12-03)


### ✨ New Features

* added interception of parameterized tests to Junit OpenFeature Extension ([#1093](https://github.com/open-feature/java-sdk-contrib/issues/1093)) ([a78c906](https://github.com/open-feature/java-sdk-contrib/commit/a78c906b24b53f7d25eb01aad85ed614eb30ca05))

## [0.1.1](https://github.com/open-feature/java-sdk-contrib/compare/dev.openfeature.contrib.tools.junitopenfeature-v0.1.0...dev.openfeature.contrib.tools.junitopenfeature-v0.1.1) (2024-09-27)


### 🐛 Bug Fixes

* race condition causing default when multiple flags are used ([#983](https://github.com/open-feature/java-sdk-contrib/issues/983)) ([356a973](https://github.com/open-feature/java-sdk-contrib/commit/356a973cf2b6ddf82b8311ea200fa30df4f1d048))

## [0.1.0](https://github.com/open-feature/java-sdk-contrib/compare/dev.openfeature.contrib.tools.junitopenfeature-v0.0.3...dev.openfeature.contrib.tools.junitopenfeature-v0.1.0) (2024-09-25)


### 🐛 Bug Fixes

* **deps:** update dependency org.apache.commons:commons-lang3 to v3.17.0 ([#932](https://github.com/open-feature/java-sdk-contrib/issues/932)) ([c598d9f](https://github.com/open-feature/java-sdk-contrib/commit/c598d9f0a61f2324fb85d72fdfea34811283c575))


### 🐛 Bug Fixes

* added missing dependency and installation instruction ([#895](https://github.com/open-feature/java-sdk-contrib/issues/895)) ([6748d02](https://github.com/open-feature/java-sdk-contrib/commit/6748d02403f0ceecb6cb9ecdfb2fecf98423a7db))
* **deps:** update dependency org.apache.commons:commons-lang3 to v3.16.0 ([#908](https://github.com/open-feature/java-sdk-contrib/issues/908)) ([d21cfe3](https://github.com/open-feature/java-sdk-contrib/commit/d21cfe3ac7da1ff6e1a4dc2ee4b0db5c24ed4847))

## [0.0.2](https://github.com/open-feature/java-sdk-contrib/compare/dev.openfeature.contrib.tools.junitopenfeature-v0.0.1...dev.openfeature.contrib.tools.junitopenfeature-v0.0.2) (2024-07-29)


### ✨ New Features

* Add JUnit5 extension for OpenFeature ([#888](https://github.com/open-feature/java-sdk-contrib/issues/888)) ([9fff9db](https://github.com/open-feature/java-sdk-contrib/commit/9fff9db4bcee3c3ae8128a1b2fb040f53df1d5ed))
103 changes: 103 additions & 0 deletions tools/flagd-http-connector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Http Connector

## Introduction
Http Connector is a tool for [flagd](https://github.com/open-feature/flagd) in-process resolver.

This mode performs flag evaluations locally (in-process).
Flag configurations for evaluation are obtained via Http.

## Http Connector functionality

HttpConnector is responsible for polling data from a specified URL at regular intervals.
It is leveraging Http cache mechanism with 'ETag' header, then when receiving 304 Not Modified response,
reducing traffic, reducing rate limits effects and changes updates. Can be enabled via useHttpCache option.
The implementation is using Java HttpClient.

## Use cases and benefits
* Reduce infrastructure/devops work, without additional containers needed.
* Use as an additional provider for fallback / internal backup service via multi-provider.

### What happens if the Http source is down when application is starting ?

It supports optional fail-safe initialization via cache, such that on initial fetch error following by
source downtime window, initial payload is taken from cache to avoid starting with default values until
the source is back up. Therefore, the cache ttl expected to be higher than the expected source
down-time to recover from during initialization.

### Sample flow
Sample flow can use:
- Github as the flags payload source.
- Redis cache as a fail-safe initialization cache.

Sample flow of initialization during Github down-time window, showing that application can still use flags
values as fetched from cache.
```mermaid
sequenceDiagram
participant Provider
participant Github
participant Redis

break source downtime
Provider->>Github: initialize
Github->>Provider: failure
end
Provider->>Redis: fetch
Redis->>Provider: last payload

```

## Usage

### Installation
<!-- x-release-please-start-version -->
```xml
<dependency>
<groupId>dev.openfeature.contrib.tools</groupId>
<artifactId>flagd-http-connector</artifactId>
<version>0.0.1</version>
</dependency>
```
<!-- x-release-please-end-version -->

### Usage example

```java

HttpConnectorOptions httpConnectorOptions = HttpConnectorOptions.builder()
.url("http://example.com/flags")
.build();
HttpConnector connector = HttpConnector.builder()
.httpConnectorOptions(httpConnectorOptions)
.build();

FlagdOptions options =
FlagdOptions.builder()
.resolverType(Config.Resolver.IN_PROCESS)
.customConnector(connector)
.build();

FlagdProvider flagdProvider = new FlagdProvider(options);
```

### Configuration
The Http Connector can be configured using the following properties in the `HttpConnectorOptions` class.:

| Property Name | Type | Description |
|-------------------------------------------|---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| url | String | The URL to poll for updates. This is a required field. |
| pollIntervalSeconds | Integer | The interval in seconds at which the connector will poll the URL for updates. Default is 60 seconds. |
| connectTimeoutSeconds | Integer | The timeout in seconds for establishing a connection to the URL. Default is 10 seconds. |
| requestTimeoutSeconds | Integer | The timeout in seconds for the request to complete. Default is 10 seconds. |
| linkedBlockingQueueCapacity | Integer | The capacity of the linked blocking queue used for processing requests. Default is 100. |
| scheduledThreadPoolSize | Integer | The size of the scheduled thread pool used for processing requests. Default is 2. |
| headers | Map<String, String> | A map of headers to be included in the request. Default is an empty map. |
| httpClientExecutor | ExecutorService | The executor service used for making HTTP requests. Default is a fixed thread pool with 1 thread. |
| proxyHost | String | The host of the proxy server to use for requests. Default is null. |
| proxyPort | Integer | The port of the proxy server to use for requests. Default is null. |
| payloadCacheOptions | PayloadCacheOptions | Options for configuring the payload cache. Default is null. |
| payloadCache | PayloadCache | The payload cache to use for caching responses. Default is null. |
| useHttpCache | Boolean | Whether to use HTTP caching for the requests. Default is false. |
| PayloadCacheOptions.updateIntervalSeconds | Integer | The interval, in seconds, at which the cache is updated. By default, this is set to 30 minutes. The goal is to avoid overloading fallback cache writes, since the cache serves only as a fallback mechanism. Typically, this value can be tuned to be shorter than the cache's TTL, balancing the need to minimize unnecessary updates while still handling edge cases effectively. |



54 changes: 54 additions & 0 deletions tools/flagd-http-connector/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.openfeature.contrib</groupId>
<artifactId>parent</artifactId>
<version>0.1.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<groupId>dev.openfeature.contrib.tools</groupId>
<artifactId>flagd-http-connector</artifactId>
<version>0.0.1</version> <!--x-release-please-version -->

<name>flagd-http-connector</name>
<description>Flagd Http Connector</description>
<url>https://openfeature.dev</url>

<developers>
<developer>
<id>liran2000</id>
<name>Liran Mendelovich</name>
<organization>OpenFeature</organization>
<url>https://openfeature.dev/</url>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>dev.openfeature.contrib.providers</groupId>
<artifactId>flagd</artifactId>
<version>0.11.8</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.17.0</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dev.openfeature.contrib.tools.flagd.resolver.process.storage.connector.sync.http;

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
* Fetches content from a given HTTP endpoint using caching headers to optimize network usage.
* If cached ETag or Last-Modified values are available, they are included in the request headers
* to potentially receive a 304 Not Modified response, reducing data transfer.
* Updates the cached ETag and Last-Modified values upon receiving a 200 OK response.
* It does not store the cached response, assuming not needed after first successful fetching.
* Non thread-safe.
*
* @param httpClient the HTTP client used to send the request
* @param httpRequestBuilder the builder for constructing the HTTP request
* @return the HTTP response received from the server
*/
@Slf4j
public class HttpCacheFetcher {
private String cachedETag = null;
private String cachedLastModified = null;

@SneakyThrows
public HttpResponse<String> fetchContent(HttpClient httpClient, HttpRequest.Builder httpRequestBuilder) {
if (cachedETag != null) {
httpRequestBuilder.header("If-None-Match", cachedETag);
}
if (cachedLastModified != null) {
httpRequestBuilder.header("If-Modified-Since", cachedLastModified);
}

HttpRequest request = httpRequestBuilder.build();
HttpResponse<String> httpResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

if (httpResponse.statusCode() == 200) {
if (httpResponse.headers() != null) {
cachedETag = httpResponse.headers().firstValue("ETag").orElse(null);
cachedLastModified = httpResponse.headers().firstValue("Last-Modified").orElse(null);
}
log.debug("fetched new content");
} else if (httpResponse.statusCode() == 304) {
log.debug("got 304 Not Modified");
}
return httpResponse;
}
}
Loading
Loading