-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig_manager.py
More file actions
285 lines (248 loc) · 11.4 KB
/
config_manager.py
File metadata and controls
285 lines (248 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import json
import logging
import threading
import time
from datetime import datetime
from typing import Optional
import ld_eventsource.actions
from devcycle_python_sdk.api.config_client import ConfigAPIClient
from devcycle_python_sdk.api.local_bucketing import LocalBucketing
from devcycle_python_sdk.exceptions import (
APIClientError,
APIClientUnauthorizedError,
)
from wsgiref.handlers import format_date_time
from devcycle_python_sdk.options import DevCycleLocalOptions
from devcycle_python_sdk.managers.sse_manager import SSEManager
from devcycle_python_sdk.models.config_metadata import ConfigMetadata
logger = logging.getLogger(__name__)
class EnvironmentConfigManager(threading.Thread):
def __init__(
self,
sdk_key: str,
options: DevCycleLocalOptions,
local_bucketing: LocalBucketing,
):
super().__init__()
self._sdk_key = sdk_key
self._options = options
self._local_bucketing = local_bucketing
self._sse_manager: Optional[SSEManager] = None
self._sse_polling_interval = 1000 * 60 * 15 * 60
self._sse_connected = False
self._config: Optional[dict] = None
self._config_etag: Optional[str] = None
self._config_lastmodified: Optional[str] = None
# Exponential backoff configuration
self._sse_reconnect_attempts = 0
self._min_reconnect_interval = 5.0 # Start at 5 seconds
self._max_reconnect_interval = 300.0 # Cap at 5 minutes
self._last_reconnect_attempt_time: Optional[float] = None
self._sse_reconnecting = False
self._sse_reconnect_lock = threading.Lock()
self._config_api_client = ConfigAPIClient(self._sdk_key, self._options)
self._polling_enabled = True
self.daemon = True
self.start()
def is_initialized(self) -> bool:
return self._config is not None
def _recreate_sse_connection(self):
"""Recreate the SSE connection with the current config."""
# Acquire lock to check state and save references to old connection
with self._sse_reconnect_lock:
if self._config is None or self._options.disable_realtime_updates:
logger.debug(
"DevCycle: Skipping SSE recreation - no config or updates disabled"
)
return
# Save reference to old SSE manager and clear it to prevent concurrent access
old_sse_manager = self._sse_manager
self._sse_manager = None
# Perform potentially blocking operations outside the lock to avoid deadlock
# The SSE read thread may call sse_error/sse_state which need the lock
try:
if old_sse_manager is not None and old_sse_manager.client is not None:
old_sse_manager.client.close()
if old_sse_manager.read_thread.is_alive():
old_sse_manager.read_thread.join(timeout=1.0)
except Exception as e:
logger.debug(f"DevCycle: Error closing old SSE connection: {e}")
# Re-acquire lock to create new connection and update state
try:
with self._sse_reconnect_lock:
# Re-read config to ensure we use the latest version
if self._config is None:
logger.debug(
"DevCycle: Config was cleared during SSE reconnection, skipping"
)
return
# Create new SSE manager with the current config
self._sse_manager = SSEManager(
self.sse_state,
self.sse_error,
self.sse_message,
)
self._sse_manager.update(self._config)
logger.info("DevCycle: SSE connection created successfully")
except Exception as e:
logger.debug(f"DevCycle: Failed to recreate SSE connection: {e}")
def _delayed_sse_reconnect(self, delay_seconds: float):
"""Delayed SSE reconnection with configurable backoff."""
try:
logger.debug(
f"DevCycle: Waiting {delay_seconds}s before reconnecting SSE..."
)
time.sleep(delay_seconds)
self._recreate_sse_connection()
except Exception as e:
logger.error(f"DevCycle: Error during delayed SSE reconnection: {e}")
finally:
with self._sse_reconnect_lock:
self._sse_reconnecting = False
def _get_config(self, last_modified: Optional[float] = None):
try:
lm_header = self._config_lastmodified
if last_modified is not None:
lm_timestamp = datetime.fromtimestamp(last_modified)
lm_header = format_date_time(time.mktime(lm_timestamp.timetuple()))
new_config, new_etag, new_lastmodified = self._config_api_client.get_config(
config_etag=self._config_etag, last_modified=lm_header
)
# Abort early if the last modified is before the sent one.
if new_config is None and new_etag is None and new_lastmodified is None:
return
if new_config is None and new_etag == self._config_etag:
# api not returning data and the etag is the same
# no change to the config since last request
return
elif new_config is None:
logger.warning(
"DevCycle: Config CDN fetch returned no data with a different etag"
)
return
trigger_on_client_initialized = self._config is None
self._config = new_config
self._config_etag = new_etag
self._config_lastmodified = new_lastmodified
json_config = json.dumps(self._config)
self._local_bucketing.store_config(json_config)
if not self._options.disable_realtime_updates:
if (
self._sse_manager is None
or self._sse_manager.client is None
or not self._sse_manager.read_thread.is_alive()
):
# Only recreate if not already reconnecting from error handler
with self._sse_reconnect_lock:
if not self._sse_reconnecting:
logger.info(
"DevCycle: SSE connection not active, creating new connection"
)
should_recreate = True
else:
logger.debug(
"DevCycle: SSE reconnection already scheduled, skipping"
)
should_recreate = False
if should_recreate:
self._recreate_sse_connection()
if (
trigger_on_client_initialized
and self._options.on_client_initialized is not None
):
try:
self._options.on_client_initialized()
except Exception as e:
logger.warning(
f"DevCycle: Error received from on_client_initialized callback: {str(e)}"
)
except APIClientError as e:
logger.warning(f"DevCycle: Config fetch failed. Status: {str(e)}")
except APIClientUnauthorizedError:
logger.error(
"DevCycle: Error initializing DevCycle: Invalid SDK key provided."
)
self._polling_enabled = False
def get_config_metadata(self) -> Optional[ConfigMetadata]:
return self._local_bucketing.get_config_metadata()
def run(self):
while self._polling_enabled:
try:
self._get_config()
except Exception as e:
if self._polling_enabled:
logger.warning(
f"DevCycle: Error polling for config changes: {str(e)}"
)
if self._sse_connected:
time.sleep(self._sse_polling_interval / 1000.0)
else:
time.sleep(self._options.config_polling_interval_ms / 1000.0)
def sse_message(self, message: ld_eventsource.actions.Event):
# Received a message from the SSE stream but our sse_connected is False, so we need to set it to True
if not self._sse_connected:
self.sse_state(None)
logger.info(f"DevCycle: Received message: {message.data}")
sse_message = json.loads(message.data)
dvc_data = json.loads(sse_message.get("data"))
if (
dvc_data.get("type") == "refetchConfig"
or dvc_data.get("type") == ""
or dvc_data.get("type") is None
):
logger.info("DevCycle: Received refetchConfig message - updating config")
self._get_config(dvc_data["lastModified"] / 1000.0)
# SSE connection healthy, reconnect attempts reset.
if dvc_data.get("type") == "ping" or dvc_data.get("type") == "refetchConfig":
self._sse_reconnect_attempts = 0
def sse_error(self, error: ld_eventsource.actions.Fault):
self._sse_connected = False
logger.debug(f"Devcyle: SSE connection error: {error.error}")
current_time = time.time()
# Calculate exponential backoff interval (capped at max)
backoff_interval = min(
self._min_reconnect_interval * (2**self._sse_reconnect_attempts),
self._max_reconnect_interval,
)
with self._sse_reconnect_lock:
if self._sse_reconnecting:
logger.debug("Devcyle: Reconnection already in progress, skipping")
return
# Check if we need to wait for backoff
if (
self._last_reconnect_attempt_time is not None
and current_time - self._last_reconnect_attempt_time < backoff_interval
):
time_remaining = backoff_interval - (
current_time - self._last_reconnect_attempt_time
)
logger.debug(
f"Devcyle: Skipping reconnection attempt, waiting for backoff period "
f"({time_remaining:.1f}s remaining of {backoff_interval:.1f}s)"
)
return
self._sse_reconnecting = True
self._last_reconnect_attempt_time = current_time
self._sse_reconnect_attempts += 1
logger.debug(
f"Devcyle: Attempting SSE reconnection (attempt #{self._sse_reconnect_attempts}, "
f"next backoff: {backoff_interval:.1f}s)"
)
reconnect_thread = threading.Thread(
target=self._delayed_sse_reconnect, args=(backoff_interval,), daemon=True
)
reconnect_thread.start()
def sse_state(self, state: Optional[ld_eventsource.actions.Start]):
if not self._sse_connected:
self._sse_connected = True
logger.info("DevCycle: Connected to SSE stream")
# Clear reconnection state
with self._sse_reconnect_lock:
self._sse_reconnecting = False
self._last_reconnect_attempt_time = None
else:
logger.debug("Devcyle: SSE keepalive received")
def close(self):
self._polling_enabled = False
if self._sse_manager is not None and self._sse_manager.client is not None:
self._sse_manager.client.close()