Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion providers/openfeature-provider-flagd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The default options can be defined in the FlagdProvider constructor.
| cert_path | FLAGD_SERVER_CERT_PATH | String | null | rpc & in-process |
| deadline | FLAGD_DEADLINE_MS | int | 500 | rpc & in-process |
| stream_deadline_ms | FLAGD_STREAM_DEADLINE_MS | int | 600000 | rpc & in-process |
| keep_alive_time | FLAGD_KEEP_ALIVE_TIME_MS | int | 0 | rpc & in-process |
| keep_alive_time | FLAGD_KEEP_ALIVE_TIME_MS | int | 600000 | rpc & in-process |
| selector | FLAGD_SOURCE_SELECTOR | str | null | in-process |
| cache_type | FLAGD_CACHE | enum - `lru`, `disabled` | lru | rpc |
| max_cache_size | FLAGD_MAX_CACHE_SIZE | int | 1000 | rpc |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class CacheType(Enum):
DEFAULT_CACHE_SIZE = 1000
DEFAULT_DEADLINE = 500
DEFAULT_HOST = "localhost"
DEFAULT_KEEP_ALIVE = 0
# 10 minutes in ms. Must stay above flagd's grpc-go server MinPingInterval (5 minutes),
# otherwise the server rejects the connection with GOAWAY (ENHANCE_YOUR_CALM, "too_many_pings").
# A value of 0 (or any non-positive value) is never passed to grpc.keepalive_time_ms;
# the channel then uses the gRPC library's default keepalive behavior instead.
DEFAULT_KEEP_ALIVE = 600000
DEFAULT_OFFLINE_SOURCE_PATH: str | None = None
DEFAULT_OFFLINE_POLL_MS = 5000
DEFAULT_PORT_IN_PROCESS = 8015
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def _generate_channel(self, config: Config) -> grpc.Channel:
target = f"{config.host}:{config.port}"
# Create the channel with the service config
options = [
("grpc.keepalive_time_ms", config.keep_alive_time),
("grpc.initial_reconnect_backoff_ms", config.retry_backoff_ms),
("grpc.max_reconnect_backoff_ms", config.retry_backoff_max_ms),
("grpc.min_reconnect_backoff_ms", config.deadline_ms),
Expand Down Expand Up @@ -117,6 +116,13 @@ def _generate_channel(self, config: Config) -> grpc.Channel:
),
),
]
if config.keep_alive_time > 0:
# Only send keepalive pings when a positive interval is configured.
# A value of 0 makes the gRPC C-core ping continuously, which flagd's
# grpc-go server rejects with GOAWAY (ENHANCE_YOUR_CALM, "too_many_pings");
# omitting the option defers to the gRPC library's default instead.
options.append(("grpc.keepalive_time_ms", config.keep_alive_time))

if config.tls:
credentials = grpc.ssl_channel_credentials()
if config.cert_path:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def _generate_channel(self, config: Config) -> grpc.Channel:
target = f"{config.host}:{config.port}"
# Create the channel with the service config
options: list[tuple[str, typing.Any]] = [
("grpc.keepalive_time_ms", config.keep_alive_time),
("grpc.initial_reconnect_backoff_ms", config.retry_backoff_ms),
("grpc.max_reconnect_backoff_ms", config.retry_backoff_max_ms),
("grpc.min_reconnect_backoff_ms", config.stream_deadline_ms),
Expand Down Expand Up @@ -94,6 +93,13 @@ def _generate_channel(self, config: Config) -> grpc.Channel:
),
),
]
if config.keep_alive_time > 0:
# Only send keepalive pings when a positive interval is configured.
# A value of 0 makes the gRPC C-core ping continuously, which flagd's
# grpc-go server rejects with GOAWAY (ENHANCE_YOUR_CALM, "too_many_pings");
# omitting the option defers to the gRPC library's default instead.
options.append(("grpc.keepalive_time_ms", config.keep_alive_time))

if config.default_authority is not None:
options.append(("grpc.default_authority", config.default_authority))

Expand Down
6 changes: 6 additions & 0 deletions providers/openfeature-provider-flagd/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def test_return_default_values_in_process():
assert config.tls is DEFAULT_TLS


def test_default_keep_alive_time_respects_flagd_min_ping_interval():
# flagd's grpc-go server enforces a MinPingInterval of 5 minutes; any client
# keepalive interval below that triggers GOAWAY (ENHANCE_YOUR_CALM, "too_many_pings").
assert DEFAULT_KEEP_ALIVE == 600000


@pytest.fixture(params=ResolverType, scope="module")
def resolver_type(request):
return request.param
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import typing
from unittest.mock import MagicMock, patch

import pytest

from openfeature.contrib.provider.flagd.config import DEFAULT_KEEP_ALIVE, Config
from openfeature.contrib.provider.flagd.resolvers.grpc import GrpcResolver
from openfeature.contrib.provider.flagd.resolvers.process.connector.grpc_watcher import (
GrpcWatcher,
)

RESOLVER_CLASSES = [GrpcResolver, GrpcWatcher]


@pytest.fixture(params=RESOLVER_CLASSES, ids=["rpc", "in-process"])
def generate_channel(request):
"""Generates a grpc channel for the resolver under test and returns the
options that were passed to grpc.insecure_channel."""
captured: list[list[tuple[str, typing.Any]]] = []

def fake_insecure_channel(target, options=None, **kwargs):
captured.append(list(options or []))
return MagicMock()

def generate(config: Config) -> list[tuple[str, typing.Any]]:
# `_generate_channel` only uses `config`, so the instance can be omitted.
with patch("grpc.insecure_channel", side_effect=fake_insecure_channel):
request.param._generate_channel(None, config)
return captured[-1]

return generate


def test_default_keep_alive_time_is_above_flagd_min_ping_interval(generate_channel):
# flagd's grpc-go server enforces a MinPingInterval of 5 minutes; a more
# frequent ping interval triggers GOAWAY (ENHANCE_YOUR_CALM, "too_many_pings").
options = generate_channel(Config())
assert [o for o in options if o[0] == "grpc.keepalive_time_ms"] == [
("grpc.keepalive_time_ms", DEFAULT_KEEP_ALIVE)
]
assert DEFAULT_KEEP_ALIVE >= 5 * 60 * 1000


def test_zero_keep_alive_time_does_not_send_immediate_pings(generate_channel):
# The gRPC C-core interprets keepalive_time_ms=0 as "send pings immediately",
# so the option must be omitted (deferring to the gRPC default) instead.
options = generate_channel(Config(keep_alive_time=0))
assert all(o[0] != "grpc.keepalive_time_ms" for o in options)


def test_custom_keep_alive_time_is_passed_to_grpc(generate_channel):
options = generate_channel(Config(keep_alive_time=10000))
assert ("grpc.keepalive_time_ms", 10000) in options
Loading