Skip to content

Commit c3b0e11

Browse files
authored
Merge branch 'main' into feat/unify-cucumber-version
2 parents 9581a5b + 1bd8f86 commit c3b0e11

File tree

17 files changed

+149
-70
lines changed

17 files changed

+149
-70
lines changed

providers/flagd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tmp/

providers/flagd/README.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ The value is updated with every (re)connection to the sync implementation.
5454
This can be used to enrich evaluations with such data.
5555
If the `in-process` mode is not used, and before the provider is ready, the `getSyncMetadata` returns an empty map.
5656

57-
#### Offline mode
57+
### Offline mode (File resolver)
5858

5959
In-process resolvers can also work in an offline mode.
6060
To enable this mode, you should provide a valid flag configuration file with the option `offlineFlagSourcePath`.
6161

6262
```java
6363
FlagdProvider flagdProvider = new FlagdProvider(
6464
FlagdOptions.builder()
65-
.resolverType(Config.Resolver.IN_PROCESS)
65+
.resolverType(Config.Resolver.FILE)
6666
.offlineFlagSourcePath("PATH")
6767
.build());
6868
```
@@ -103,24 +103,25 @@ variables.
103103

104104
Given below are the supported configurations:
105105

106-
| Option name | Environment variable name | Type & Values | Default | Compatible resolver |
107-
| --------------------- | ------------------------------ | ------------------------ | --------- | ------------------- |
108-
| resolver | FLAGD_RESOLVER | String - rpc, in-process | rpc | |
109-
| host | FLAGD_HOST | String | localhost | rpc & in-process |
110-
| port | FLAGD_PORT | int | 8013 | rpc & in-process |
111-
| targetUri | FLAGD_TARGET_URI | string | null | rpc & in-process |
112-
| tls | FLAGD_TLS | boolean | false | rpc & in-process |
113-
| socketPath | FLAGD_SOCKET_PATH | String | null | rpc & in-process |
114-
| certPath | FLAGD_SERVER_CERT_PATH | String | null | rpc & in-process |
115-
| deadline | FLAGD_DEADLINE_MS | int | 500 | rpc & in-process |
116-
| streamDeadlineMs | FLAGD_STREAM_DEADLINE_MS | int | 600000 | rpc & in-process |
117-
| keepAliveTime | FLAGD_KEEP_ALIVE_TIME_MS | long | 0 | rpc & in-process |
118-
| selector | FLAGD_SOURCE_SELECTOR | String | null | in-process |
119-
| cache | FLAGD_CACHE | String - lru, disabled | lru | rpc |
120-
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | rpc |
121-
| maxEventStreamRetries | FLAGD_MAX_EVENT_STREAM_RETRIES | int | 5 | rpc |
122-
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | int | 1000 | rpc |
123-
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | String | null | in-process |
106+
| Option name | Environment variable name | Type & Values | Default | Compatible resolver |
107+
|-----------------------|--------------------------------|--------------------------|-----------|-------------------------|
108+
| resolver | FLAGD_RESOLVER | String - rpc, in-process | rpc | |
109+
| host | FLAGD_HOST | String | localhost | rpc & in-process |
110+
| port | FLAGD_PORT | int | 8013 | rpc & in-process |
111+
| targetUri | FLAGD_TARGET_URI | string | null | rpc & in-process |
112+
| tls | FLAGD_TLS | boolean | false | rpc & in-process |
113+
| socketPath | FLAGD_SOCKET_PATH | String | null | rpc & in-process |
114+
| certPath | FLAGD_SERVER_CERT_PATH | String | null | rpc & in-process |
115+
| deadline | FLAGD_DEADLINE_MS | int | 500 | rpc & in-process & file |
116+
| streamDeadlineMs | FLAGD_STREAM_DEADLINE_MS | int | 600000 | rpc & in-process |
117+
| keepAliveTime | FLAGD_KEEP_ALIVE_TIME_MS | long | 0 | rpc & in-process |
118+
| selector | FLAGD_SOURCE_SELECTOR | String | null | in-process |
119+
| cache | FLAGD_CACHE | String - lru, disabled | lru | rpc |
120+
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | rpc |
121+
| maxEventStreamRetries | FLAGD_MAX_EVENT_STREAM_RETRIES | int | 5 | rpc |
122+
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | int | 1000 | rpc |
123+
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | String | null | file |
124+
| offlinePollIntervalMs | FLAGD_OFFLINE_POLL_MS | int | 5000 | file |
124125

125126
> [!NOTE]
126127
> Some configurations are only applicable for RPC resolver.

providers/flagd/schemas

providers/flagd/spec

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/Config.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public final class Config {
1717
static final int DEFAULT_STREAM_DEADLINE_MS = 10 * 60 * 1000;
1818
static final int DEFAULT_STREAM_RETRY_GRACE_PERIOD = 5;
1919
static final int DEFAULT_MAX_CACHE_SIZE = 1000;
20+
static final int DEFAULT_OFFLINE_POLL_MS = 5000;
2021
static final long DEFAULT_KEEP_ALIVE = 0;
2122

2223
static final String RESOLVER_ENV_VAR = "FLAGD_RESOLVER";
@@ -33,13 +34,15 @@ public final class Config {
3334
static final String STREAM_DEADLINE_MS_ENV_VAR_NAME = "FLAGD_STREAM_DEADLINE_MS";
3435
static final String SOURCE_SELECTOR_ENV_VAR_NAME = "FLAGD_SOURCE_SELECTOR";
3536
static final String OFFLINE_SOURCE_PATH = "FLAGD_OFFLINE_FLAG_SOURCE_PATH";
37+
static final String OFFLINE_POLL_MS = "FLAGD_OFFLINE_POLL_MS";
3638
static final String KEEP_ALIVE_MS_ENV_VAR_NAME_OLD = "FLAGD_KEEP_ALIVE_TIME";
3739
static final String KEEP_ALIVE_MS_ENV_VAR_NAME = "FLAGD_KEEP_ALIVE_TIME_MS";
3840
static final String TARGET_URI_ENV_VAR_NAME = "FLAGD_TARGET_URI";
3941
static final String STREAM_RETRY_GRACE_PERIOD = "FLAGD_RETRY_GRACE_PERIOD";
4042

4143
static final String RESOLVER_RPC = "rpc";
4244
static final String RESOLVER_IN_PROCESS = "in-process";
45+
static final String RESOLVER_FILE = "file";
4346

4447
public static final String STATIC_REASON = "STATIC";
4548
public static final String CACHED_REASON = "CACHED";
@@ -87,6 +90,8 @@ static Resolver fromValueProvider(Function<String, String> provider) {
8790
return Resolver.IN_PROCESS;
8891
case "rpc":
8992
return Resolver.RPC;
93+
case "file":
94+
return Resolver.FILE;
9095
default:
9196
log.warn("Unsupported resolver variable: {}", resolverVar);
9297
return DEFAULT_RESOLVER_TYPE;
@@ -143,6 +148,11 @@ public String asString() {
143148
public String asString() {
144149
return RESOLVER_IN_PROCESS;
145150
}
151+
},
152+
FILE {
153+
public String asString() {
154+
return RESOLVER_FILE;
155+
}
146156
}
147157
}
148158
}

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdOptions.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.function.Function;
1313
import lombok.Builder;
1414
import lombok.Getter;
15+
import org.apache.commons.lang3.StringUtils;
1516

1617
/**
1718
* FlagdOptions is a builder to build flagd provider options.
@@ -119,8 +120,14 @@ public class FlagdOptions {
119120
* File source of flags to be used by offline mode.
120121
* Setting this enables the offline mode of the in-process provider.
121122
*/
123+
private String offlineFlagSourcePath;
124+
125+
/**
126+
* File polling interval.
127+
* Defaults to 0 (disabled).
128+
**/
122129
@Builder.Default
123-
private String offlineFlagSourcePath = fallBackToEnvOrDefault(Config.OFFLINE_SOURCE_PATH, null);
130+
private int offlinePollIntervalMs = fallBackToEnvOrDefault(Config.OFFLINE_POLL_MS, Config.DEFAULT_OFFLINE_POLL_MS);
124131

125132
/**
126133
* gRPC custom target string.
@@ -193,7 +200,20 @@ void prebuild() {
193200
resolverType = fromValueProvider(System::getenv);
194201
}
195202

196-
if (port == 0) {
203+
if (StringUtils.isBlank(offlineFlagSourcePath)) {
204+
offlineFlagSourcePath = fallBackToEnvOrDefault(Config.OFFLINE_SOURCE_PATH, null);
205+
}
206+
207+
if (!StringUtils.isEmpty(offlineFlagSourcePath) && resolverType == Config.Resolver.IN_PROCESS) {
208+
resolverType = Config.Resolver.FILE;
209+
}
210+
211+
// We need a file path for FILE Provider
212+
if (StringUtils.isEmpty(offlineFlagSourcePath) && resolverType == Config.Resolver.FILE) {
213+
throw new IllegalArgumentException("Resolver Type 'FILE' requires a offlineFlagSourcePath");
214+
}
215+
216+
if (port == 0 && resolverType != Config.Resolver.FILE) {
197217
port = Integer.parseInt(
198218
fallBackToEnvOrDefault(Config.PORT_ENV_VAR_NAME, determineDefaultPortForResolver()));
199219
}

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public FlagdProvider() {
8080
*/
8181
public FlagdProvider(final FlagdOptions options) {
8282
switch (options.getResolverType().asString()) {
83+
case Config.RESOLVER_FILE:
8384
case Config.RESOLVER_IN_PROCESS:
8485
this.flagResolver = new InProcessResolver(options, this::onProviderEvent);
8586
break;

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/grpc/GrpcResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public GrpcResolver(
7272
Evaluation.EventStreamRequest.getDefaultInstance(),
7373
new EventStreamObserver(
7474
(flags) -> {
75-
if (cache != null) {
76-
flags.forEach(cache::remove);
75+
if (this.cache != null) {
76+
flags.forEach(this.cache::remove);
7777
}
7878
onProviderEvent.accept(new FlagdProviderEvent(
7979
ProviderEvent.PROVIDER_CONFIGURATION_CHANGED, flags));

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/process/InProcessResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static Connector getConnector(final FlagdOptions options, Consumer<FlagdProvider
151151
}
152152
return options.getOfflineFlagSourcePath() != null
153153
&& !options.getOfflineFlagSourcePath().isEmpty()
154-
? new FileConnector(options.getOfflineFlagSourcePath())
154+
? new FileConnector(options.getOfflineFlagSourcePath(), options.getOfflinePollIntervalMs())
155155
: new GrpcStreamConnector(options, onConnectionEvent);
156156
}
157157

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/process/storage/connector/file/FileConnector.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
@Slf4j
2525
public class FileConnector implements Connector {
2626

27-
private static final int POLL_INTERVAL_MS = 5000;
2827
private static final String OFFER_WARN = "Unable to offer file content to queue: queue is full";
2928

3029
private final String flagSourcePath;
30+
private final int pollInterval;
3131
private final BlockingQueue<QueuePayload> queue = new LinkedBlockingQueue<>(1);
3232
private boolean shutdown = false;
3333

34-
public FileConnector(final String flagSourcePath) {
34+
public FileConnector(final String flagSourcePath, int pollInterval) {
3535
this.flagSourcePath = flagSourcePath;
36+
this.pollInterval = pollInterval;
3637
}
3738

3839
/**
@@ -64,7 +65,7 @@ public void init() throws IOException {
6465
}
6566
}
6667

67-
Thread.sleep(POLL_INTERVAL_MS);
68+
Thread.sleep(pollInterval);
6869
}
6970

7071
log.info("Shutting down file connector.");

0 commit comments

Comments
 (0)