Skip to content

Commit 6038613

Browse files
committed
move to tool - draft - cont.
Signed-off-by: liran2000 <[email protected]>
1 parent bddeaeb commit 6038613

File tree

17 files changed

+775
-206
lines changed

17 files changed

+775
-206
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<module>providers/configcat</module>
4141
<module>providers/statsig</module>
4242
<module>providers/multiprovider</module>
43+
<module>tools/flagd-http-connector</module>
4344
</modules>
4445

4546
<scm>

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/util/ConcurrentUtils.java

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Changelog
2+
3+
## [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)
4+
5+
6+
### ✨ New Features
7+
8+
* 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))
9+
10+
## [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)
11+
12+
13+
### 🐛 Bug Fixes
14+
15+
* 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))
16+
17+
## [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)
18+
19+
20+
### 🐛 Bug Fixes
21+
22+
* **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))
23+
24+
25+
### 🐛 Bug Fixes
26+
27+
* 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))
28+
* **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))
29+
30+
## [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)
31+
32+
33+
### ✨ New Features
34+
35+
* 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))
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Http Connector
2+
3+
## Introduction
4+
Http Connector is a tool for [flagd](https://github.com/open-feature/flagd) in-process resolver.
5+
6+
This mode performs flag evaluations locally (in-process).
7+
Flag configurations for evaluation are obtained via gRPC protocol using
8+
[sync protobuf schema](https://buf.build/open-feature/flagd/file/main:sync/v1/sync_service.proto) service definition.
9+
10+
## Http Connector functionality
11+
12+
HttpConnector is responsible for polling data from a specified URL at regular intervals.
13+
It is leveraging Http cache mechanism with 'ETag' header, then when receiving 304 Not Modified response,
14+
reducing traffic, reducing rate limits effects and changes updates. Can be enabled via useHttpCache option.
15+
The implementation is using Java HttpClient.
16+
17+
## Use cases and benefits
18+
* Reduce infrastructure/devops work, without additional containers needed.
19+
* Use as an additional provider for fallback / internal backup service via multi-provider.
20+
21+
### What happens if the Http source is down when application is starting ?
22+
23+
It supports optional fail-safe initialization via cache, such that on initial fetch error following by
24+
source downtime window, initial payload is taken from cache to avoid starting with default values until
25+
the source is back up. Therefore, the cache ttl expected to be higher than the expected source
26+
down-time to recover from during initialization.
27+
28+
### Sample flow
29+
Sample flow can use:
30+
- Github as the flags payload source.
31+
- Redis cache as a fail-safe initialization cache.
32+
33+
Sample flow of initialization during Github down-time window, showing that application can still use flags
34+
values as fetched from cache.
35+
```mermaid
36+
sequenceDiagram
37+
participant Provider
38+
participant Github
39+
participant Redis
40+
41+
break source downtime
42+
Provider->>Github: initialize
43+
Github->>Provider: failure
44+
end
45+
Provider->>Redis: fetch
46+
Redis->>Provider: last payload
47+
48+
```
49+
50+
## Usage
51+
52+
### Installation
53+
<!-- x-release-please-start-version -->
54+
```xml
55+
<dependency>
56+
<groupId>dev.openfeature.contrib.tools</groupId>
57+
<artifactId>flagd-http-connector</artifactId>
58+
<version>0.0.1</version>
59+
</dependency>
60+
```
61+
<!-- x-release-please-end-version -->
62+
63+
### Usage example
64+
65+
```java
66+
67+
HttpConnectorOptions httpConnectorOptions = HttpConnectorOptions.builder()
68+
.url("http://example.com/flags")
69+
.build();
70+
HttpConnector connector = HttpConnector.builder()
71+
.httpConnectorOptions(httpConnectorOptions)
72+
.build();
73+
74+
FlagdOptions options =
75+
FlagdOptions.builder()
76+
.resolverType(Config.Resolver.IN_PROCESS)
77+
.customConnector(connector)
78+
.build();
79+
80+
FlagdProvider flagdProvider = new FlagdProvider(options);
81+
```

tools/flagd-http-connector/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>dev.openfeature.contrib</groupId>
7+
<artifactId>parent</artifactId>
8+
<version>0.1.0</version>
9+
<relativePath>../../pom.xml</relativePath>
10+
</parent>
11+
<groupId>dev.openfeature.contrib.tools</groupId>
12+
<artifactId>flagd-http-connector</artifactId>
13+
<version>0.0.1</version> <!--x-release-please-version -->
14+
15+
<name>flagd-http-connector</name>
16+
<description>Flagd Http Connector</description>
17+
<url>https://openfeature.dev</url>
18+
19+
<developers>
20+
<developer>
21+
<id>liran2000</id>
22+
<name>Liran Mendelovich</name>
23+
<organization>OpenFeature</organization>
24+
<url>https://openfeature.dev/</url>
25+
</developer>
26+
</developers>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>dev.openfeature.contrib.providers</groupId>
31+
<artifactId>flagd</artifactId>
32+
<version>0.11.8</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.apache.commons</groupId>
37+
<artifactId>commons-lang3</artifactId>
38+
<version>3.17.0</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter-api</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.junit.jupiter</groupId>
48+
<artifactId>junit-jupiter-api</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
</dependencies>
53+
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dev.openfeature.contrib.tools.flagd.resolver.process.storage.connector.sync.http;
2+
3+
import java.net.http.HttpClient;
4+
import java.net.http.HttpRequest;
5+
import java.net.http.HttpResponse;
6+
import lombok.SneakyThrows;
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
/**
10+
* Fetches content from a given HTTP endpoint using caching headers to optimize network usage.
11+
* If cached ETag or Last-Modified values are available, they are included in the request headers
12+
* to potentially receive a 304 Not Modified response, reducing data transfer.
13+
* Updates the cached ETag and Last-Modified values upon receiving a 200 OK response.
14+
* It does not store the cached response, assuming not needed after first successful fetching.
15+
* Non thread-safe.
16+
*
17+
* @param httpClient the HTTP client used to send the request
18+
* @param httpRequestBuilder the builder for constructing the HTTP request
19+
* @return the HTTP response received from the server
20+
*/
21+
@Slf4j
22+
public class HttpCacheFetcher {
23+
private String cachedETag = null;
24+
private String cachedLastModified = null;
25+
26+
@SneakyThrows
27+
public HttpResponse<String> fetchContent(HttpClient httpClient, HttpRequest.Builder httpRequestBuilder) {
28+
if (cachedETag != null) {
29+
httpRequestBuilder.header("If-None-Match", cachedETag);
30+
}
31+
if (cachedLastModified != null) {
32+
httpRequestBuilder.header("If-Modified-Since", cachedLastModified);
33+
}
34+
35+
HttpRequest request = httpRequestBuilder.build();
36+
HttpResponse<String> httpResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
37+
38+
if (httpResponse.statusCode() == 200) {
39+
if (httpResponse.headers() != null) {
40+
cachedETag = httpResponse.headers().firstValue("ETag").orElse(null);
41+
cachedLastModified = httpResponse.headers().firstValue("Last-Modified").orElse(null);
42+
}
43+
log.debug("fetched new content");
44+
} else if (httpResponse.statusCode() == 304) {
45+
log.debug("got 304 Not Modified");
46+
}
47+
return httpResponse;
48+
}
49+
}

0 commit comments

Comments
 (0)