SDK configuration and flag retrieval
SDK configuration connects an application to flag sources and controls how the SDK loads, refreshes, and serves flag definitions. Retrievers read source data, the retriever manager combines it, and a shared cache makes the loaded state available to evaluation without reading the source for each request. See feature flag SDK for the wider client lifecycle.
Configuration contract
home/…/config.go defines the application-facing contract for SDK startup and runtime behavior. It covers flag sources, refresh timing, startup fallback, file format, logging, notifications, offline use, and shared evaluation data. The SDK normalizes these choices before it creates the retriever manager.
The main choices are:
| Concern | Effect |
|---|---|
Retriever and Retrievers | Select one or more flag sources. The singular source is read before the ordered list. |
FileFormat | Select the parser when a retriever does not declare its own format. YAML is the default. |
PollingInterval | Set the time between background refreshes. Zero becomes 60 seconds. A positive value below one second becomes one second. |
EnablePollingJitter | Vary each updater's interval by up to about 10 percent to reduce synchronized reads. |
StartWithRetrieverError | Allow startup with SDK defaults when the first source read fails. |
PersistentFlagConfigurationFile | Use a saved flag state when no source is available at startup. The saved state can be old. |
Offline | Skip source loading and event export. Evaluations use SDK default values. |
Environment and EvaluationContextEnrichment | Add shared data to evaluation input. Request values take priority over enrichment values with the same name. |
Config.GetRetrievers turns the singular and plural source fields into one ordered list. Config.Initialize supplies the context and polling defaults needed before startup.
References: config.go• L16-115 config.go• L125-173
Retriever selection
A retriever is a source adapter that returns the raw flag configuration. The SDK uses one common retriever contract for local files, HTTP endpoints, cloud storage, version-control services, databases, Redis, and Kubernetes configuration data. The adapter owns access to its source. The manager owns the common load and refresh flow. See flag retriever integrations for the source-specific integration view.
Retrievers that need setup can expose initialization and shutdown behavior. A retriever reports READY, NOT_READY, or ERROR. A source that is not ready does not contribute to a load. The manager also supports retrievers that provide an output format, so a source can override the global file format when needed.
References: retriever/retriever.go• L10-65 retriever/manager.go• L109-161
When several retrievers are configured, the manager reads them in parallel but merges their results in configuration order. A later definition for the same flag key replaces an earlier definition. Source order is therefore part of the flag contract.
References: config.go• L56-65 retriever/manager.go• L295-354
GitHub retrieval
The GitHub retriever reads a flag definition file from a repository branch and returns it to the normal retriever manager. It supports a personal access token for direct access and GitHub App authentication when access must use an app installation.
GitHub App authentication requires an app ID, an installation ID, and an RSA private key supplied as PEM text or a file. The retriever validates this set before it becomes ready. It exchanges a short-lived app token for an installation token, reuses that token while it is valid, and refreshes it before expiry. A failed authorization response causes one fresh-token retry, which helps recover from an expired cached token without changing the manager's load flow.
The retriever can target GitHub Enterprise through a base API URL. It also tracks API rate-limit headers and stops a request while the known limit is active. These controls protect background polling because the manager continues to treat GitHub as one source that either returns flag data or returns an error.
This diagram shows the GitHub App path from SDK retrieval to flag data.
flowchart LR
A[GitHub retriever] --> B[App JWT]
B --> C[Installation token]
C --> D[Repository contents request]
D --> E[Flag definition bytes]
E --> F[Retriever manager]
References: retriever/…/retriever.go• L27-80 retriever/…/retriever.go• L98-172 retriever/…/retriever.go• L190-276 retriever/…/jwt.go• L14-49 retriever/…/retriever_app_test.go• L179-310
MongoDB retrieval
The MongoDB retriever reads flag documents from a configured database collection and presents them as one JSON flag set. Each usable document supplies its flag key through the flag field. Documents without a string flag key are skipped and reported through the retriever logger. The JSON output format lets the manager use JSON parsing even when the global file format is different.
The retriever opens its database connection during initialization and reports readiness only after that setup succeeds. The manager can then use the same source contract for startup loads and background refreshes. Retrieval before initialization fails instead of returning an incomplete flag set.
References: retriever/…/retriever.go• L15-53 retriever/…/retriever.go• L60-111 retriever/…/retriever_unit_test.go• L11-29 retriever/…/retriever_test.go• L24-131
Redis retrieval
The Redis retriever treats matching Redis keys as individual flag definitions and combines them into one JSON flag set. An optional prefix limits the keys that belong to the SDK and is removed from each returned flag name. Each matching value must contain valid JSON so the manager receives one structured source result.
Redis connectivity is checked during initialization. A failed check leaves the source unavailable. Shutdown closes the client and returns the retriever to a not-ready state, which allows the manager to stop polling without using a closed connection. Retrieval before initialization fails clearly.
References: retriever/…/retriever.go• L15-71 retriever/…/retriever.go• L74-111 retriever/…/retriever_unit_test.go• L11-29 retriever/…/retriever_test.go• L20-80 retriever/…/retriever_test.go• L112-238
S3 folder retrieval
The S3 retriever reads either one object or a folder-like group of objects. Folder mode uses a bucket area, an optional file-name pattern, and a recursive choice to select the source files. It then presents the selected files as one flag set to the normal retriever manager.
The folder source follows these rules:
- It accepts JSON, YAML, and TOML files. Other file types are skipped with a warning.
- All selected files must use the same format.
- Files merge in sorted key order. A duplicate flag key is an error rather than an override.
- Object ETags act as versions. An unchanged object is not downloaded again.
- A deleted object remains in the loaded set until a successful full load replaces that state.
- The merged result uses the selected file format. An empty result uses JSON.
The S3 retriever validates that item mode and folder selection options are not mixed. It builds only the clients needed by the selected mode. Repeated initialization can keep the folder loader and its known object state when the selection and logger stay the same. Shutdown clears the clients and folder state before the retriever becomes unavailable.
This diagram shows how an S3 folder source becomes one retriever result.
flowchart LR
A[S3 bucket] --> B[Prefix and pattern selection]
B --> C[Paginated object listing]
C --> D[Changed supported files]
D --> E[Parse and merge]
E --> F[One encoded flag set]
F --> G[Retriever manager]
References: retriever/…/retriever.go• L31-53 retriever/…/retriever.go• L82-145 retriever/…/store.go• L13-46 retriever/…/retriever_test.go• L72-307 retriever/…/retriever_test.go• L435-582
Initial flag load
SDK construction creates the retriever manager and its cache, then performs the first load before it starts background polling. Each usable retriever returns bytes. The manager selects a parser, converts the bytes to flag definitions, merges the definitions, and replaces the cache with the resulting state.
This diagram shows how startup configuration reaches evaluation.
flowchart LR
A[SDK configuration] --> B[Ordered retrievers]
B --> C[Parallel source reads]
C --> D[Format selection and parsing]
D --> E[Ordered flag merge]
E --> F[Validated in-memory cache]
F --> G[Flag evaluation]
The global file format applies to ordinary retrievers. A retriever with a format hint can name its output format and override that choice. This allows sources with different formats to take part in one ordered load.
References: feature_flag.go• L56-119 retriever/manager.go• L189-213 retriever/manager.go• L317-366 internal/…/cache_manager.go• L56-71
The cache creates an internal flag object for each definition and keeps only valid definitions. An invalid flag does not enter the active cache. Other valid definitions can still serve. Each successful cache replacement records a refresh time and can trigger change notifications.
References: internal/…/cache_manager.go• L23-29 internal/…/cache_manager.go• L74-101 internal/…/in_memory_cache.go• L26-32 internal/…/in_memory_cache.go• L68-80
Startup fallback
The first source read decides whether normal SDK startup can complete. By default, a retrieval error returns an initialization error because the SDK has no trusted flag state to serve.
The fallback choices are:
PersistentFlagConfigurationFile: load the last stored state from local disk. This favors continuity over freshness.StartWithRetrieverError: start without loaded flags and return application defaults until a later source read succeeds. This favors startup over immediate flag availability.- Neither option: fail initialization when the first load fails.
A retriever initialization error is different from a failed flag retrieval. The manager records the source as failed and the SDK still reports the initialization error. StartWithRetrieverError applies to the first retrieval stage. It does not make an uninitialized source usable.
References: retriever/manager.go• L56-72 retriever/manager.go• L109-161 retriever/manager.go• L215-260 retriever/manager_test.go• L19-117
Background updates
After manager initialization, polling runs when the normalized interval is greater than zero. Each tick repeats source reads, parsing, ordered merging, and cache replacement. A failed later refresh is logged and does not replace the current cache with incomplete state.
An application can call ForceRefresh when it knows that a source changed. GetCacheRefreshDate reports the latest cache update time. This lets an application inspect loaded-state freshness without reading the source itself.
References: retriever/manager.go• L68-95 retriever/manager.go• L263-292 feature_flag.go• L177-202
Polling uses a stop signal and waits for the refresh loop to finish before the manager closes retrievers or the cache. Close therefore prevents a refresh from using a shut-down source or writing persistent state after shutdown.
References: retriever/background_updater.go• L8-45 retriever/manager.go• L97-107 retriever/manager.go• L164-186 feature_flag.go• L159-175
Offline mode stops polling when it is enabled. The default process-wide helper can invoke polling when the SDK returns online. The existing updater is not rebuilt after it has stopped, so an offline change is a lifecycle decision rather than a fully reversible network switch.
References: config.go• L190-208 feature_flag.go• L204-237
Flag definition delivery
The cache is the boundary between retrieval and evaluation. Retrieval produces source-shaped data. Parsing maps that data to flag definitions. Cache initialization converts each definition to the internal flag form and publishes a complete replacement under a lock. Evaluations therefore read one current cache state rather than a partly merged update.
Typed variation calls ask the manager for one flag. All-flag calls can ask for the complete cached set. The SDK passes the loaded flag with the request context, configured environment, common enrichment, and expected value type to the evaluation engine. The engine applies targeting and returns the selected value or the caller's default. See flag evaluation and targeting for decision rules and evaluation contexts for request data.
References: internal/…/cache_manager.go• L84-91 internal/…/cache_manager.go• L120-141 variation.go• L294-302 variation.go• L347-402 variation_all_flags.go• L31-82
The delivery path does not depend on the source type. A source can be replaced or layered without changing evaluation calls when it returns valid definitions in a supported format and keeps source order intentional.
References: retriever/manager.go• L295-366 internal/…/cache_manager.go• L56-101 variation.go• L294-402
PostgreSQL retriever lifecycle
The PostgreSQL retriever adapts a database table to the same source contract as other retrievers. It opens a checked database pool during initialization, selects flag names and JSON definitions, and returns one JSON object for the manager to parse. A configured flagset limits the query when the SDK uses a non-default flagset.
The pool registry shares one pool for each exact database URI. Each retriever adds one reference when it obtains the pool and releases it during shutdown. The pool closes and leaves the registry after the last user releases that URI. This allows several retrievers to reuse a pool without sharing pools across different URIs.
References: retriever/…/retriever.go• L22-57 retriever/…/retriever.go• L69-145 retriever/…/postgres.go• L10-64 retriever/…/postgres_test.go• L16-91
Change checklist
When these files or sections are changed, remember to consider these:
- Keep
Config.GetRetrieversordering aligned with the manager merge rule. A source-order change changes which definition wins. - Keep global format selection and source format hints aligned with every retriever that returns encoded data.
- Keep MongoDB document shape aligned with the JSON flag map expected by the manager. Changes to the
flagkey or its type change which documents reach evaluation. - Keep Redis key-prefix rules aligned with the names written by the producer. A prefix change can silently change the flag set.
- Keep Redis and MongoDB connection checks aligned with retriever readiness. A source must be initialized before polling or retrieval can use it.
- Keep S3 selection rules, same-format rules, ETag reuse, and duplicate-key errors aligned with the one-result contract expected by the manager.
- Recheck the distinction between retriever initialization errors and first-load errors when changing startup fallback or readiness handling.
- Preserve whole-cache replacement and refresh-time updates when changing parsing or cache publication.
- Recheck persistence and polling shutdown together. No refresh should write a fallback file after
Closereturns. - Keep the cache flag conversion contract aligned with the fields consumed by the evaluation engine.
- Keep PostgreSQL pool reference counting aligned with retriever initialization and shutdown. Each acquired URI reference must be released exactly once.
- Keep GitHub App fields complete and separate from personal access token fields. Changes to token expiry, retry, or rate-limit handling affect background refresh behavior.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
No Retriever and no Retrievers are configured | The SDK cannot load flags and initialization returns an error. | Configure at least one source before SDK creation. |
| Two sources define the same flag key | The later source silently replaces the earlier definition. | Review source order as part of configuration changes. |
| Source data does not match the selected parser | Parsing fails and the current refresh is not published. | Set FileFormat correctly or provide a format hint. |
| MongoDB cannot initialize its connection | The retriever reports an error and cannot provide flags. | Check the MongoDB URI and connection before SDK startup. |
A MongoDB document has no string flag field | The document is skipped and does not create a flag. | Store a string flag key in every flag document. |
| Redis cannot pass its connection check | The retriever remains unavailable and cannot provide flags. | Check Redis options and connectivity before SDK startup. |
| A Redis value is not valid JSON | The Redis retrieval fails and the refresh cannot publish its result. | Store one valid JSON flag definition per matching key. |
| The Redis prefix does not match producer keys | The source returns fewer flags or an empty flag set. | Use the same prefix when writing and reading Redis keys. |
| An S3 folder contains mixed supported formats | S3 retrieval fails before it returns a merged flag set. | Keep selected S3 files in one format. |
| Two selected S3 files define the same flag key | S3 retrieval fails instead of choosing one definition. | Use unique flag keys within an S3 folder source. |
| S3 item mode is combined with folder options | S3 initialization fails. | Use Item alone or use folder selection without Item. |
| A selected S3 object has no ETag | The object is downloaded again on each folder refresh. | Use an S3 source that returns stable ETags when download reduction matters. |
A retriever is not READY | That source contributes no flags to the load. | Complete initialization and monitor source status. |
| The first read fails without a fallback policy | SDK initialization fails. | Provide a reachable source or choose persistent state or StartWithRetrieverError. |
| A flag definition fails validation | That flag is omitted from the active cache. | Validate definitions before publication and inspect cache or logs. |
| Offline mode is enabled | Source refresh stops and evaluations return SDK defaults. | Enable offline mode only when default-value behavior is intended. |
| A PostgreSQL pool is released too many times or not released | A shared pool can close while another retriever uses it, or remain open after shutdown. | Pair each pool acquisition with one release for the same exact URI. |
| GitHub App authentication omits an app ID, installation ID, or usable RSA private key | The retriever fails initialization and cannot load repository flags. | Provide the complete app credential set through a PEM value or key file. |
| GitHub App fields are combined with a personal access token | Retriever initialization rejects the authentication setup. | Select one GitHub authentication mode. |
| GitHub returns an expired or rejected installation token | The retriever refreshes once on a 401; if the retry fails, the refresh does not publish new flags. | Keep app credentials and installation access valid. |
| GitHub rate-limit headers show no remaining calls before reset | Retrieval stops until the reset time and background updates keep the current cache. | Set a suitable polling interval and monitor GitHub API limits. |
Key files
| File | Why to read it |
|---|---|
| config.go | Defines public SDK configuration and source ordering. |
| feature_flag.go | Connects SDK construction to retriever management, refresh controls, and shutdown. |
| retriever/retriever.go | Defines the source contract, lifecycle hooks, format hints, and readiness states. |
| retriever/manager.go | Shows initial load, source merge, fallback, polling, cache access, and shutdown. |
| retriever/background_updater.go | Defines the timer and stop signal for background refresh. |
| internal/…/cache_manager.go | Defines parsing, cache replacement, refresh time, notifications, and persistence. |
| internal/…/in_memory_cache.go | Shows how loaded definitions become validated internal flag objects. |
| variation.go | Shows how a cached flag enters typed evaluation. |
| variation_all_flags.go | Shows how one or all cached flags reach all-flag evaluation. |
| retriever/…/retriever.go | Shows how MongoDB documents become one JSON flag source and how source readiness is managed. |
| retriever/…/retriever_test.go | Shows the MongoDB integration contract for document filtering, lifecycle, and JSON format. |
| retriever/…/retriever.go | Shows how Redis keys become one JSON flag source and how prefix and lifecycle rules work. |
| retriever/…/retriever_test.go | Shows the Redis integration contract for prefixes, JSON values, status, and shutdown. |
| retriever/…/retriever.go | Defines S3 item and folder modes and their lifecycle. |
| retriever/…/store.go | Defines the S3 listing and download boundary used by folder retrieval. |
| retriever/…/retriever_test.go | Shows the supported S3 selection, refresh, format, and lifecycle contract. |
| retriever/…/retriever.go | Illustrates a retriever that reads from a remote endpoint. |
| retriever/…/retriever.go | Illustrates a database retriever that selects a flagset and returns JSON. |
| retriever/…/postgres.go | Defines shared PostgreSQL pool ownership and release behavior. |
| retriever/…/retriever.go | Shows repository flag retrieval, authentication modes, token refresh, rate-limit handling, and GitHub Enterprise support. |
| retriever/…/jwt.go | Defines the short-lived GitHub App authentication token used to obtain an installation token. |