-
Notifications
You must be signed in to change notification settings - Fork 53
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
Merged
toddbaert
merged 44 commits into
open-feature:main
from
liran2000:feature/flagd-http-connector
May 14, 2025
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
b4ae05c
adding http connector
liran2000 f93014e
adding http connector - cont.
liran2000 d8f6943
adding http cache
liran2000 c5d93b5
refactor for using options
liran2000 d201b1f
readme update
liran2000 4576816
Merge branch 'main' into feature/flagd-http-connector
chrfwow 2091826
move to tool - draft
liran2000 bddeaeb
Merge remote-tracking branch 'origin/feature/flagd-http-connector' in…
liran2000 6038613
move to tool - draft - cont.
liran2000 8f03143
move to tool - draft - cont.
liran2000 59ffacb
move to tool - draft - cont.
liran2000 6405472
add Configuration section to readme
liran2000 512d3a2
readme update
liran2000 2eea2c1
Merge branch 'main' into feature/flagd-http-connector
liran2000 d615b0c
updates
liran2000 36f5bc9
updates
liran2000 ec5f158
updates
liran2000 17d5d0b
updates
liran2000 1a48989
updates
liran2000 273f2fc
updates
liran2000 8fac595
updates
liran2000 f2b70ce
add polling cache support
liran2000 ea8ec7b
add polling cache support
liran2000 9a723b9
add polling cache support
liran2000 1537745
adjust test
liran2000 e5e49f4
adjust test
liran2000 f357005
adjust
liran2000 a8fccd0
adjust
liran2000 96fd59e
adjust
liran2000 367147e
adjust
liran2000 41502c1
readme update
liran2000 350cde9
remove redundant lines
liran2000 f136dd1
Update tools/flagd-http-connector/README.md
liran2000 b9d525a
Update tools/flagd-http-connector/README.md
liran2000 34deb32
Merge branch 'main' into feature/flagd-http-connector
aepfli 6653a9f
updates
liran2000 55f2a9d
updates
liran2000 bedc394
spotless apply
liran2000 694ee51
updates
liran2000 e168622
updates
liran2000 f37b55e
updates
liran2000 0f4b639
update readme
liran2000 0e7a171
revert flagd provider changes
liran2000 bd8d1fc
revert flagd provider changes
liran2000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Changelog | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
# 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 | ||
* flagd installation is not required, the Http Connector works independently. | ||
Minimizing infrastructure and DevOps overhead - no extra containers required. | ||
* Low latency by fetching data directly in-process. | ||
* Decreased external network traffic from the HTTP source, even without a standalone flagd container or proxy, | ||
when using polling cache. | ||
* Can serve as an additional provider for fallback or internal backup scenarios using a multi-provider setup. | ||
|
||
### What happens if the Http source is down during application startup? | ||
|
||
Http Connector supports optional resilient fail-safe initialization using a cache. | ||
If the initial fetch fails due to source unavailability, it can load the initial payload from the cache instead of | ||
falling back to default values. | ||
This ensures smoother startup behavior until the source becomes available again. To be effective, the TTL of the | ||
fallback cache should be longer than the expected duration of the source downtime during initialization. | ||
|
||
### Polling cache | ||
The polling cache is used to store the payload fetched from the URL. | ||
Used when usePollingCache is configured as true. | ||
A key advantage of this cache is that it enables a single microservice within a cluster to handle the polling of a | ||
URL, effectively acting as a flagd/proxy while all other services leverage the shared cache. | ||
This approach optimizes resource usage by preventing redundant polling across services. | ||
|
||
### Sample flows demonstrating the architecture | ||
|
||
#### Basic Simple Configuration | ||
|
||
This example demonstrates a simple flow using: | ||
- GitHub as the source for flag payload. | ||
|
||
```mermaid | ||
sequenceDiagram | ||
participant service | ||
participant Github | ||
|
||
service->>Github: fetch | ||
Github->>service: payload | ||
Note right of service: polling interval passed | ||
service->>Github: fetch | ||
Github->>service: payload | ||
``` | ||
|
||
#### A More Scalable Configuration Utilizing Fail-Safe and Polling Caching Mechanisms | ||
|
||
This configuration aim to reduce network requests to the source URL, to improve performance and to improve the | ||
application's resilience to source downtime. | ||
|
||
This example demonstrates a micro-services architectural flow using: | ||
- GitHub as the source for flag payload. | ||
- Redis serving as both the fail-safe initialization cache and the polling cache. | ||
|
||
Example initialization flow during GitHub downtime, | ||
demonstrates how the application continues to access flag values from the cache even when GitHub is unavailable. | ||
In this setup, multiple microservices share the same cache, with only one service responsible for polling the source | ||
URL. | ||
|
||
```mermaid | ||
sequenceDiagram | ||
box Cluster | ||
participant micro-service-1 | ||
participant micro-service-2 | ||
participant micro-service-3 | ||
participant Redis | ||
end | ||
participant Github | ||
|
||
break source downtime | ||
micro-service-1->>Github: initialize | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Github->>micro-service-1: failure | ||
end | ||
micro-service-1->>Redis: fetch | ||
Redis->>micro-service-1: failsafe payload | ||
Note right of micro-service-1: polling interval passed | ||
micro-service-1->>Github: fetch | ||
Github->>micro-service-1: payload | ||
micro-service-2->>Redis: fetch | ||
Redis->>micro-service-2: payload | ||
micro-service-3->>Redis: fetch | ||
Redis->>micro-service-3: 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); | ||
``` | ||
|
||
#### HttpConnector using fail-safe cache and polling cache | ||
|
||
```java | ||
PayloadCache payloadCache = new PayloadCache() { | ||
|
||
@Override | ||
public void put(String key, String payload) { | ||
// implement put in cache | ||
} | ||
|
||
@Override | ||
public void put(String key, String payload, int ttlSeconds) { | ||
// implement put in cache with TTL | ||
} | ||
|
||
@Override | ||
public String get(String key) { | ||
// implement get from cache and return | ||
} | ||
}; | ||
|
||
HttpConnectorOptions httpConnectorOptions = HttpConnectorOptions.builder() | ||
.url(testUrl) | ||
.useHttpCache(true) | ||
.payloadCache(payloadCache) | ||
.payloadCacheOptions(PayloadCacheOptions.builder().build()) | ||
.useFailsafeCache(true) | ||
.pollIntervalSeconds(10) | ||
.usePollingCache(true) | ||
.build(); | ||
|
||
HttpConnector connector = HttpConnector.builder() | ||
.httpConnectorOptions(httpConnectorOptions) | ||
.build(); | ||
``` | ||
|
||
### 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. | | ||
| useFailsafeCache | Boolean | Whether to use a failsafe cache for initialization. Default is false. | | ||
| usePollingCache | Boolean | Whether to use a polling cache for initialization. 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. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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.2,)</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<groupId>dev.openfeature.contrib.tools</groupId> | ||
<artifactId>flagdhttpconnector</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>com.google.code.findbugs</groupId> | ||
<artifactId>annotations</artifactId> | ||
<version>3.0.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
84 changes: 84 additions & 0 deletions
84
...ature/contrib/tools/flagd/resolver/process/storage/connector/sync/http/FailSafeCache.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package dev.openfeature.contrib.tools.flagd.resolver.process.storage.connector.sync.http; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import lombok.Builder; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* A wrapper class for managing a payload cache with a specified update interval. | ||
* This class ensures that the cache is only updated if the specified time interval | ||
* has passed since the last update. It logs debug messages when updates are skipped | ||
* and error messages if the update process fails. | ||
* Not thread-safe. | ||
* | ||
* <p>Usage involves creating an instance with {@link PayloadCacheOptions} to set | ||
* the update interval, and then using {@link #updatePayloadIfNeeded(String)} to | ||
* conditionally update the cache and {@link #get()} to retrieve the cached payload.</p> | ||
*/ | ||
@SuppressFBWarnings( | ||
value = {"EI_EXPOSE_REP2", "CT_CONSTRUCTOR_THROW"}, | ||
justification = "builder validations") | ||
@Slf4j | ||
public class FailSafeCache { | ||
public static final String FAILSAFE_PAYLOAD_CACHE_KEY = FailSafeCache.class.getSimpleName() + ".failsafe-payload"; | ||
private long lastUpdateTimeMs; | ||
private long updateIntervalMs; | ||
private PayloadCache payloadCache; | ||
|
||
/** | ||
* Constructor for FailSafeCache. | ||
* | ||
* @param payloadCache the payload cache to be used | ||
* @param payloadCacheOptions the options for configuring the cache | ||
*/ | ||
@Builder | ||
public FailSafeCache(PayloadCache payloadCache, PayloadCacheOptions payloadCacheOptions) { | ||
validate(payloadCacheOptions); | ||
this.updateIntervalMs = payloadCacheOptions.getUpdateIntervalSeconds() * 1000L; | ||
this.payloadCache = payloadCache; | ||
} | ||
|
||
private static void validate(PayloadCacheOptions payloadCacheOptions) { | ||
if (payloadCacheOptions.getUpdateIntervalSeconds() < 1) { | ||
throw new IllegalArgumentException("pollIntervalSeconds must be larger than 0"); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the payload in the cache if the specified update interval has passed. | ||
* | ||
* @param payload the payload to be cached | ||
*/ | ||
public void updatePayloadIfNeeded(String payload) { | ||
if ((getCurrentTimeMillis() - lastUpdateTimeMs) < updateIntervalMs) { | ||
log.debug("not updating payload, updateIntervalMs not reached"); | ||
return; | ||
} | ||
|
||
try { | ||
log.debug("updating payload"); | ||
payloadCache.put(FAILSAFE_PAYLOAD_CACHE_KEY, payload); | ||
lastUpdateTimeMs = getCurrentTimeMillis(); | ||
} catch (Exception e) { | ||
log.error("failed updating cache", e); | ||
} | ||
} | ||
|
||
protected long getCurrentTimeMillis() { | ||
return System.currentTimeMillis(); | ||
} | ||
|
||
/** | ||
* Retrieves the cached payload. | ||
* | ||
* @return the cached payload | ||
*/ | ||
public String get() { | ||
try { | ||
return payloadCache.get(FAILSAFE_PAYLOAD_CACHE_KEY); | ||
} catch (Exception e) { | ||
log.error("failed getting from cache", e); | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.