Skip to content

Commit

Permalink
fixup: add readme and missing option
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Schrottner <[email protected]>
  • Loading branch information
aepfli committed Feb 7, 2025
1 parent a7f96d9 commit f852866
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 30 deletions.
1 change: 1 addition & 0 deletions providers/flagd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmp/
41 changes: 21 additions & 20 deletions providers/flagd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ The value is updated with every (re)connection to the sync implementation.
This can be used to enrich evaluations with such data.
If the `in-process` mode is not used, and before the provider is ready, the `getSyncMetadata` returns an empty map.

#### Offline mode
### Offline mode (File resolver)

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

```java
FlagdProvider flagdProvider = new FlagdProvider(
FlagdOptions.builder()
.resolverType(Config.Resolver.IN_PROCESS)
.resolverType(Config.Resolver.FILE)
.offlineFlagSourcePath("PATH")
.build());
```
Expand Down Expand Up @@ -103,24 +103,25 @@ variables.

Given below are the supported configurations:

| Option name | Environment variable name | Type & Values | Default | Compatible resolver |
| --------------------- | ------------------------------ | ------------------------ | --------- | ------------------- |
| resolver | FLAGD_RESOLVER | String - rpc, in-process | rpc | |
| host | FLAGD_HOST | String | localhost | rpc & in-process |
| port | FLAGD_PORT | int | 8013 | rpc & in-process |
| targetUri | FLAGD_TARGET_URI | string | null | rpc & in-process |
| tls | FLAGD_TLS | boolean | false | rpc & in-process |
| socketPath | FLAGD_SOCKET_PATH | String | null | rpc & in-process |
| certPath | FLAGD_SERVER_CERT_PATH | String | null | rpc & in-process |
| deadline | FLAGD_DEADLINE_MS | int | 500 | rpc & in-process |
| streamDeadlineMs | FLAGD_STREAM_DEADLINE_MS | int | 600000 | rpc & in-process |
| keepAliveTime | FLAGD_KEEP_ALIVE_TIME_MS | long | 0 | rpc & in-process |
| selector | FLAGD_SOURCE_SELECTOR | String | null | in-process |
| cache | FLAGD_CACHE | String - lru, disabled | lru | rpc |
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | rpc |
| maxEventStreamRetries | FLAGD_MAX_EVENT_STREAM_RETRIES | int | 5 | rpc |
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | int | 1000 | rpc |
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | String | null | in-process |
| Option name | Environment variable name | Type & Values | Default | Compatible resolver |
|-----------------------|--------------------------------|--------------------------|-----------|-------------------------|
| resolver | FLAGD_RESOLVER | String - rpc, in-process | rpc | |
| host | FLAGD_HOST | String | localhost | rpc & in-process |
| port | FLAGD_PORT | int | 8013 | rpc & in-process |
| targetUri | FLAGD_TARGET_URI | string | null | rpc & in-process |
| tls | FLAGD_TLS | boolean | false | rpc & in-process |
| socketPath | FLAGD_SOCKET_PATH | String | null | rpc & in-process |
| certPath | FLAGD_SERVER_CERT_PATH | String | null | rpc & in-process |
| deadline | FLAGD_DEADLINE_MS | int | 500 | rpc & in-process & file |
| streamDeadlineMs | FLAGD_STREAM_DEADLINE_MS | int | 600000 | rpc & in-process |
| keepAliveTime | FLAGD_KEEP_ALIVE_TIME_MS | long | 0 | rpc & in-process |
| selector | FLAGD_SOURCE_SELECTOR | String | null | in-process |
| cache | FLAGD_CACHE | String - lru, disabled | lru | rpc |
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | rpc |
| maxEventStreamRetries | FLAGD_MAX_EVENT_STREAM_RETRIES | int | 5 | rpc |
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | int | 1000 | rpc |
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | String | null | file |
| offlinePollIntervalMs | FLAGD_OFFLINE_POLL_MS | int | 5000 | file |

> [!NOTE]
> Some configurations are only applicable for RPC resolver.
Expand Down
2 changes: 1 addition & 1 deletion providers/flagd/schemas
2 changes: 1 addition & 1 deletion providers/flagd/spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class Config {
static final int DEFAULT_STREAM_DEADLINE_MS = 10 * 60 * 1000;
static final int DEFAULT_STREAM_RETRY_GRACE_PERIOD = 5;
static final int DEFAULT_MAX_CACHE_SIZE = 1000;
static final int DEFAULT_OFFLINE_POLL_MS = 5000;
static final long DEFAULT_KEEP_ALIVE = 0;

static final String RESOLVER_ENV_VAR = "FLAGD_RESOLVER";
Expand All @@ -33,6 +34,7 @@ public final class Config {
static final String STREAM_DEADLINE_MS_ENV_VAR_NAME = "FLAGD_STREAM_DEADLINE_MS";
static final String SOURCE_SELECTOR_ENV_VAR_NAME = "FLAGD_SOURCE_SELECTOR";
static final String OFFLINE_SOURCE_PATH = "FLAGD_OFFLINE_FLAG_SOURCE_PATH";
static final String OFFLINE_POLL_MS = "FLAGD_OFFLINE_POLL_MS";
static final String KEEP_ALIVE_MS_ENV_VAR_NAME_OLD = "FLAGD_KEEP_ALIVE_TIME";
static final String KEEP_ALIVE_MS_ENV_VAR_NAME = "FLAGD_KEEP_ALIVE_TIME_MS";
static final String TARGET_URI_ENV_VAR_NAME = "FLAGD_TARGET_URI";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ public class FlagdOptions {
*/
private String offlineFlagSourcePath;

/**
* File polling interval.
* Defaults to 0 (disabled).
**/
@Builder.Default
private int offlinePollIntervalMs = fallBackToEnvOrDefault(Config.OFFLINE_POLL_MS, Config.DEFAULT_OFFLINE_POLL_MS);

/**
* gRPC custom target string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static Connector getConnector(final FlagdOptions options, Consumer<FlagdProvider
}
return options.getOfflineFlagSourcePath() != null
&& !options.getOfflineFlagSourcePath().isEmpty()
? new FileConnector(options.getOfflineFlagSourcePath())
? new FileConnector(options.getOfflineFlagSourcePath(), options.getOfflinePollIntervalMs())
: new GrpcStreamConnector(options, onConnectionEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
@Slf4j
public class FileConnector implements Connector {

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

private final String flagSourcePath;
private final int pollInterval;
private final BlockingQueue<QueuePayload> queue = new LinkedBlockingQueue<>(1);
private boolean shutdown = false;

public FileConnector(final String flagSourcePath) {
public FileConnector(final String flagSourcePath, int pollInterval) {
this.flagSourcePath = flagSourcePath;
this.pollInterval = pollInterval;
}

/**
Expand Down Expand Up @@ -64,7 +65,7 @@ public void init() throws IOException {
}
}

Thread.sleep(POLL_INTERVAL_MS);
Thread.sleep(pollInterval);
}

log.info("Shutting down file connector.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class ConfigSteps extends AbstractSteps {
*/
public static final List<String> IGNORED_FOR_NOW = new ArrayList<String>() {
{
add("offlinePollIntervalMs");
add("retryBackoffMaxMs");
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FileConnectorTest {
@Test
void readAndExposeFeatureFlagsFromSource() throws IOException {
// given
final FileConnector connector = new FileConnector(getResourcePath(VALID_LONG));
final FileConnector connector = new FileConnector(getResourcePath(VALID_LONG), 5000);

// when
connector.init();
Expand All @@ -45,7 +45,7 @@ void readAndExposeFeatureFlagsFromSource() throws IOException {
@Test
void emitErrorStateForInvalidPath() throws IOException {
// given
final FileConnector connector = new FileConnector("INVALID_PATH");
final FileConnector connector = new FileConnector("INVALID_PATH", 5000);

// when
connector.init();
Expand Down Expand Up @@ -75,7 +75,7 @@ void watchForFileUpdatesAndEmitThem() throws IOException {
final Path updPath = Paths.get(getResourcePath(UPDATABLE_FILE));
Files.write(updPath, initial.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);

final FileConnector connector = new FileConnector(updPath.toString());
final FileConnector connector = new FileConnector(updPath.toString(), 5000);

// when
connector.init();
Expand Down

0 comments on commit f852866

Please sign in to comment.