Skip to content

Commit 47a8b8a

Browse files
MridulTailorgemini-code-assist[bot]gruebel
authored
fix(flagd): use FLAGD_SYNC_PORT for in-process provider with backwards compatibility (#345)
* fix(flagd): use FLAGD_SYNC_PORT for in-process provider with backwards compatibility - Add ENV_VAR_SYNC_PORT constant for FLAGD_SYNC_PORT - Update port resolution: FLAGD_SYNC_PORT → FLAGD_PORT → default for in-process mode - Keep RPC mode using FLAGD_PORT only - Add 5 new tests covering sync port usage, fallback, priority, and RPC isolation - Update README documentation to reflect new environment variable Fixes #318 Signed-off-by: Mridul Tailor <mtailor729@gmail.com> * Update providers/openfeature-provider-flagd/README.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: Mridul Tailor <71081929+MridulTailor@users.noreply.github.com> * fix(flagd): simplify in-process port resolution logic Refactor port env var resolution for in-process mode to use os.environ.get() chaining instead of nested if/else, as suggested in code review. Logic and behavior are unchanged. Signed-off-by: Mridul Tailor <mtailor729@gmail.com> * fix(flagd): fix FILE resolver port regression and simplify logic FILE resolver was incorrectly defaulting to RPC port (8013) instead of in-process port (8015). Simplify port resolution using env_or_default helper to handle RPC, IN_PROCESS, and FILE resolver types correctly. Signed-off-by: Mridul Tailor <mtailor729@gmail.com> * fix(flagd): only apply FLAGD_SYNC_PORT env var for in-process resolver Signed-off-by: Mridul Tailor <mtailor729@gmail.com> * simplify conditions Signed-off-by: gruebel <anton.gruebel@gmail.com> --------- Signed-off-by: Mridul Tailor <mtailor729@gmail.com> Signed-off-by: Mridul Tailor <71081929+MridulTailor@users.noreply.github.com> Signed-off-by: gruebel <anton.gruebel@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: gruebel <anton.gruebel@gmail.com>
1 parent a7cc860 commit 47a8b8a

3 files changed

Lines changed: 71 additions & 21 deletions

File tree

‎providers/openfeature-provider-flagd/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ The default options can be defined in the FlagdProvider constructor.
8181
|--------------------------|--------------------------------|----------------------------|-------------------------------|---------------------|
8282
| resolver_type | FLAGD_RESOLVER | enum - `rpc`, `in-process` | rpc | |
8383
| host | FLAGD_HOST | str | localhost | rpc & in-process |
84-
| port | FLAGD_PORT | int | 8013 (rpc), 8015 (in-process) | rpc & in-process |
84+
| port | FLAGD_SYNC_PORT (in-process), FLAGD_PORT (rpc or fallback) | int | 8013 (rpc), 8015 (in-process) | rpc & in-process |
8585
| tls | FLAGD_TLS | bool | false | rpc & in-process |
8686
| cert_path | FLAGD_SERVER_CERT_PATH | String | null | rpc & in-process |
8787
| deadline | FLAGD_DEADLINE_MS | int | 500 | rpc & in-process |

‎providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/config.py‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def env_or_default(
8181

8282
@dataclasses.dataclass
8383
class Config:
84-
def __init__( # noqa: PLR0913
84+
def __init__( # noqa: PLR0913, PLR0915
8585
self,
8686
host: str | None = None,
8787
port: int | None = None,
@@ -166,25 +166,21 @@ def __init__( # noqa: PLR0913
166166
else resolver
167167
)
168168

169-
default_port = (
170-
DEFAULT_PORT_RPC
171-
if self.resolver is ResolverType.RPC
172-
else DEFAULT_PORT_IN_PROCESS
173-
)
174-
175-
self.port: int = (
176-
int(env_or_default(ENV_VAR_PORT, default_port, cast=int))
177-
if port is None
178-
else port
179-
)
180-
181-
self.port = (
182-
int(env_or_default(ENV_VAR_SYNC_PORT, self.port, cast=int))
183-
if sync_port is None and port is None
184-
else sync_port
185-
if sync_port is not None
186-
else self.port
187-
)
169+
if sync_port is not None:
170+
self.port = sync_port
171+
elif port is not None:
172+
self.port = port
173+
else:
174+
is_rpc = self.resolver is ResolverType.RPC
175+
# Use FLAGD_SYNC_PORT for in-process/file if set, otherwise fallback to FLAGD_PORT
176+
use_sync = not is_rpc and os.environ.get(ENV_VAR_SYNC_PORT)
177+
self.port = int(
178+
env_or_default(
179+
ENV_VAR_SYNC_PORT if use_sync else ENV_VAR_PORT,
180+
DEFAULT_PORT_RPC if is_rpc else DEFAULT_PORT_IN_PROCESS,
181+
cast=int,
182+
)
183+
)
188184

189185
self.offline_flag_source_path = (
190186
env_or_default(

‎providers/openfeature-provider-flagd/tests/test_config.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ENV_VAR_PORT,
2424
ENV_VAR_RETRY_BACKOFF_MS,
2525
ENV_VAR_STREAM_DEADLINE_MS,
26+
ENV_VAR_SYNC_PORT,
2627
ENV_VAR_TLS,
2728
CacheType,
2829
Config,
@@ -147,3 +148,56 @@ def test_uses_arguments_over_environments_and_defaults(monkeypatch, resolver_typ
147148
assert config.retry_backoff_ms == retry_backoff
148149
assert config.stream_deadline_ms == stream_deadline
149150
assert config.tls is tls
151+
152+
153+
def test_in_process_uses_sync_port(monkeypatch):
154+
"""Test that FLAGD_SYNC_PORT is used for in-process mode when set."""
155+
sync_port = 9999
156+
monkeypatch.setenv(ENV_VAR_SYNC_PORT, str(sync_port))
157+
158+
config = Config(resolver=ResolverType.IN_PROCESS)
159+
assert config.port == sync_port
160+
assert config.resolver == ResolverType.IN_PROCESS
161+
162+
163+
def test_in_process_fallback_to_port(monkeypatch):
164+
"""Test that FLAGD_PORT is used as fallback when FLAGD_SYNC_PORT is not set."""
165+
port = 7777
166+
monkeypatch.setenv(ENV_VAR_PORT, str(port))
167+
168+
config = Config(resolver=ResolverType.IN_PROCESS)
169+
assert config.port == port
170+
assert config.resolver == ResolverType.IN_PROCESS
171+
172+
173+
def test_in_process_sync_port_priority(monkeypatch):
174+
"""Test that FLAGD_SYNC_PORT takes priority over FLAGD_PORT when both are set."""
175+
sync_port = 9999
176+
port = 7777
177+
monkeypatch.setenv(ENV_VAR_SYNC_PORT, str(sync_port))
178+
monkeypatch.setenv(ENV_VAR_PORT, str(port))
179+
180+
config = Config(resolver=ResolverType.IN_PROCESS)
181+
assert config.port == sync_port # FLAGD_SYNC_PORT should win
182+
assert config.resolver == ResolverType.IN_PROCESS
183+
184+
185+
def test_rpc_ignores_sync_port(monkeypatch):
186+
"""Test that RPC mode uses FLAGD_PORT and ignores FLAGD_SYNC_PORT."""
187+
sync_port = 9999
188+
port = 7777
189+
monkeypatch.setenv(ENV_VAR_SYNC_PORT, str(sync_port))
190+
monkeypatch.setenv(ENV_VAR_PORT, str(port))
191+
192+
config = Config(resolver=ResolverType.RPC)
193+
assert config.port == port # RPC should use FLAGD_PORT
194+
assert config.resolver == ResolverType.RPC
195+
196+
197+
def test_in_process_default_port_when_no_env_var(monkeypatch):
198+
"""Test that in-process mode uses default port when neither env var is set."""
199+
monkeypatch.delenv(ENV_VAR_SYNC_PORT, raising=False)
200+
monkeypatch.delenv(ENV_VAR_PORT, raising=False)
201+
config = Config(resolver=ResolverType.IN_PROCESS)
202+
assert config.port == DEFAULT_PORT_IN_PROCESS
203+
assert config.resolver == ResolverType.IN_PROCESS

0 commit comments

Comments
 (0)