fb-python-sdk 1.1.7__tar.gz → 1.1.9__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 (48) hide show
  1. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/MANIFEST.in +1 -1
  2. {fb_python_sdk-1.1.7/fb_python_sdk.egg-info → fb_python_sdk-1.1.9}/PKG-INFO +48 -9
  3. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/README.md +42 -1
  4. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/dev-requirements.txt +2 -2
  5. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9/fb_python_sdk.egg-info}/PKG-INFO +48 -9
  6. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fb_python_sdk.egg-info/SOURCES.txt +2 -2
  7. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fb_python_sdk.egg-info/requires.txt +2 -2
  8. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fb_python_sdk.egg-info/top_level.txt +0 -1
  9. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/__init__.py +0 -3
  10. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/client.py +86 -25
  11. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/common_types.py +12 -3
  12. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/config.py +20 -13
  13. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/data_storage.py +40 -17
  14. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/event_processor.py +81 -30
  15. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/interfaces.py +21 -3
  16. fb_python_sdk-1.1.9/fbclient/notice_broadcaster.py +85 -0
  17. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/status.py +57 -5
  18. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/streaming.py +95 -19
  19. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/update_processor.py +6 -2
  20. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/utils/__init__.py +3 -3
  21. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/utils/http_client.py +8 -4
  22. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/utils/repeatable_task.py +13 -6
  23. fb_python_sdk-1.1.9/release/package.json +3 -0
  24. fb_python_sdk-1.1.9/requirements.txt +4 -0
  25. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/setup.py +4 -7
  26. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/tests/test_data_storage.py +33 -1
  27. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/tests/test_data_update_status_provider.py +66 -0
  28. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/tests/test_event_processor.py +75 -0
  29. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/tests/test_fbclient.py +56 -4
  30. fb_python_sdk-1.1.9/tests/test_runtime_safety.py +472 -0
  31. fb_python_sdk-1.1.7/fbclient/notice_broadcaster.py +0 -57
  32. fb_python_sdk-1.1.7/release/package.json +0 -3
  33. fb_python_sdk-1.1.7/requirements.txt +0 -4
  34. fb_python_sdk-1.1.7/tests/__init__.py +0 -0
  35. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/LICENSE +0 -0
  36. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fb_python_sdk.egg-info/dependency_links.txt +0 -0
  37. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/category.py +0 -0
  38. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/evaluator.py +0 -0
  39. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/event_types.py +0 -0
  40. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/flag_change_notification.py +0 -0
  41. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/status_types.py +0 -0
  42. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/utils/exponential_backoff_jitter_strategy.py +0 -0
  43. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/utils/rwlock.py +0 -0
  44. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/fbclient/utils/variation_splitting_algorithm.py +0 -0
  45. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/pyproject.toml +0 -0
  46. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/setup.cfg +0 -0
  47. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/tests/test_evaluator.py +0 -0
  48. {fb_python_sdk-1.1.7 → fb_python_sdk-1.1.9}/tests/test_flag_change_notification.py +0 -0
@@ -1,4 +1,4 @@
1
1
  include requirements.txt
2
2
  include README.md
3
3
  include dev-requirements.txt
4
- include release/package.json
4
+ include release/package.json
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fb-python-sdk
3
- Version: 1.1.7
3
+ Version: 1.1.9
4
4
  Summary: A Python SDK for FeatBit plateform
5
5
  Home-page: https://github.com/featbit/featbit-python-sdk
6
6
  Author: Dian SUN
@@ -13,23 +13,21 @@ Classifier: License :: OSI Approved :: Apache Software License
13
13
  Classifier: Operating System :: OS Independent
14
14
  Classifier: Topic :: Software Development
15
15
  Classifier: Topic :: Software Development :: Libraries
16
- Classifier: Programming Language :: Python :: 3.6
17
- Classifier: Programming Language :: Python :: 3.7
18
- Classifier: Programming Language :: Python :: 3.8
19
- Classifier: Programming Language :: Python :: 3.9
20
16
  Classifier: Programming Language :: Python :: 3.10
21
17
  Classifier: Programming Language :: Python :: 3.11
22
18
  Classifier: Programming Language :: Python :: 3.12
23
- Requires-Python: >=3.6, <3.13
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
+ Requires-Python: >=3.10, <3.15
24
22
  Description-Content-Type: text/markdown
25
23
  License-File: LICENSE
26
24
  Requires-Dist: certifi>=2018.4.16
27
- Requires-Dist: urllib3>=1.22.0
25
+ Requires-Dist: urllib3<3,>=1.26.5
28
26
  Requires-Dist: python-dateutil>=2.8.2
29
27
  Requires-Dist: websocket-client>=1.0.0
30
28
  Provides-Extra: dev
31
29
  Requires-Dist: certifi>=2018.4.16; extra == "dev"
32
- Requires-Dist: urllib3>=1.22.0; extra == "dev"
30
+ Requires-Dist: urllib3<3,>=1.26.5; extra == "dev"
33
31
  Requires-Dist: websocket-client>=1.0.0; extra == "dev"
34
32
  Requires-Dist: python-dateutil>=2.8.2; extra == "dev"
35
33
  Requires-Dist: flake8; extra == "dev"
@@ -71,7 +69,10 @@ If you want to use your own data source, see [Offline Mode](#offline-mode).
71
69
  ## Get Started
72
70
 
73
71
  ### Installation
74
- install the sdk in using pip, this version of the SDK is compatible with Python 3.6 through 3.11.
72
+ Install the SDK using pip. This version is compatible with Python 3.10 through 3.14.
73
+ CI is configured to test compatibility on Linux and Windows through Python 3.14,
74
+ including the full unit suite and SDK resource-lifecycle audit.
75
+ Free-threaded CPython 3.14 builds are not currently tested or supported.
75
76
 
76
77
  ```shell
77
78
  pip install fb-python-sdk
@@ -119,6 +120,29 @@ client.stop()
119
120
 
120
121
  - [Python Demo](https://github.com/featbit/featbit-samples/blob/main/samples/dino-game/demo-python/demo_python.py)
121
122
 
123
+ ### SDK reliability and live verification
124
+
125
+ The repository includes a repeatable [reliability review](docs/REVIEW.md)
126
+ covering unit tests, thread safety, memory retention, background-thread
127
+ shutdown, public runtime exception isolation, and redundant code. Run the
128
+ local resource audit with:
129
+
130
+ ```shell
131
+ python scripts/resource_audit.py --evaluations 80000 --workers 8
132
+ ```
133
+
134
+ To verify WebSocket synchronization, remote evaluation, status changes, event
135
+ delivery, and clean shutdown against FeatBit Cloud or a self-hosted evaluation
136
+ service, set `FEATBIT_ENV_SECRET` and run:
137
+
138
+ ```shell
139
+ python scripts/live_integration_check.py
140
+ ```
141
+
142
+ The live script never prints or persists the environment secret. See the
143
+ review document for all supported environment variables and verification
144
+ criteria.
145
+
122
146
  ### FBClient
123
147
 
124
148
  Applications **SHOULD instantiate a single FBClient instance** for the lifetime of the application. In the case where an application
@@ -165,6 +189,16 @@ if client.update_status_provider.wait_for_OKState():
165
189
 
166
190
  It's possible to set a timeout in seconds for the `wait_for_OKState` method. If the timeout is reached, the method will return `False` and the client will still be in an uninitialized state. If you do not specify a timeout, the method will wait indefinitely.
167
191
 
192
+ You can also observe later connection interruptions and recoveries:
193
+
194
+ ```python
195
+ def on_status_change(state):
196
+ print(state.state_type, state.error_track)
197
+
198
+ client.update_status_provider.add_listener(on_status_change)
199
+ # Remove it during application shutdown:
200
+ client.update_status_provider.remove_listener(on_status_change)
201
+ ```
168
202
 
169
203
  > To check if the client is ready is optional. Even if the client is not ready, you can still evaluate feature flags, but the default value will be returned if SDK is not yet initialized.
170
204
 
@@ -189,6 +223,7 @@ if client.initialize:
189
223
  flag_value = client.variation(flag_key, user, default_value)
190
224
  # evaluate the flag value and get the detail
191
225
  detail = client.variation_detail(flag_key, user, default=None)
226
+ print(detail.variation, detail.variation_id, detail.reason)
192
227
  ```
193
228
 
194
229
  If you would like to get variations of all feature flags in a special environment, you can use `fbclient.client.FBClient.get_all_latest_flag_variations`, SDK will return `fbclient.common_types.AllFlagStates`, that explain the details of all feature flags. `fbclient.common_types.AllFlagStates.get()` returns the detail of a given feature flag key.
@@ -208,6 +243,10 @@ if client.initialize:
208
243
 
209
244
  > **Note**
210
245
  > If evaluation happened before the client is initialized, or you provide the wrong flag key/user for evaluation, the `variation` calls will return the default value. The `fbclient.common_types.EvalDetail` will explain the details of the latest evaluation including error reason.
246
+ > Runtime evaluation does not raise solely because a supplied default has an
247
+ > unsupported type. If no flag value can be produced, that default object is
248
+ > returned unchanged and a diagnostic message is logged. Required client
249
+ > configuration remains fail-fast during construction.
211
250
 
212
251
  ### Flag Tracking
213
252
 
@@ -18,7 +18,10 @@ If you want to use your own data source, see [Offline Mode](#offline-mode).
18
18
  ## Get Started
19
19
 
20
20
  ### Installation
21
- install the sdk in using pip, this version of the SDK is compatible with Python 3.6 through 3.11.
21
+ Install the SDK using pip. This version is compatible with Python 3.10 through 3.14.
22
+ CI is configured to test compatibility on Linux and Windows through Python 3.14,
23
+ including the full unit suite and SDK resource-lifecycle audit.
24
+ Free-threaded CPython 3.14 builds are not currently tested or supported.
22
25
 
23
26
  ```shell
24
27
  pip install fb-python-sdk
@@ -66,6 +69,29 @@ client.stop()
66
69
 
67
70
  - [Python Demo](https://github.com/featbit/featbit-samples/blob/main/samples/dino-game/demo-python/demo_python.py)
68
71
 
72
+ ### SDK reliability and live verification
73
+
74
+ The repository includes a repeatable [reliability review](docs/REVIEW.md)
75
+ covering unit tests, thread safety, memory retention, background-thread
76
+ shutdown, public runtime exception isolation, and redundant code. Run the
77
+ local resource audit with:
78
+
79
+ ```shell
80
+ python scripts/resource_audit.py --evaluations 80000 --workers 8
81
+ ```
82
+
83
+ To verify WebSocket synchronization, remote evaluation, status changes, event
84
+ delivery, and clean shutdown against FeatBit Cloud or a self-hosted evaluation
85
+ service, set `FEATBIT_ENV_SECRET` and run:
86
+
87
+ ```shell
88
+ python scripts/live_integration_check.py
89
+ ```
90
+
91
+ The live script never prints or persists the environment secret. See the
92
+ review document for all supported environment variables and verification
93
+ criteria.
94
+
69
95
  ### FBClient
70
96
 
71
97
  Applications **SHOULD instantiate a single FBClient instance** for the lifetime of the application. In the case where an application
@@ -112,6 +138,16 @@ if client.update_status_provider.wait_for_OKState():
112
138
 
113
139
  It's possible to set a timeout in seconds for the `wait_for_OKState` method. If the timeout is reached, the method will return `False` and the client will still be in an uninitialized state. If you do not specify a timeout, the method will wait indefinitely.
114
140
 
141
+ You can also observe later connection interruptions and recoveries:
142
+
143
+ ```python
144
+ def on_status_change(state):
145
+ print(state.state_type, state.error_track)
146
+
147
+ client.update_status_provider.add_listener(on_status_change)
148
+ # Remove it during application shutdown:
149
+ client.update_status_provider.remove_listener(on_status_change)
150
+ ```
115
151
 
116
152
  > To check if the client is ready is optional. Even if the client is not ready, you can still evaluate feature flags, but the default value will be returned if SDK is not yet initialized.
117
153
 
@@ -136,6 +172,7 @@ if client.initialize:
136
172
  flag_value = client.variation(flag_key, user, default_value)
137
173
  # evaluate the flag value and get the detail
138
174
  detail = client.variation_detail(flag_key, user, default=None)
175
+ print(detail.variation, detail.variation_id, detail.reason)
139
176
  ```
140
177
 
141
178
  If you would like to get variations of all feature flags in a special environment, you can use `fbclient.client.FBClient.get_all_latest_flag_variations`, SDK will return `fbclient.common_types.AllFlagStates`, that explain the details of all feature flags. `fbclient.common_types.AllFlagStates.get()` returns the detail of a given feature flag key.
@@ -155,6 +192,10 @@ if client.initialize:
155
192
 
156
193
  > **Note**
157
194
  > If evaluation happened before the client is initialized, or you provide the wrong flag key/user for evaluation, the `variation` calls will return the default value. The `fbclient.common_types.EvalDetail` will explain the details of the latest evaluation including error reason.
195
+ > Runtime evaluation does not raise solely because a supplied default has an
196
+ > unsupported type. If no flag value can be produced, that default object is
197
+ > returned unchanged and a diagnostic message is logged. Required client
198
+ > configuration remains fail-fast during construction.
158
199
 
159
200
  ### Flag Tracking
160
201
 
@@ -1,5 +1,5 @@
1
1
  certifi>=2018.4.16
2
- urllib3>=1.22.0
2
+ urllib3>=1.26.5,<3
3
3
  websocket-client>=1.0.0
4
4
  python-dateutil>=2.8.2
5
5
  flake8
@@ -7,4 +7,4 @@ pytest
7
7
  pytest-mock
8
8
  autopep8
9
9
  build
10
- twine
10
+ twine
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fb-python-sdk
3
- Version: 1.1.7
3
+ Version: 1.1.9
4
4
  Summary: A Python SDK for FeatBit plateform
5
5
  Home-page: https://github.com/featbit/featbit-python-sdk
6
6
  Author: Dian SUN
@@ -13,23 +13,21 @@ Classifier: License :: OSI Approved :: Apache Software License
13
13
  Classifier: Operating System :: OS Independent
14
14
  Classifier: Topic :: Software Development
15
15
  Classifier: Topic :: Software Development :: Libraries
16
- Classifier: Programming Language :: Python :: 3.6
17
- Classifier: Programming Language :: Python :: 3.7
18
- Classifier: Programming Language :: Python :: 3.8
19
- Classifier: Programming Language :: Python :: 3.9
20
16
  Classifier: Programming Language :: Python :: 3.10
21
17
  Classifier: Programming Language :: Python :: 3.11
22
18
  Classifier: Programming Language :: Python :: 3.12
23
- Requires-Python: >=3.6, <3.13
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
+ Requires-Python: >=3.10, <3.15
24
22
  Description-Content-Type: text/markdown
25
23
  License-File: LICENSE
26
24
  Requires-Dist: certifi>=2018.4.16
27
- Requires-Dist: urllib3>=1.22.0
25
+ Requires-Dist: urllib3<3,>=1.26.5
28
26
  Requires-Dist: python-dateutil>=2.8.2
29
27
  Requires-Dist: websocket-client>=1.0.0
30
28
  Provides-Extra: dev
31
29
  Requires-Dist: certifi>=2018.4.16; extra == "dev"
32
- Requires-Dist: urllib3>=1.22.0; extra == "dev"
30
+ Requires-Dist: urllib3<3,>=1.26.5; extra == "dev"
33
31
  Requires-Dist: websocket-client>=1.0.0; extra == "dev"
34
32
  Requires-Dist: python-dateutil>=2.8.2; extra == "dev"
35
33
  Requires-Dist: flake8; extra == "dev"
@@ -71,7 +69,10 @@ If you want to use your own data source, see [Offline Mode](#offline-mode).
71
69
  ## Get Started
72
70
 
73
71
  ### Installation
74
- install the sdk in using pip, this version of the SDK is compatible with Python 3.6 through 3.11.
72
+ Install the SDK using pip. This version is compatible with Python 3.10 through 3.14.
73
+ CI is configured to test compatibility on Linux and Windows through Python 3.14,
74
+ including the full unit suite and SDK resource-lifecycle audit.
75
+ Free-threaded CPython 3.14 builds are not currently tested or supported.
75
76
 
76
77
  ```shell
77
78
  pip install fb-python-sdk
@@ -119,6 +120,29 @@ client.stop()
119
120
 
120
121
  - [Python Demo](https://github.com/featbit/featbit-samples/blob/main/samples/dino-game/demo-python/demo_python.py)
121
122
 
123
+ ### SDK reliability and live verification
124
+
125
+ The repository includes a repeatable [reliability review](docs/REVIEW.md)
126
+ covering unit tests, thread safety, memory retention, background-thread
127
+ shutdown, public runtime exception isolation, and redundant code. Run the
128
+ local resource audit with:
129
+
130
+ ```shell
131
+ python scripts/resource_audit.py --evaluations 80000 --workers 8
132
+ ```
133
+
134
+ To verify WebSocket synchronization, remote evaluation, status changes, event
135
+ delivery, and clean shutdown against FeatBit Cloud or a self-hosted evaluation
136
+ service, set `FEATBIT_ENV_SECRET` and run:
137
+
138
+ ```shell
139
+ python scripts/live_integration_check.py
140
+ ```
141
+
142
+ The live script never prints or persists the environment secret. See the
143
+ review document for all supported environment variables and verification
144
+ criteria.
145
+
122
146
  ### FBClient
123
147
 
124
148
  Applications **SHOULD instantiate a single FBClient instance** for the lifetime of the application. In the case where an application
@@ -165,6 +189,16 @@ if client.update_status_provider.wait_for_OKState():
165
189
 
166
190
  It's possible to set a timeout in seconds for the `wait_for_OKState` method. If the timeout is reached, the method will return `False` and the client will still be in an uninitialized state. If you do not specify a timeout, the method will wait indefinitely.
167
191
 
192
+ You can also observe later connection interruptions and recoveries:
193
+
194
+ ```python
195
+ def on_status_change(state):
196
+ print(state.state_type, state.error_track)
197
+
198
+ client.update_status_provider.add_listener(on_status_change)
199
+ # Remove it during application shutdown:
200
+ client.update_status_provider.remove_listener(on_status_change)
201
+ ```
168
202
 
169
203
  > To check if the client is ready is optional. Even if the client is not ready, you can still evaluate feature flags, but the default value will be returned if SDK is not yet initialized.
170
204
 
@@ -189,6 +223,7 @@ if client.initialize:
189
223
  flag_value = client.variation(flag_key, user, default_value)
190
224
  # evaluate the flag value and get the detail
191
225
  detail = client.variation_detail(flag_key, user, default=None)
226
+ print(detail.variation, detail.variation_id, detail.reason)
192
227
  ```
193
228
 
194
229
  If you would like to get variations of all feature flags in a special environment, you can use `fbclient.client.FBClient.get_all_latest_flag_variations`, SDK will return `fbclient.common_types.AllFlagStates`, that explain the details of all feature flags. `fbclient.common_types.AllFlagStates.get()` returns the detail of a given feature flag key.
@@ -208,6 +243,10 @@ if client.initialize:
208
243
 
209
244
  > **Note**
210
245
  > If evaluation happened before the client is initialized, or you provide the wrong flag key/user for evaluation, the `variation` calls will return the default value. The `fbclient.common_types.EvalDetail` will explain the details of the latest evaluation including error reason.
246
+ > Runtime evaluation does not raise solely because a supplied default has an
247
+ > unsupported type. If no flag value can be produced, that default object is
248
+ > returned unchanged and a diagnostic message is logged. Required client
249
+ > configuration remains fail-fast during construction.
211
250
 
212
251
  ### Flag Tracking
213
252
 
@@ -33,10 +33,10 @@ fbclient/utils/repeatable_task.py
33
33
  fbclient/utils/rwlock.py
34
34
  fbclient/utils/variation_splitting_algorithm.py
35
35
  release/package.json
36
- tests/__init__.py
37
36
  tests/test_data_storage.py
38
37
  tests/test_data_update_status_provider.py
39
38
  tests/test_evaluator.py
40
39
  tests/test_event_processor.py
41
40
  tests/test_fbclient.py
42
- tests/test_flag_change_notification.py
41
+ tests/test_flag_change_notification.py
42
+ tests/test_runtime_safety.py
@@ -1,11 +1,11 @@
1
1
  certifi>=2018.4.16
2
- urllib3>=1.22.0
2
+ urllib3<3,>=1.26.5
3
3
  python-dateutil>=2.8.2
4
4
  websocket-client>=1.0.0
5
5
 
6
6
  [dev]
7
7
  certifi>=2018.4.16
8
- urllib3>=1.22.0
8
+ urllib3<3,>=1.26.5
9
9
  websocket-client>=1.0.0
10
10
  python-dateutil>=2.8.2
11
11
  flake8
@@ -24,9 +24,7 @@ def get() -> FBClient:
24
24
  If you need to create multiple client instances with different environments, instead of this
25
25
  singleton approach you can call directly the :class:`fbclient.client.FBClient` constructor.
26
26
  """
27
- global __config
28
27
  global __client
29
- global __lock
30
28
 
31
29
  try:
32
30
  __lock.read_lock()
@@ -58,7 +56,6 @@ def set_config(config: Config):
58
56
  """
59
57
  global __config
60
58
  global __client
61
- global __lock
62
59
 
63
60
  try:
64
61
  __lock.write_lock()
@@ -16,6 +16,7 @@ from fbclient.flag_change_notification import FlagTracker
16
16
  from fbclient.interfaces import DataUpdateStatusProvider
17
17
  from fbclient.notice_broadcaster import NoticeBroadcater
18
18
  from fbclient.status import DataUpdateStatusProviderImpl
19
+ from fbclient.status_types import State
19
20
  from fbclient.streaming import Streaming, _data_to_dict
20
21
  from fbclient.update_processor import NullUpdateProcessor
21
22
  from fbclient.utils import (cast_variation_by_flag_type, check_uwsgi, log,
@@ -68,6 +69,10 @@ class FBClient:
68
69
  raise ValueError("Config is not valid")
69
70
 
70
71
  self._config = config
72
+ self._stop_lock = threading.Lock()
73
+ self._stop_complete = threading.Event()
74
+ self._stop_owner = None
75
+ self._closed = False
71
76
  if self._config.is_offline:
72
77
  log.info("FB Python SDK: SDK is in offline mode")
73
78
  else:
@@ -79,7 +84,7 @@ class FBClient:
79
84
  # init components
80
85
  # event processor
81
86
  self._event_processor = self._build_event_processor(config)
82
- self._event_handler = lambda event: self._event_processor.send_event(event)
87
+ self._event_handler = self._send_event_safely
83
88
  # data storage
84
89
  self._data_storage = config.data_storage
85
90
  # evaluator
@@ -126,6 +131,15 @@ class FBClient:
126
131
 
127
132
  return Streaming(config, broadcaster, update_status_provider, update_processor_event)
128
133
 
134
+ def _send_event_safely(self, event):
135
+ try:
136
+ if not self._closed:
137
+ self._event_processor.send_event(event)
138
+ except Exception:
139
+ # Event delivery must never affect the application request that
140
+ # produced the event, including when a custom processor is used.
141
+ log.exception('FB Python SDK: event processor failed')
142
+
129
143
  @property
130
144
  def initialize(self) -> bool:
131
145
  """Returns true if the client has successfully connected to feature flag center.
@@ -154,11 +168,46 @@ class FBClient:
154
168
 
155
169
  Do not attempt to use the client after calling this method.
156
170
  """
157
- log.info("FB Python SDK: Python SDK client is closing...")
158
- self._data_storage.stop()
159
- self._update_processor.stop()
160
- self._event_processor.stop()
161
- self._broadcaster.stop()
171
+ current_thread_id = threading.current_thread().ident
172
+ owns_shutdown = False
173
+ with self._stop_lock:
174
+ if self._closed:
175
+ # A recursive call from a synchronous shutdown callback must
176
+ # return immediately. Unrelated callers wait until the owner
177
+ # has released every SDK resource.
178
+ if self._stop_owner == current_thread_id:
179
+ return
180
+ else:
181
+ self._closed = True
182
+ self._stop_owner = current_thread_id
183
+ owns_shutdown = True
184
+
185
+ if not owns_shutdown:
186
+ self._stop_complete.wait()
187
+ return
188
+
189
+ try:
190
+ # The client was marked closed atomically above. Component
191
+ # shutdown can synchronously publish status changes, and an
192
+ # application listener is allowed to call client.stop() again, so
193
+ # no component method may run under the lifecycle lock.
194
+ log.info("FB Python SDK: Python SDK client is closing...")
195
+ # Stop producers before consumers, and isolate every component so
196
+ # one extension failure cannot prevent the remaining resources
197
+ # from being released or escape into application shutdown code.
198
+ for name, component in (
199
+ ('update processor', self._update_processor),
200
+ ('event processor', self._event_processor),
201
+ ('notice broadcaster', self._broadcaster),
202
+ ('data storage', self._data_storage)):
203
+ try:
204
+ component.stop()
205
+ except Exception:
206
+ log.exception('FB Python SDK: %s failed to stop' % name)
207
+ finally:
208
+ with self._stop_lock:
209
+ self._stop_owner = None
210
+ self._stop_complete.set()
162
211
 
163
212
  def __enter__(self):
164
213
  return self
@@ -174,17 +223,21 @@ class FBClient:
174
223
  def _get_flag_internal(self, key: str) -> Optional[dict]:
175
224
  return self._data_storage.get(FEATURE_FLAGS, key)
176
225
 
177
- def __handle_default_value(self, key: str, default: Any) -> Tuple[Optional[str], Optional[str]]:
226
+ def __handle_default_value(self, key: str, default: Any) -> Tuple[Optional[str], Any]:
178
227
  default_value = self._config.get_default_value(key, default)
179
- default_value_type = simple_type_inference(default_value)
180
- if default_value is None:
181
- return None, None
182
- elif default_value_type == 'boolean':
183
- return default_value_type, str(default).lower()
184
- elif default_value_type == 'json':
185
- return default_value_type, json.dumps(default_value)
186
- else:
187
- return default_value_type, str(default_value)
228
+ try:
229
+ default_value_type = simple_type_inference(default_value)
230
+ if default_value is None:
231
+ return None, None
232
+ elif default_value_type == 'boolean':
233
+ return default_value_type, str(default_value).lower()
234
+ elif default_value_type == 'json':
235
+ return default_value_type, json.dumps(default_value)
236
+ else:
237
+ return default_value_type, str(default_value)
238
+ except Exception:
239
+ log.warning('FB Python SDK: unsupported default value; returning it unchanged on evaluation failure')
240
+ return None, default_value
188
241
 
189
242
  def _evaluate_internal(self, key: str, user: dict, default: Any = None) -> _EvalResult:
190
243
  default_value_type, default_value = self.__handle_default_value(key, default)
@@ -235,7 +288,7 @@ class FBClient:
235
288
  :param default: the default value of the flag, to be used if the return value is not available
236
289
  :return: one of the flag's values in any type in any type of string, bool, float, json
237
290
  or the default value if flag evaluation fails
238
- :raises: ValueError if the default is not a string, boolean, numeric, or json type
291
+ Unsupported defaults are returned unchanged if evaluation cannot produce a flag value.
239
292
  """
240
293
  er = self._evaluate_internal(key, user, default)
241
294
  return cast_variation_by_flag_type(er.flag_type, er.value)
@@ -252,7 +305,7 @@ class FBClient:
252
305
  :param user: the attributes of the user
253
306
  :param default: the default value of the flag, to be used if the return value is not available
254
307
  :return: an :class:`fbclient.common_types.EvalDetail` object
255
- :raises: ValueError if the default is not a string, boolean, numeric, or json type
308
+ Unsupported defaults are returned unchanged if evaluation cannot produce a flag value.
256
309
  """
257
310
  return self._evaluate_internal(key, user, default).to_evail_detail
258
311
 
@@ -318,7 +371,11 @@ class FBClient:
318
371
  schedules the next event delivery to be as soon as possible; however, the delivery still
319
372
  happens asynchronously on a thread, so this method will return immediately.
320
373
  """
321
- self._event_processor.flush()
374
+ try:
375
+ if not self._closed:
376
+ self._event_processor.flush()
377
+ except Exception:
378
+ log.exception('FB Python SDK: event processor flush failed')
322
379
 
323
380
  def identify(self, user: dict):
324
381
  """register an end user in the feature flag center
@@ -352,7 +409,6 @@ class FBClient:
352
409
  log.warning('FB Python SDK: user invalid')
353
410
  return
354
411
 
355
- fb_user = FBUser.from_dict(user)
356
412
  metric_event = MetricEvent(fb_user).add(Metric(event_name, metric_value))
357
413
  self._event_handler(metric_event)
358
414
 
@@ -385,10 +441,15 @@ class FBClient:
385
441
  :param json_str: feature flags, segments...etc in the json format
386
442
  :return: True if the initialization is well done
387
443
  """
388
- if self._config.is_offline:
389
- all_data = json.loads(json_str)
390
- if valide_all_data(all_data):
391
- version, data = _data_to_dict(all_data['data'])
392
- return self._update_status_provider.init(data, version)
444
+ try:
445
+ if self._config.is_offline:
446
+ all_data = json.loads(json_str)
447
+ if valide_all_data(all_data):
448
+ version, data = _data_to_dict(all_data['data'])
449
+ if self._update_status_provider.init(data, version):
450
+ self._update_status_provider.update_state(State.ok_state())
451
+ return True
452
+ except Exception:
453
+ log.exception('FB Python SDK: invalid external bootstrap data')
393
454
 
394
455
  return False
@@ -90,19 +90,21 @@ class EvalDetail(Jsonfy):
90
90
  reason: str,
91
91
  variation: Any,
92
92
  key_name: Optional[str] = None,
93
- name: Optional[str] = None):
93
+ name: Optional[str] = None,
94
+ variation_id: Optional[str] = None):
94
95
  """Constructs an instance.
95
96
 
96
- :param id: variation id
97
97
  :param reason: main factor that influenced the flag evaluation value
98
98
  :param variation: result of the flag evaluation in any type of string, bool, float/int, json(Python object) or default value if flag evaluation fails
99
99
  :param key_name: key name of the flag
100
100
  :param name: name of the flag
101
+ :param variation_id: stable identifier of the resolved variation, or None if evaluation failed
101
102
  """
102
103
  self._reason = reason
103
104
  self._variation = variation
104
105
  self._key_name = key_name
105
106
  self._name = name
107
+ self._variation_id = variation_id
106
108
 
107
109
  @property
108
110
  def reason(self) -> str:
@@ -129,6 +131,12 @@ class EvalDetail(Jsonfy):
129
131
  """
130
132
  return self._name
131
133
 
134
+ @property
135
+ def variation_id(self) -> Optional[str]:
136
+ """The stable identifier of the resolved variation, if evaluation succeeded.
137
+ """
138
+ return self._variation_id
139
+
132
140
  def to_json_dict(self) -> dict:
133
141
  json_dict = {}
134
142
  json_dict['reason'] = self.reason
@@ -329,7 +337,8 @@ class _EvalResult:
329
337
  @property
330
338
  def to_evail_detail(self) -> "EvalDetail":
331
339
  _value = cast_variation_by_flag_type(self.__flag_type, self.__value)
332
- return EvalDetail(self.__reason, _value, self.__key_name, self.__name)
340
+ variation_id = self.__id if self.is_success else None
341
+ return EvalDetail(self.__reason, _value, self.__key_name, self.__name, variation_id)
333
342
 
334
343
  @property
335
344
  def to_flag_state(self) -> "FlagState":