dynatrace-openfeature-provider-python 0.3.0__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/PKG-INFO +5 -1
  2. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/README.md +3 -0
  3. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/pyproject.toml +9 -1
  4. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/pyproject.toml.orig +8 -1
  5. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_cdn.py +28 -10
  6. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_cdn_config.py +40 -0
  7. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_polling.py +250 -0
  8. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_sse.py +157 -0
  9. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_sse_message.py +49 -0
  10. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/provider.py +159 -8
  11. dynatrace_openfeature_provider_python-0.3.0/src/dynatrace/openfeature/_polling.py +0 -140
  12. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/LICENSE +0 -0
  13. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/__init__.py +0 -0
  14. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_metrics_aggregator.py +0 -0
  15. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_metrics_flusher.py +0 -0
  16. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_metrics_hook.py +0 -0
  17. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_retry.py +0 -0
  18. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/options.py +0 -0
  19. {dynatrace_openfeature_provider_python-0.3.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dynatrace-openfeature-provider-python
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: The official Dynatrace OpenFeature provider for Python
5
5
  Keywords: openfeature,dynatrace,feature-flags,feature-management
6
6
  Author: Dynatrace
@@ -13,6 +13,7 @@ Requires-Dist: openfeature-sdk>=0.8.2
13
13
  Requires-Dist: openfeature-flagd-core>=2.0.0,<3.0.0
14
14
  Requires-Dist: requests>=2.27.0
15
15
  Requires-Dist: urllib3>=2.3.0
16
+ Requires-Dist: launchdarkly-eventsource>=1.7.3,<2.0.0
16
17
  Requires-Python: >=3.10
17
18
  Project-URL: Homepage, https://github.com/Dynatrace/openfeature-provider-python
18
19
  Description-Content-Type: text/markdown
@@ -23,6 +24,8 @@ The official [Dynatrace](https://dynatrace.com) [OpenFeature](https://openfeatur
23
24
 
24
25
  The provider evaluates flags in-process: on initialization it fetches your flag configuration from the Dynatrace CDN and refreshes it in the background, so evaluations are local and do not make a network call.
25
26
 
27
+ Changes are picked up through a server-sent events (SSE) stream advertised in the flag configuration: a change notification triggers an immediate re-fetch, and polling every 10 minutes acts as a safety net. While the stream is unavailable or disabled, the provider polls every 10 seconds instead.
28
+
26
29
  > **Status:** Early alpha — not yet ready for production use.
27
30
 
28
31
  ## Installation
@@ -71,6 +74,7 @@ api.set_provider(
71
74
  | --- | --- | --- | --- |
72
75
  | `config_origin` | `str` | derived from SDK key | Override the flag configuration origin. |
73
76
  | `metrics_origin` | `str` | derived from SDK key | Override the evaluation metrics ingest origin. |
77
+ | `disable_sse` | `bool` | `False` | Don't attempt an SSE connection and poll only, every 10 seconds. |
74
78
 
75
79
  ## Running the e2e tests
76
80
 
@@ -4,6 +4,8 @@ The official [Dynatrace](https://dynatrace.com) [OpenFeature](https://openfeatur
4
4
 
5
5
  The provider evaluates flags in-process: on initialization it fetches your flag configuration from the Dynatrace CDN and refreshes it in the background, so evaluations are local and do not make a network call.
6
6
 
7
+ Changes are picked up through a server-sent events (SSE) stream advertised in the flag configuration: a change notification triggers an immediate re-fetch, and polling every 10 minutes acts as a safety net. While the stream is unavailable or disabled, the provider polls every 10 seconds instead.
8
+
7
9
  > **Status:** Early alpha — not yet ready for production use.
8
10
 
9
11
  ## Installation
@@ -52,6 +54,7 @@ api.set_provider(
52
54
  | --- | --- | --- | --- |
53
55
  | `config_origin` | `str` | derived from SDK key | Override the flag configuration origin. |
54
56
  | `metrics_origin` | `str` | derived from SDK key | Override the evaluation metrics ingest origin. |
57
+ | `disable_sse` | `bool` | `False` | Don't attempt an SSE connection and poll only, every 10 seconds. |
55
58
 
56
59
  ## Running the e2e tests
57
60
 
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "dynatrace-openfeature-provider-python"
7
- version = "0.3.0"
7
+ version = "0.3.1"
8
8
  description = "The official Dynatrace OpenFeature provider for Python"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -25,6 +25,7 @@ dependencies = [
25
25
  "openfeature-flagd-core>=2.0.0,<3.0.0",
26
26
  "requests>=2.27.0",
27
27
  "urllib3>=2.3.0",
28
+ "launchdarkly-eventsource>=1.7.3,<2.0.0",
28
29
  ]
29
30
  requires-python = ">=3.10"
30
31
 
@@ -75,6 +76,13 @@ pretty = true
75
76
  strict = true
76
77
  disallow_any_generics = false
77
78
 
79
+ [[tool.mypy.overrides]]
80
+ module = [
81
+ "ld_eventsource",
82
+ "ld_eventsource.*",
83
+ ]
84
+ ignore_missing_imports = true
85
+
78
86
  [tool.ruff]
79
87
  target-version = "py310"
80
88
  exclude = [
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "dynatrace-openfeature-provider-python"
7
- version = "0.3.0"
7
+ version = "0.3.1"
8
8
  description = "The official Dynatrace OpenFeature provider for Python"
9
9
  readme = "README.md"
10
10
  authors = [{ name = "Dynatrace" }]
@@ -22,6 +22,8 @@ dependencies = [
22
22
  "requests>=2.27.0",
23
23
  # 2.3 added HTTPResponse.shutdown, which _cdn uses to enforce the fetch deadline
24
24
  "urllib3>=2.3.0",
25
+ # SSE client (ld_eventsource); the floor is the release _sse was built and verified against
26
+ "launchdarkly-eventsource>=1.7.3,<2.0.0",
25
27
  ]
26
28
  requires-python = ">=3.10"
27
29
 
@@ -63,6 +65,11 @@ pretty = true
63
65
  strict = true
64
66
  disallow_any_generics = false
65
67
 
68
+ [[tool.mypy.overrides]]
69
+ # ld_eventsource ships no type stubs or py.typed marker
70
+ module = ["ld_eventsource", "ld_eventsource.*"]
71
+ ignore_missing_imports = true
72
+
66
73
  [tool.ruff]
67
74
  target-version = "py310"
68
75
  exclude = [".git", ".venv", "__pycache__", "venv"]
@@ -51,6 +51,11 @@ class FetchResult:
51
51
  def is_success(self) -> bool:
52
52
  return 200 <= self.status < 300
53
53
 
54
+ @property
55
+ def last_modified_epoch_s(self) -> float | None:
56
+ parsed = _parse_http_date(self.last_modified)
57
+ return parsed.timestamp() if parsed is not None else None
58
+
54
59
 
55
60
  @dataclass
56
61
  class _CacheState:
@@ -76,6 +81,7 @@ class CdnFetcher:
76
81
  self._session = requests.Session()
77
82
  self._clock: Callable[[], float] = clock or time.time
78
83
  self._cache = _CacheState()
84
+ self._pending_cache: _CacheState | None = None
79
85
  self._rate_limited_until: float = 0.0
80
86
  self._fetch_lock = threading.RLock()
81
87
 
@@ -83,15 +89,27 @@ class CdnFetcher:
83
89
  t = now if now is not None else self._clock()
84
90
  return t < self._rate_limited_until
85
91
 
86
- def invalidate_cache(self) -> None:
87
- """Forget the cached validators.
92
+ def is_newer_than_committed(self, last_modified_epoch_s: float) -> bool:
93
+ """Whether a Last-Modified is newer than the config in force; true if that config is undated."""
94
+ committed = self._cache.last_modified_at
95
+ return committed is None or last_modified_epoch_s > committed.timestamp()
96
+
97
+ def commit(self) -> None:
98
+ """Promote the validators staged by the last successful fetch.
88
99
 
89
- They are stored as soon as a 2xx arrives, before the caller applies the
90
- body. If applying it fails they describe a config we never loaded, and
91
- the next conditional request would come back 304 over an empty store.
100
+ Call this only once the body has been applied. Until then the previous validators stay in
101
+ force, so a body that is empty or fails to apply is re-requested in full instead of being
102
+ answered with a 304.
92
103
  """
93
- with self._fetch_lock:
94
- self._cache = _CacheState()
104
+ pending = self._pending_cache
105
+ if pending is not None:
106
+ self._cache = pending
107
+ self._pending_cache = None
108
+
109
+ def invalidate_cache(self) -> None:
110
+ """Forget the validators, so the next fetch is unconditional."""
111
+ self._cache = _CacheState()
112
+ self._pending_cache = None
95
113
 
96
114
  def fetch(self, cancel: threading.Event | None = None) -> FetchResult:
97
115
  """Fetch the config, or skip without making a request.
@@ -100,8 +118,7 @@ class CdnFetcher:
100
118
  section 2.3) is neither success nor failure and must not be reported as
101
119
  either. PollingManager serialises its own fetches, so the in-flight
102
120
  guard is for callers outside it -- initialize() fetches on the caller's
103
- thread -- and it keeps a conditional request from storing an ETag and a
104
- Last-Modified that came from different responses.
121
+ thread.
105
122
  """
106
123
  if self.is_rate_limited():
107
124
  log.debug("rate limited; skipping fetch")
@@ -120,6 +137,7 @@ class CdnFetcher:
120
137
  self._fetch_lock.release()
121
138
 
122
139
  def _do_fetch(self) -> FetchResult:
140
+ self._pending_cache = None
123
141
  cache = self._cache
124
142
  headers: dict[str, str] = {}
125
143
  if cache.etag is not None:
@@ -170,7 +188,7 @@ class CdnFetcher:
170
188
  cache.last_modified_header,
171
189
  )
172
190
  return FetchResult(304, None, cache.etag, cache.last_modified_header)
173
- self._cache = _CacheState(
191
+ self._pending_cache = _CacheState(
174
192
  etag=new_etag,
175
193
  last_modified_header=new_last_modified,
176
194
  last_modified_at=new_lm_at,
@@ -0,0 +1,40 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import typing
5
+
6
+ #: ``eventStreams[].type`` value identifying the SSE change-notification stream
7
+ STREAM_TYPE_SSE = "sse"
8
+
9
+
10
+ def _non_blank(value: typing.Any) -> str | None:
11
+ return value if isinstance(value, str) and value.strip() else None
12
+
13
+
14
+ def extract_sse_url(config_json: str | None, fallback_origin: str | None) -> str | None:
15
+ """Return the SSE URL of the first ``sse`` entry in ``eventStreams``.
16
+ A non-blank ``url`` wins; else ``endpoint.requestUri`` on ``endpoint.origin`` or ``fallback_origin``."""
17
+ if not config_json or not config_json.strip():
18
+ return None
19
+ try:
20
+ config: typing.Any = json.loads(config_json)
21
+ except ValueError:
22
+ return None
23
+ streams = config.get("eventStreams") if isinstance(config, dict) else None
24
+ if not isinstance(streams, list):
25
+ return None
26
+ for stream in streams:
27
+ if not isinstance(stream, dict) or stream.get("type") != STREAM_TYPE_SSE:
28
+ continue
29
+ url = _non_blank(stream.get("url"))
30
+ if url is not None:
31
+ return url
32
+ endpoint = stream.get("endpoint")
33
+ if not isinstance(endpoint, dict):
34
+ return None
35
+ request_uri = _non_blank(endpoint.get("requestUri"))
36
+ if request_uri is None:
37
+ return None
38
+ origin = _non_blank(endpoint.get("origin")) or _non_blank(fallback_origin)
39
+ return origin + request_uri if origin is not None else None
40
+ return None
@@ -0,0 +1,250 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ import math
5
+ import threading
6
+ import time
7
+ from collections.abc import Callable
8
+
9
+ from ._cdn import FetchResult
10
+
11
+ log = logging.getLogger(__name__)
12
+
13
+ # stands in for an SSE announcement that carried no lastModified: greater than any real one, so never dropped
14
+ _LAST_MODIFIED_UNKNOWN = math.inf
15
+
16
+
17
+ class PollingManager:
18
+ """Polls on a fixed-rate schedule anchored to request initiation, not completion.
19
+
20
+ At most one fetch runs at a time (providers.md 2.1.1): a contended tick is skipped and emits
21
+ nothing; a contended SSE re-fetch is deferred and coalesced. set_interval switches cadence.
22
+ """
23
+
24
+ def __init__(
25
+ self,
26
+ fetch_flags: Callable[[threading.Event | None], FetchResult],
27
+ store_flags: Callable[[str], list[str]],
28
+ commit_fetch: Callable[[], None],
29
+ is_newer_than_applied: Callable[[float], bool],
30
+ emit_change: Callable[[list[str]], None],
31
+ emit_success: Callable[[], None],
32
+ emit_failure: Callable[[str], None],
33
+ interval_s: float,
34
+ ) -> None:
35
+ self._fetch_flags = fetch_flags
36
+ self._store_flags = store_flags
37
+ self._commit_fetch = commit_fetch
38
+ self._is_newer_than_applied = is_newer_than_applied
39
+ self._emit_change = emit_change
40
+ self._emit_success = emit_success
41
+ self._emit_failure = emit_failure
42
+ self._stop_event = threading.Event()
43
+ self._ticker_thread: threading.Thread | None = None
44
+ # guards everything below, and wakes the ticker when the interval changes or polling stops
45
+ self._cond = threading.Condition()
46
+ self._interval_s = interval_s
47
+ self._rescheduled = False
48
+ # set from submit until the fetch has settled (not until its thread exits), so no trigger defers behind a settled fetch
49
+ self._in_flight = False
50
+ # last-modified value of a deferred SSE re-fetch, or None when none is pending
51
+ self._pending_sse_refetch_at: float | None = None
52
+ # last-modified value of the current (in-flight) SSE-triggered fetch, or None if poll-triggered
53
+ self._in_flight_sse_refetch_at: float | None = None
54
+
55
+ @property
56
+ def interval_s(self) -> float:
57
+ with self._cond:
58
+ return self._interval_s
59
+
60
+ def start(self) -> None:
61
+ if self._ticker_thread is not None and self._ticker_thread.is_alive():
62
+ return
63
+ self._stop_event.clear()
64
+ self._ticker_thread = threading.Thread(
65
+ target=self._ticker_loop,
66
+ name="dt-provider-ticker",
67
+ daemon=True,
68
+ )
69
+ self._ticker_thread.start()
70
+
71
+ def stop(self) -> None:
72
+ # stop event doubles as the fetch cancel signal, so an in-flight poll abandons its backoff
73
+ self._stop_event.set()
74
+ with self._cond:
75
+ self._pending_sse_refetch_at = None
76
+ self._cond.notify_all()
77
+ if self._ticker_thread is not None:
78
+ self._ticker_thread.join(timeout=2.0)
79
+
80
+ def set_interval(self, interval_s: float) -> None:
81
+ """Change the poll cadence and re-anchor the schedule to now. No-op if unchanged."""
82
+ with self._cond:
83
+ if interval_s == self._interval_s:
84
+ return
85
+ self._interval_s = interval_s
86
+ self._rescheduled = True
87
+ self._cond.notify_all()
88
+
89
+ def request_out_of_band_fetch(
90
+ self, announced_last_modified: float | None = None
91
+ ) -> None:
92
+ """Fetch now if the slot is free, else defer and coalesce; never disturbs the cadence.
93
+ A deferred re-fetch is dropped only if the applied config is at least as new as announced."""
94
+ announced = (
95
+ _LAST_MODIFIED_UNKNOWN
96
+ if announced_last_modified is None
97
+ else announced_last_modified
98
+ )
99
+ with self._cond:
100
+ if self._stop_event.is_set():
101
+ return
102
+ if self._in_flight:
103
+ self._pending_sse_refetch_at = (
104
+ announced
105
+ if self._pending_sse_refetch_at is None
106
+ else max(self._pending_sse_refetch_at, announced)
107
+ )
108
+ log.debug(
109
+ "fetch in flight; deferring SSE re-fetch (lastModified=%s)",
110
+ announced_last_modified,
111
+ )
112
+ return
113
+ self._submit_locked(announced)
114
+
115
+ def _ticker_loop(self) -> None:
116
+ with self._cond:
117
+ next_tick = time.monotonic() + self._interval_s
118
+ while not self._stop_event.is_set():
119
+ if self._rescheduled:
120
+ # the polling interval changed
121
+ self._rescheduled = False
122
+ next_tick = time.monotonic() + self._interval_s
123
+ wait = next_tick - time.monotonic()
124
+ if wait > 0:
125
+ # woken early by set_interval or stop; the loop re-evaluates either way
126
+ self._cond.wait(wait)
127
+ continue
128
+ next_tick += self._interval_s
129
+ now = time.monotonic()
130
+ if next_tick < now:
131
+ next_tick = now + self._interval_s
132
+ self._on_tick_locked()
133
+
134
+ def _on_tick_locked(self) -> None:
135
+ if self._stop_event.is_set():
136
+ return
137
+ if self._in_flight:
138
+ # Neither failed nor succeeded: the tick learned nothing.
139
+ log.debug("poll tick skipped: a fetch is already in flight")
140
+ return
141
+ self._submit_locked(None)
142
+
143
+ def _submit_locked(self, sse_refetch_at: float | None) -> None:
144
+ self._in_flight_sse_refetch_at = sse_refetch_at
145
+ self._in_flight = True
146
+ threading.Thread(
147
+ target=self._run_fetches,
148
+ name="dt-provider-poller",
149
+ daemon=True,
150
+ ).start()
151
+
152
+ def _run_fetches(self) -> None:
153
+ # a deferred re-fetch runs on this same thread, so it queues behind the fetch rather than overlapping it
154
+ again = True
155
+ while again:
156
+ result: FetchResult | None = None
157
+ try:
158
+ result = self._poll()
159
+ finally:
160
+ with self._cond:
161
+ again = self._on_fetch_complete_locked(result)
162
+
163
+ def _on_fetch_complete_locked(self, completed: FetchResult | None) -> bool:
164
+ """Settle the SSE re-fetch bookkeeping; return True to issue the pending re-fetch now.
165
+ ``completed`` is the finished fetch's result, or None if it raised."""
166
+ issued_for = self._in_flight_sse_refetch_at
167
+ self._in_flight_sse_refetch_at = None
168
+ self._in_flight = False
169
+
170
+ if completed is not None and completed.skipped:
171
+ # no request was made, so a re-fetch we issued is still unsatisfied; put it back rather than re-issue it,
172
+ # which would just be skipped again (the Retry-After window or fetch lock still holds) and spin
173
+ if issued_for is not None:
174
+ self._pending_sse_refetch_at = (
175
+ issued_for
176
+ if self._pending_sse_refetch_at is None
177
+ else max(self._pending_sse_refetch_at, issued_for)
178
+ )
179
+ log.debug("SSE re-fetch made no request; keeping it pending")
180
+ return False
181
+
182
+ pending = self._pending_sse_refetch_at
183
+ if pending is None or self._stop_event.is_set():
184
+ return False
185
+ self._pending_sse_refetch_at = None
186
+
187
+ if pending != _LAST_MODIFIED_UNKNOWN and not self._is_newer_than_applied(
188
+ pending
189
+ ):
190
+ log.debug(
191
+ "dropping deferred SSE re-fetch: applied config is at least as new as %s",
192
+ pending,
193
+ )
194
+ return False
195
+ log.debug("issuing deferred SSE re-fetch")
196
+ self._in_flight_sse_refetch_at = pending
197
+ self._in_flight = True
198
+ return True
199
+
200
+ def _poll(self) -> FetchResult | None:
201
+ """Run one fetch and emit its outcome; return its result, or None if the fetch raised."""
202
+ cancel = self._stop_event
203
+ result: FetchResult | None = None
204
+ try:
205
+ result = self._fetch_flags(cancel)
206
+
207
+ if cancel.is_set():
208
+ log.debug("provider stopped during fetch; discarding result")
209
+ return result
210
+
211
+ self._handle_result(result)
212
+
213
+ except Exception as exc:
214
+ if cancel.is_set():
215
+ log.debug("poll abandoned during shutdown")
216
+ return result
217
+ log.warning("poll failed: %s", type(exc).__name__)
218
+ self._emit_failure(type(exc).__name__)
219
+ return result
220
+
221
+ def _handle_result(self, result: FetchResult) -> None:
222
+ if result.skipped:
223
+ log.debug("fetch skipped (single-flight); emitting nothing")
224
+ return
225
+
226
+ if result.is_not_modified:
227
+ log.debug("flags not modified")
228
+ self._emit_success()
229
+ return
230
+
231
+ if result.is_rate_limited:
232
+ log.debug("rate limited; serving cached values")
233
+ return
234
+
235
+ if not result.is_success:
236
+ self._emit_failure(f"HTTP {result.status}")
237
+ return
238
+
239
+ body = result.body
240
+ if not body:
241
+ self._emit_failure("empty response body")
242
+ return
243
+
244
+ changed_keys = self._store_flags(body)
245
+ self._commit_fetch()
246
+ self._emit_success()
247
+
248
+ if changed_keys:
249
+ log.info("flag configuration changed: %s", changed_keys)
250
+ self._emit_change(changed_keys)
@@ -0,0 +1,157 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ import threading
5
+ from collections.abc import Callable
6
+
7
+ from ld_eventsource import SSEClient
8
+ from ld_eventsource.actions import Event, Fault, Start
9
+ from ld_eventsource.config import ConnectStrategy, ErrorStrategy, RetryDelayStrategy
10
+ from urllib3.util import Timeout as Urllib3Timeout
11
+
12
+ from ._cdn import REQUEST_TIMEOUT_S
13
+ from ._sse_message import SseMessage
14
+
15
+ log = logging.getLogger(__name__)
16
+
17
+ # the stream is long-lived and idle between notifications, so only a dead connection should trip this
18
+ SSE_IDLE_TIMEOUT_S: float = 150
19
+ # reconnect backoff: doubles from the initial delay up to the max, with jitter
20
+ SSE_INITIAL_RETRY_DELAY_S: float = 1.0
21
+ SSE_MAX_RETRY_DELAY_S: float = 30.0
22
+ _SSE_RETRY_JITTER = 0.5
23
+
24
+
25
+ def mask_url(url: str) -> str:
26
+ """Mask the query string, which carries the Ably credentials, for safe logging."""
27
+ q = url.find("?")
28
+ return url[:q] + "?<redacted>" if q >= 0 else url
29
+
30
+
31
+ class SseManager:
32
+ """Manages the SSE connection to the Ably event stream (an opaque HTTP endpoint, no Ably SDK).
33
+
34
+ One daemon thread routes ``Start``, ``Fault`` and parsed ``Event`` actions to the callbacks;
35
+ reconnection and backoff are delegated to ``ld_eventsource``.
36
+ """
37
+
38
+ def __init__(
39
+ self,
40
+ sse_url: str,
41
+ on_connected: Callable[[], None],
42
+ on_disconnected: Callable[[], None],
43
+ on_message: Callable[[SseMessage], None],
44
+ ) -> None:
45
+ self._sse_url = sse_url
46
+ self._on_connected = on_connected
47
+ self._on_disconnected = on_disconnected
48
+ self._on_message = on_message
49
+ self._stopped = threading.Event()
50
+ self._client: SSEClient | None = None
51
+ self._thread: threading.Thread | None = None
52
+
53
+ @property
54
+ def url(self) -> str:
55
+ """The stream URL, credentials included; mask it before logging."""
56
+ return self._sse_url
57
+
58
+ def start(self) -> None:
59
+ """Open the stream on a daemon thread; returns without waiting for it to connect."""
60
+ if self._thread is not None:
61
+ return
62
+ self._client = SSEClient(
63
+ connect=ConnectStrategy.http(
64
+ self._sse_url,
65
+ urllib3_request_options={
66
+ "timeout": Urllib3Timeout(
67
+ connect=REQUEST_TIMEOUT_S, read=SSE_IDLE_TIMEOUT_S
68
+ )
69
+ },
70
+ ),
71
+ initial_retry_delay=SSE_INITIAL_RETRY_DELAY_S,
72
+ retry_delay_strategy=RetryDelayStrategy.default(
73
+ max_delay=SSE_MAX_RETRY_DELAY_S, jitter_multiplier=_SSE_RETRY_JITTER
74
+ ),
75
+ # never give up: every failure surfaces as a Fault and the client reconnects
76
+ error_strategy=ErrorStrategy.always_continue(),
77
+ )
78
+ self._thread = threading.Thread(
79
+ target=self._run, name="dt-provider-sse", daemon=True
80
+ )
81
+ self._thread.start()
82
+ log.info("SSE manager started (url=%s)", mask_url(self._sse_url))
83
+
84
+ def stop(self) -> None:
85
+ """Close the stream; callbacks stop at once. The library wakes a reader blocked mid-read
86
+ before closing the socket, so this returns promptly and is safe under a caller's lock."""
87
+ if self._stopped.is_set():
88
+ return
89
+ self._stopped.set()
90
+ client = self._client
91
+ if client is not None:
92
+ self._close_client(client)
93
+ log.info("SSE manager stopped")
94
+
95
+ def _close_client(self, client: SSEClient) -> None:
96
+ try:
97
+ # the library owns its pool, so close() also closes the pooled connection
98
+ # known limitation: on Windows ld_eventsource only releases the socket (left to GC, server sees no FIN);
99
+ # see launchdarkly/python-eventsource#74
100
+ client.close()
101
+ except Exception as exc:
102
+ log.warning("error closing SSE connection: %s", type(exc).__name__)
103
+
104
+ def _run(self) -> None:
105
+ client = self._client
106
+ if client is None:
107
+ return
108
+ try:
109
+ for action in client.all:
110
+ # the client reconnects even after close() if it was sleeping out a backoff
111
+ if self._stopped.is_set():
112
+ break
113
+ try:
114
+ self._dispatch(action)
115
+ except Exception:
116
+ # a failing callback must not kill the stream
117
+ log.warning("SSE callback failed", exc_info=True)
118
+ except Exception as exc:
119
+ if not self._stopped.is_set():
120
+ # always_continue keeps the iterator alive, so only a library fault lands here
121
+ log.warning("SSE reader stopped unexpectedly: %s", type(exc).__name__)
122
+ self._on_disconnected()
123
+ finally:
124
+ # closes a connection opened after stop() raced a pending reconnect
125
+ self._close_client(client)
126
+
127
+ def _dispatch(self, action: object) -> None:
128
+ if isinstance(action, Start):
129
+ log.info("SSE connection opened (url=%s)", mask_url(self._sse_url))
130
+ self._on_connected()
131
+ elif isinstance(action, Fault):
132
+ if action.error is None:
133
+ log.info("SSE connection closed")
134
+ else:
135
+ # type only: the message of a connect error can carry the credentialed URL
136
+ log.warning(
137
+ "SSE error (url=%s): %s",
138
+ mask_url(self._sse_url),
139
+ type(action.error).__name__,
140
+ )
141
+ self._on_disconnected()
142
+ elif isinstance(action, Event):
143
+ parsed = SseMessage.parse(action.data)
144
+ if parsed is None:
145
+ log.debug(
146
+ "SSE event received (event=%s) but payload unparseable; ignoring",
147
+ action.event,
148
+ )
149
+ return
150
+ log.debug(
151
+ "SSE event received (event=%s, type=%s, lastModified=%s)",
152
+ action.event,
153
+ parsed.type,
154
+ parsed.last_modified,
155
+ )
156
+ self._on_message(parsed)
157
+ # comments are keep-alives; ignore
@@ -0,0 +1,49 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import typing
5
+ from dataclasses import dataclass
6
+
7
+ #: server-side signal to re-fetch the flagd config JSON from the CDN
8
+ TYPE_REFETCH_CONFIG = "refetchConfig"
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class SseMessage:
13
+ """Parsed SSE message envelope per the SSE message format spec.
14
+
15
+ Callers must ignore unknown ``type``s; ``etag`` is parsed for completeness only.
16
+ ``metadata`` is not exposed: providers must not forward it to OFREP clients.
17
+ """
18
+
19
+ type: str
20
+ etag: str | None = None
21
+ last_modified: int | None = None
22
+
23
+ @classmethod
24
+ def parse(cls, payload: str | None) -> SseMessage | None:
25
+ """Parse an Ably envelope (message JSON-encoded in ``data``) or a flat message.
26
+ Returns None for invalid JSON or a missing/blank ``type``."""
27
+ if not payload or not payload.strip():
28
+ return None
29
+ try:
30
+ outer: typing.Any = json.loads(payload)
31
+ data = outer.get("data") if isinstance(outer, dict) else None
32
+ inner: typing.Any = json.loads(data) if isinstance(data, str) else outer
33
+ except ValueError:
34
+ return None
35
+ if not isinstance(inner, dict):
36
+ return None
37
+ msg_type = inner.get("type")
38
+ if not isinstance(msg_type, str) or not msg_type.strip():
39
+ return None
40
+ etag = inner.get("etag")
41
+ last_modified = inner.get("lastModified")
42
+ return cls(
43
+ type=msg_type,
44
+ etag=etag if isinstance(etag, str) else None,
45
+ # a malformed lastModified reads as unknown, so the re-fetch it announces is never dropped
46
+ last_modified=last_modified
47
+ if isinstance(last_modified, int) and not isinstance(last_modified, bool)
48
+ else None,
49
+ )
@@ -5,6 +5,7 @@ import threading
5
5
  from collections.abc import Mapping, Sequence
6
6
  from importlib.metadata import PackageNotFoundError
7
7
  from importlib.metadata import version as _pkg_version
8
+ from urllib.parse import urlsplit
8
9
 
9
10
  import requests
10
11
 
@@ -18,6 +19,7 @@ from openfeature.provider import AbstractProvider, Metadata, ProviderStatus
18
19
 
19
20
  from ._cdn import MAX_FETCH_DURATION_S as _MAX_FETCH_DURATION_S
20
21
  from ._cdn import CdnFetcher
22
+ from ._cdn_config import extract_sse_url
21
23
  from ._metrics_aggregator import (
22
24
  HARD_CAP_DISTINCT_FLAGS,
23
25
  SOFT_CAP_DISTINCT_FLAGS,
@@ -26,12 +28,15 @@ from ._metrics_aggregator import (
26
28
  from ._metrics_flusher import EvaluationMetricsFlusher
27
29
  from ._metrics_hook import DynatraceMetricsHook
28
30
  from ._polling import PollingManager
31
+ from ._sse import SseManager
32
+ from ._sse_message import TYPE_REFETCH_CONFIG, SseMessage
29
33
  from .options import DynatraceProviderOptions
30
34
 
31
35
  log = logging.getLogger(__name__)
32
36
 
33
37
  _PROVIDER_NAME = "dynatrace"
34
38
  _POLL_INTERVAL_S: float = 10.0
39
+ _SSE_CONNECTED_POLL_INTERVAL_S: float = 600.0
35
40
  _GRACE_PERIOD_S: float = 60.0
36
41
  _METRICS_FLUSH_INTERVAL_S: float = 30.0
37
42
  _VERSION_METADATA_KEY = "version"
@@ -55,6 +60,7 @@ class DynatraceProvider(AbstractProvider):
55
60
  fetcher: CdnFetcher | None = None,
56
61
  metrics_flusher: EvaluationMetricsFlusher | None = None,
57
62
  poll_interval_s: float = _POLL_INTERVAL_S,
63
+ disable_sse: bool = False,
58
64
  grace_period_s: float = _GRACE_PERIOD_S,
59
65
  metrics_flush_interval_s: float = _METRICS_FLUSH_INTERVAL_S,
60
66
  ) -> None:
@@ -75,8 +81,13 @@ class DynatraceProvider(AbstractProvider):
75
81
  self._flagd_core = flagd_core or FlagdCore()
76
82
  self._fetcher = fetcher or CdnFetcher(options)
77
83
  self._poll_interval_s = poll_interval_s
84
+ self._sse_connected_poll_interval_s = _SSE_CONNECTED_POLL_INTERVAL_S
85
+ self._sse_enabled = not disable_sse
78
86
  self._grace_period_s = grace_period_s
79
87
  self._poller: PollingManager | None = None
88
+ self._sse_manager: SseManager | None = None
89
+ # true while the SSE stream is open; only selects the poll cadence
90
+ self._sse_connected = False
80
91
  self._grace_timer: threading.Timer | None = None
81
92
  self._state = ProviderStatus.NOT_READY
82
93
  self._lock = threading.RLock()
@@ -97,6 +108,11 @@ class DynatraceProvider(AbstractProvider):
97
108
  self._metrics_aggregator, on_record=self._metrics_flusher.on_record
98
109
  )
99
110
 
111
+ @property
112
+ def _init_started(self) -> bool:
113
+ """Whether initialize() has run since the last shutdown(); its poller runs on both outcomes. Caller holds ``_lock``."""
114
+ return self._poller is not None
115
+
100
116
  def get_metadata(self) -> Metadata:
101
117
  return Metadata(name=_PROVIDER_NAME)
102
118
 
@@ -104,10 +120,15 @@ class DynatraceProvider(AbstractProvider):
104
120
  return [self._metrics_hook]
105
121
 
106
122
  def initialize(self, evaluation_context: EvaluationContext | None = None) -> None:
107
- log.info("initializing Dynatrace provider (key=%s)", self._options.public_key)
108
123
  with self._lock:
124
+ if self._init_started:
125
+ log.debug("provider already initialized; ignoring initialize()")
126
+ return
109
127
  self._generation += 1
110
128
  generation = self._generation
129
+ log.info("initializing Dynatrace provider (key=%s)", self._options.public_key)
130
+ if not self._sse_enabled:
131
+ log.debug("SSE disabled by configuration; running in polling-only mode")
111
132
 
112
133
  body = self._fetch_initial_body(generation)
113
134
 
@@ -117,6 +138,7 @@ class DynatraceProvider(AbstractProvider):
117
138
  # not just ParseError: a non-JSON body raises JSONDecodeError, which must still leave a recoverable provider
118
139
  self._fail_initialization(generation, f"invalid config: {exc}")
119
140
  raise GeneralError(f"initial CDN config is invalid: {exc}") from exc
141
+ self._fetcher.commit()
120
142
 
121
143
  # set the aggregator version eagerly so it is authoritative before any evaluation is recorded
122
144
  self._update_metrics_version_from_flag_set()
@@ -132,6 +154,7 @@ class DynatraceProvider(AbstractProvider):
132
154
  self._cancel_grace_timer()
133
155
  self._state = ProviderStatus.READY
134
156
  self._start_poller_locked()
157
+ self._sync_sse_locked(body)
135
158
  self._metrics_flusher.start()
136
159
  log.info("Dynatrace provider initialized successfully")
137
160
 
@@ -173,6 +196,8 @@ class DynatraceProvider(AbstractProvider):
173
196
  self._poller = PollingManager(
174
197
  fetch_flags=self._fetcher.fetch,
175
198
  store_flags=self._store_flags,
199
+ commit_fetch=self._fetcher.commit,
200
+ is_newer_than_applied=self._fetcher.is_newer_than_committed,
176
201
  emit_change=self._on_configuration_changed,
177
202
  emit_success=self._on_fetch_success,
178
203
  emit_failure=self._on_fetch_failure,
@@ -180,6 +205,63 @@ class DynatraceProvider(AbstractProvider):
180
205
  )
181
206
  self._poller.start()
182
207
 
208
+ def _sync_sse_locked(self, body: str) -> None:
209
+ """Follow the SSE stream advertised in ``body``: open it, reconnect when its URL rotates,
210
+ close it when it is withdrawn. Caller holds ``_lock``."""
211
+ if not self._sse_enabled:
212
+ return
213
+ cdn = urlsplit(self._options.cdn_url)
214
+ sse_url = extract_sse_url(body, f"{cdn.scheme}://{cdn.netloc}")
215
+ current = self._sse_manager
216
+ if sse_url is None:
217
+ # the server always sends eventStreams, so only a withdrawn stream is worth logging
218
+ if current is not None:
219
+ log.info(
220
+ "config no longer advertises an SSE stream; falling back to polling only"
221
+ )
222
+ self._stop_sse_locked()
223
+ return
224
+ if current is not None:
225
+ if current.url == sse_url:
226
+ return
227
+ log.info("SSE stream URL rotated; reconnecting")
228
+ self._stop_sse_locked()
229
+
230
+ manager: SseManager | None = None
231
+
232
+ def on_connected() -> None:
233
+ self._on_sse_connected(manager)
234
+
235
+ def on_disconnected() -> None:
236
+ self._on_sse_disconnected(manager)
237
+
238
+ def on_message(msg: SseMessage) -> None:
239
+ self._on_sse_message(manager, msg)
240
+
241
+ manager = SseManager(sse_url, on_connected, on_disconnected, on_message)
242
+ try:
243
+ manager.start()
244
+ except Exception as exc:
245
+ # not fatal; polling still carries changes.
246
+ manager.stop()
247
+ log.warning(
248
+ "failed to start SSE connection; falling back to polling-only mode: %s",
249
+ type(exc).__name__,
250
+ )
251
+ return
252
+ self._sse_manager = manager
253
+
254
+ def _stop_sse_locked(self) -> None:
255
+ """Close the SSE stream and forget its state; polling returns to its normal cadence.
256
+ Caller holds ``_lock``."""
257
+ was_connected = self._sse_connected
258
+ self._sse_connected = False
259
+ if self._sse_manager is not None:
260
+ self._sse_manager.stop()
261
+ self._sse_manager = None
262
+ if was_connected and self._poller is not None:
263
+ self._poller.set_interval(self._poll_interval_s)
264
+
183
265
  def _fail_initialization(self, generation: int, reason: str) -> None:
184
266
  """Enter ERROR and start polling so the provider can still recover.
185
267
 
@@ -195,7 +277,7 @@ class DynatraceProvider(AbstractProvider):
195
277
  "not starting recovery poller"
196
278
  )
197
279
  return
198
- # nothing was applied, so cached validators would let the recovery poll 304 its way to READY over an empty store
280
+ # a previous lifecycle's validators would let the recovery poll 304 its way to READY over an empty store
199
281
  self._fetcher.invalidate_cache()
200
282
  log.warning("initialization failed (%s); polling for recovery", reason)
201
283
  self._state = ProviderStatus.ERROR
@@ -212,6 +294,8 @@ class DynatraceProvider(AbstractProvider):
212
294
  )
213
295
  if not already_idle:
214
296
  log.info("shutting down Dynatrace provider")
297
+ # providers.md 6.1: close SSE first, so no notification triggers a fetch mid-shutdown
298
+ self._stop_sse_locked()
215
299
  self._state = ProviderStatus.NOT_READY
216
300
  self._cancel_grace_timer()
217
301
  if self._poller is not None:
@@ -282,13 +366,13 @@ class DynatraceProvider(AbstractProvider):
282
366
  sealed = self._metrics_aggregator.drain()
283
367
  self._metrics_flusher.enqueue_sealed_batch(sealed)
284
368
  self._metrics_flusher.trigger_flush()
285
- try:
286
- changed = self._flagd_core.set_flags_and_get_changed_keys(body)
287
- except Exception:
288
- # fetched but not applied, so its validators would make the next poll a 304 over the previous config
289
- self._fetcher.invalidate_cache()
290
- raise
369
+ changed = self._flagd_core.set_flags_and_get_changed_keys(body)
291
370
  self._update_metrics_version_from_flag_set()
371
+ with self._lock:
372
+ # every stored config may open, rotate or withdraw the stream, including the first one after a failed init;
373
+ # a shut-down provider has no poller
374
+ if self._poller is not None:
375
+ self._sync_sse_locked(body)
292
376
  return changed
293
377
 
294
378
  def _update_metrics_version_from_flag_set(self) -> None:
@@ -300,6 +384,11 @@ class DynatraceProvider(AbstractProvider):
300
384
  with self._lock:
301
385
  if self._state == ProviderStatus.NOT_READY:
302
386
  return
387
+ if self._sse_connected:
388
+ self._set_poll_interval_locked(
389
+ self._sse_connected_poll_interval_s,
390
+ "fetch succeeded with SSE connected",
391
+ )
303
392
  self._cancel_grace_timer()
304
393
  if self._state in (ProviderStatus.STALE, ProviderStatus.ERROR):
305
394
  prev_state = self._state
@@ -311,6 +400,7 @@ class DynatraceProvider(AbstractProvider):
311
400
  with self._lock:
312
401
  if self._state == ProviderStatus.NOT_READY:
313
402
  return
403
+ self._set_poll_interval_locked(self._poll_interval_s, "fetch failed")
314
404
  if self._state == ProviderStatus.READY:
315
405
  self._state = ProviderStatus.STALE
316
406
  log.warning("poll failed, transitioning to STALE: %s", reason)
@@ -319,6 +409,14 @@ class DynatraceProvider(AbstractProvider):
319
409
  else:
320
410
  log.warning("poll failed (state=%s): %s", self._state, reason)
321
411
 
412
+ def _set_poll_interval_locked(self, interval_s: float, reason: str) -> None:
413
+ """Switch the poll cadence, logging only an actual change. Caller holds ``_lock``."""
414
+ poller = self._poller
415
+ if poller is None or poller.interval_s == interval_s:
416
+ return
417
+ poller.set_interval(interval_s)
418
+ log.info("poll interval switched to %ss (%s)", interval_s, reason)
419
+
322
420
  def _on_configuration_changed(self, changed_keys: list[str]) -> None:
323
421
  with self._lock:
324
422
  if self._state == ProviderStatus.NOT_READY:
@@ -327,6 +425,59 @@ class DynatraceProvider(AbstractProvider):
327
425
  ProviderEventDetails(flags_changed=changed_keys)
328
426
  )
329
427
 
428
+ # SSE callbacks: each names the manager it came from, so a stream already closed by shutdown cannot act.
429
+ # SSE only prompts fetches and never touches READY/STALE/ERROR; the cadence follows fetch outcomes (see above).
430
+
431
+ def _on_sse_connected(self, source: SseManager | None) -> None:
432
+ """Issue a catch-up fetch for anything missed while disconnected; its success relaxes the cadence."""
433
+ with self._lock:
434
+ if source is None or source is not self._sse_manager:
435
+ return
436
+ self._sse_connected = True
437
+ poller = self._poller
438
+ if poller is None:
439
+ return
440
+ log.info("SSE connected; requesting a catch-up fetch")
441
+ # no announced lastModified, so a deferred catch-up is never dropped
442
+ poller.request_out_of_band_fetch(None)
443
+
444
+ def _on_sse_disconnected(self, source: SseManager | None) -> None:
445
+ """Switch to the 10s cadence; fetch failures from here on are handled like any poll failure."""
446
+ with self._lock:
447
+ if source is None or source is not self._sse_manager:
448
+ return
449
+ if not self._sse_connected:
450
+ # never connected, or a repeat fault while the library retries
451
+ return
452
+ self._sse_connected = False
453
+ log.warning(
454
+ "SSE disconnected; falling back to polling every %ss",
455
+ self._poll_interval_s,
456
+ )
457
+ if self._poller is not None:
458
+ self._poller.set_interval(self._poll_interval_s)
459
+
460
+ def _on_sse_message(self, source: SseManager | None, msg: SseMessage) -> None:
461
+ """Trigger a conditional re-fetch on ``refetchConfig``; ignore every other type.
462
+ lastModified is forwarded only so a deferred re-fetch can be dropped (providers.md 2.1.1)."""
463
+ if msg.type != TYPE_REFETCH_CONFIG:
464
+ log.debug(
465
+ "ignoring SSE message of type %r (server provider only handles %r)",
466
+ msg.type,
467
+ TYPE_REFETCH_CONFIG,
468
+ )
469
+ return
470
+ with self._lock:
471
+ if source is None or source is not self._sse_manager:
472
+ return
473
+ poller = self._poller
474
+ log.debug(
475
+ "SSE refetchConfig received; triggering CDN re-fetch (lastModified=%s)",
476
+ msg.last_modified,
477
+ )
478
+ if poller is not None:
479
+ poller.request_out_of_band_fetch(msg.last_modified)
480
+
330
481
  def _start_grace_timer(self) -> None:
331
482
  self._cancel_grace_timer()
332
483
  t = threading.Timer(self._grace_period_s, self._grace_expired)
@@ -1,140 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import logging
4
- import threading
5
- import time
6
- from collections.abc import Callable
7
-
8
- from ._cdn import FetchResult
9
-
10
- log = logging.getLogger(__name__)
11
-
12
-
13
- class PollingManager:
14
- """
15
- Polls on a fixed-rate schedule anchored to request initiation, not completion.
16
-
17
- A ticker thread fires every interval_s regardless of how long the previous
18
- fetch took. At most one fetch is in flight at a time (providers.md section
19
- 2.1.1): a tick firing during a poll is skipped and emits nothing, so it does
20
- not reset the staleness grace timer. The section 2.1 bound should keep that
21
- from happening at all, but the bound is a design invariant rather than
22
- something the runtime enforces, so the case is handled.
23
- """
24
-
25
- def __init__(
26
- self,
27
- fetch_flags: Callable[[threading.Event | None], FetchResult],
28
- store_flags: Callable[[str], list[str]],
29
- emit_change: Callable[[list[str]], None],
30
- emit_success: Callable[[], None],
31
- emit_failure: Callable[[str], None],
32
- interval_s: float,
33
- ) -> None:
34
- self._fetch_flags = fetch_flags
35
- self._store_flags = store_flags
36
- self._emit_change = emit_change
37
- self._emit_success = emit_success
38
- self._emit_failure = emit_failure
39
- self._interval_s = interval_s
40
- self._stop_event = threading.Event()
41
- self._in_flight = False
42
- self._in_flight_lock = threading.Lock()
43
- self._ticker_thread: threading.Thread | None = None
44
-
45
- def start(self) -> None:
46
- if self._ticker_thread is not None and self._ticker_thread.is_alive():
47
- return
48
- self._stop_event.clear()
49
- self._ticker_thread = threading.Thread(
50
- target=self._ticker_loop,
51
- name="dt-provider-ticker",
52
- daemon=True,
53
- )
54
- self._ticker_thread.start()
55
-
56
- def stop(self) -> None:
57
- # stop event doubles as the fetch cancel signal, so an in-flight poll abandons its backoff
58
- self._stop_event.set()
59
- # leave _in_flight alone: only _run_poll's finally clears it; clearing here would let a restart double up
60
- if self._ticker_thread is not None:
61
- self._ticker_thread.join(timeout=2.0)
62
-
63
- def _ticker_loop(self) -> None:
64
- next_tick = time.monotonic() + self._interval_s
65
- while not self._stop_event.is_set():
66
- wait = max(0.0, next_tick - time.monotonic())
67
- if self._stop_event.wait(wait):
68
- break
69
- next_tick += self._interval_s
70
- now = time.monotonic()
71
- if next_tick < now:
72
- next_tick = now + self._interval_s
73
- self._dispatch()
74
-
75
- def _dispatch(self) -> None:
76
- if self._stop_event.is_set():
77
- return
78
- with self._in_flight_lock:
79
- if self._in_flight:
80
- # Neither failed nor succeeded: the tick learned nothing.
81
- log.debug("poll tick skipped: a fetch is already in flight")
82
- return
83
- self._in_flight = True
84
- threading.Thread(
85
- target=self._run_poll,
86
- name="dt-provider-poller",
87
- daemon=True,
88
- ).start()
89
-
90
- def _run_poll(self) -> None:
91
- try:
92
- self._poll()
93
- finally:
94
- with self._in_flight_lock:
95
- self._in_flight = False
96
-
97
- def _poll(self) -> None:
98
- cancel = self._stop_event
99
- try:
100
- result = self._fetch_flags(cancel)
101
-
102
- if cancel.is_set():
103
- log.debug("provider stopped during fetch; discarding result")
104
- return
105
-
106
- if result.skipped:
107
- log.debug("fetch skipped (single-flight); emitting nothing")
108
- return
109
-
110
- if result.is_not_modified:
111
- log.debug("flags not modified")
112
- self._emit_success()
113
- return
114
-
115
- if result.is_rate_limited:
116
- log.debug("rate limited; serving cached values")
117
- return
118
-
119
- if not result.is_success:
120
- self._emit_failure(f"HTTP {result.status}")
121
- return
122
-
123
- body = result.body
124
- if not body:
125
- self._emit_failure("empty response body")
126
- return
127
-
128
- changed_keys = self._store_flags(body)
129
- self._emit_success()
130
-
131
- if changed_keys:
132
- log.info("flag configuration changed: %s", changed_keys)
133
- self._emit_change(changed_keys)
134
-
135
- except Exception as exc:
136
- if cancel.is_set():
137
- log.debug("poll abandoned during shutdown")
138
- return
139
- log.warning("poll failed: %s", type(exc).__name__)
140
- self._emit_failure(type(exc).__name__)