Skip to content
Merged
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
14 changes: 11 additions & 3 deletions ldclient/impl/datasourcev2/async_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from urllib import parse

from ldclient.config import (
DEFAULT_POLL_INTERVAL,
DataSourceBuilder,
DataSourceBuilderConfig,
HTTPConfig
Expand Down Expand Up @@ -264,7 +265,10 @@ def base_uri(self, uri: str) -> 'AsyncPollingDataSourceBuilder':
return self

def poll_interval(self, poll_interval: float) -> 'AsyncPollingDataSourceBuilder':
"""Sets the polling interval for the AsyncPollingDataSource."""
"""
Sets the polling interval, in seconds. An interval below 30 seconds is raised to 30
seconds.
"""
self.__poll_interval = poll_interval
return self

Expand Down Expand Up @@ -297,7 +301,8 @@ def build(self, config: DataSourceBuilderConfig) -> AsyncPollingDataSource:
)

return AsyncPollingDataSource(
poll_interval=self.__poll_interval or config.poll_interval,
# The minimum also applies to an interval that is set on this builder.
poll_interval=max(self.__poll_interval or config.poll_interval, DEFAULT_POLL_INTERVAL),
requester=requester
)

Expand All @@ -319,7 +324,10 @@ def base_uri(self, uri: str) -> 'AsyncFallbackToFDv1PollingDataSourceBuilder':
return self

def poll_interval(self, poll_interval: float) -> 'AsyncFallbackToFDv1PollingDataSourceBuilder':
"""Sets the polling interval for the data source."""
"""
Sets the polling interval, in seconds. An interval below 30 seconds is raised to 30
seconds.
"""
self.__poll_interval = poll_interval
return self

Expand Down
14 changes: 11 additions & 3 deletions ldclient/impl/datasourcev2/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import urllib3

from ldclient.config import (
DEFAULT_POLL_INTERVAL,
DataSourceBuilder,
DataSourceBuilderConfig,
HTTPConfig
Expand Down Expand Up @@ -240,7 +241,10 @@ def base_uri(self, uri: str) -> 'PollingDataSourceBuilder':
return self

def poll_interval(self, poll_interval: float) -> 'PollingDataSourceBuilder':
"""Sets the polling interval for the PollingDataSource."""
"""
Sets the polling interval, in seconds. An interval below 30 seconds is raised to 30
seconds.
"""
self.__poll_interval = poll_interval
return self

Expand All @@ -267,7 +271,8 @@ def build(self, config: DataSourceBuilderConfig) -> PollingDataSource:
)

return PollingDataSource(
poll_interval=self.__poll_interval or config.poll_interval,
# The minimum also applies to an interval that is set on this builder.
poll_interval=max(self.__poll_interval or config.poll_interval, DEFAULT_POLL_INTERVAL),
requester=requester
)

Expand All @@ -288,7 +293,10 @@ def base_uri(self, uri: str) -> 'FallbackToFDv1PollingDataSourceBuilder':
return self

def poll_interval(self, poll_interval: float) -> 'FallbackToFDv1PollingDataSourceBuilder':
"""Sets the polling interval for the data source."""
"""
Sets the polling interval, in seconds. An interval below 30 seconds is raised to 30
seconds.
"""
self.__poll_interval = poll_interval
return self

Expand Down
8 changes: 6 additions & 2 deletions ldclient/impl/datasystem/async_fdv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ async def stop(self):
# Wait for the coordinator's background work to complete
await self._runner.stop_all(timeout=5.0)

self._data_source_status_provider.update_status(DataSourceState.OFF, None)

# Close the store
await self._store.close()

Expand Down Expand Up @@ -591,8 +593,10 @@ async def reader():
if update.state == DataSourceState.VALID and not set_on_ready.is_set():
set_on_ready.set()

# Update status
self._data_source_status_provider.update_status(update.state, update.error)
# A synchronizer that reports OFF has failed, but the data system goes on with the
# next synchronizer. The data system reports OFF only when it stops.
status_state = DataSourceState.INTERRUPTED if update.state == DataSourceState.OFF else update.state
self._data_source_status_provider.update_status(status_state, update.error)

# Check if we should fall back to FDv1 immediately. fallback_to_fdv1
# may ride along on a Valid update (payload + directive in the same
Expand Down
8 changes: 6 additions & 2 deletions ldclient/impl/datasystem/fdv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ def stop(self):
if thread.is_alive():
log.warning("Thread %s did not terminate in time", thread.name)

self._data_source_status_provider.update_status(DataSourceState.OFF, None)

# Close the store
self._store.close()

Expand Down Expand Up @@ -569,8 +571,10 @@ def reader(self: 'FDv2'):
if update.state == DataSourceState.VALID and not set_on_ready.is_set():
set_on_ready.set()

# Update status
self._data_source_status_provider.update_status(update.state, update.error)
# A synchronizer that reports OFF has failed, but the data system goes on with the
# next synchronizer. The data system reports OFF only when it stops.
status_state = DataSourceState.INTERRUPTED if update.state == DataSourceState.OFF else update.state
self._data_source_status_provider.update_status(status_state, update.error)

# Check if we should fall back to FDv1 immediately. fallback_to_fdv1
# may ride along on a Valid update (payload + directive in the same
Expand Down
5 changes: 5 additions & 0 deletions ldclient/impl/datasystem/fdv2_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def update_status(self, new_state: DataSourceState, new_error: Optional[DataSour
with self.__lock.write():
old_status = self.__status

# OFF is terminal. Work that is still in progress when the data system stops must not
# report a status after OFF.
if old_status.state == DataSourceState.OFF:
return

if new_state == DataSourceState.INTERRUPTED and old_status.state == DataSourceState.INITIALIZING:
new_state = DataSourceState.INITIALIZING

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
The FDv2 polling builders apply the same 30 second minimum poll interval as ``Config``. This
includes an interval that is set directly on a builder.
"""

import pytest

from ldclient.async_config import AsyncConfig
from ldclient.config import Config
from ldclient.impl.datasourcev2.async_polling import (
AsyncFallbackToFDv1PollingDataSourceBuilder,
AsyncPollingDataSourceBuilder
)
from ldclient.impl.datasourcev2.polling import (
FallbackToFDv1PollingDataSourceBuilder,
PollingDataSourceBuilder
)

SYNC_BUILDERS = [PollingDataSourceBuilder, FallbackToFDv1PollingDataSourceBuilder]
ASYNC_BUILDERS = [AsyncPollingDataSourceBuilder, AsyncFallbackToFDv1PollingDataSourceBuilder]


@pytest.mark.parametrize('builder_class', SYNC_BUILDERS)
@pytest.mark.parametrize('interval, expected', [(1, 30), (30, 30), (45, 45)])
def test_builder_interval_has_a_minimum(builder_class, interval, expected):
source = builder_class().poll_interval(interval).build(Config('sdk-key'))
assert source._poll_interval == expected


@pytest.mark.parametrize('builder_class', SYNC_BUILDERS)
def test_builder_uses_the_config_interval_when_none_is_set(builder_class):
source = builder_class().build(Config('sdk-key', poll_interval=60))
assert source._poll_interval == 60


@pytest.mark.parametrize('builder_class', ASYNC_BUILDERS)
@pytest.mark.parametrize('interval, expected', [(1, 30), (30, 30), (45, 45)])
def test_async_builder_interval_has_a_minimum(builder_class, interval, expected):
source = builder_class().poll_interval(interval).build(AsyncConfig('sdk-key'))
assert source._poll_interval == expected


@pytest.mark.parametrize('builder_class', ASYNC_BUILDERS)
def test_async_builder_uses_the_config_interval_when_none_is_set(builder_class):
source = builder_class().build(AsyncConfig('sdk-key', poll_interval=60))
assert source._poll_interval == 60
Loading
Loading