Skip to content

Commit 320e723

Browse files
committed
[sdk-1710] convert some existing lock implementation to RWlock if they benefit
1 parent 76f9eb9 commit 320e723

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

ldclient/impl/datasystem/store.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
ChangeSet applications and flag change notifications.
77
"""
88

9-
import threading
109
from collections import defaultdict
1110
from typing import Any, Callable, Dict, List, Optional, Set
1211

@@ -194,7 +193,7 @@ def __init__(
194193
self._selector = Selector.no_selector()
195194

196195
# Thread synchronization
197-
self._lock = threading.RLock()
196+
self._lock = ReadWriteLock()
198197

199198
def with_persistence(
200199
self,
@@ -213,7 +212,7 @@ def with_persistence(
213212
Returns:
214213
Self for method chaining
215214
"""
216-
with self._lock:
215+
with self._lock.write_lock():
217216
self._persistent_store = persistent_store
218217
self._persistent_store_writable = writable
219218
self._persistent_store_status_provider = status_provider
@@ -225,12 +224,12 @@ def with_persistence(
225224

226225
def selector(self) -> Selector:
227226
"""Returns the current selector."""
228-
with self._lock:
227+
with self._lock.read_lock():
229228
return self._selector
230229

231230
def close(self) -> Optional[Exception]:
232231
"""Close the store and any persistent store if configured."""
233-
with self._lock:
232+
with self._lock.write_lock():
234233
if self._persistent_store is not None:
235234
try:
236235
# Most FeatureStore implementations don't have close methods
@@ -251,7 +250,7 @@ def apply(self, change_set: ChangeSet, persist: bool) -> None:
251250
"""
252251
collections = self._changes_to_store_data(change_set.changes)
253252

254-
with self._lock:
253+
with self._lock.write_lock():
255254
try:
256255
if change_set.intent_code == IntentCode.TRANSFER_FULL:
257256
self._set_basis(collections, change_set.selector, persist)
@@ -443,7 +442,7 @@ def __mapping(data: Dict[str, ModelEntity]) -> Dict[str, Dict[str, Any]]:
443442

444443
return __mapping
445444

446-
with self._lock:
445+
with self._lock.write_lock():
447446
if self._should_persist():
448447
try:
449448
# Get all data from memory store and write to persistent store
@@ -457,7 +456,7 @@ def __mapping(data: Dict[str, ModelEntity]) -> Dict[str, Dict[str, Any]]:
457456

458457
def get_active_store(self) -> ReadOnlyStore:
459458
"""Get the currently active store for reading data."""
460-
with self._lock:
459+
with self._lock.read_lock():
461460
return self._active_store
462461

463462
def is_initialized(self) -> bool:
@@ -466,5 +465,5 @@ def is_initialized(self) -> bool:
466465

467466
def get_data_store_status_provider(self) -> Optional[DataStoreStatusProvider]:
468467
"""Get the data store status provider for the persistent store, if configured."""
469-
with self._lock:
468+
with self._lock.read_lock():
470469
return self._persistent_store_status_provider

ldclient/impl/listeners.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from threading import RLock
21
from typing import Any, Callable
32

3+
from ldclient.impl.rwlock import ReadWriteLock
44
from ldclient.impl.util import log
55

66

@@ -12,25 +12,25 @@ class Listeners:
1212

1313
def __init__(self):
1414
self.__listeners = []
15-
self.__lock = RLock()
15+
self.__lock = ReadWriteLock()
1616

1717
def has_listeners(self) -> bool:
18-
with self.__lock:
18+
with self.__lock.read_lock():
1919
return len(self.__listeners) > 0
2020

2121
def add(self, listener: Callable):
22-
with self.__lock:
22+
with self.__lock.write_lock():
2323
self.__listeners.append(listener)
2424

2525
def remove(self, listener: Callable):
26-
with self.__lock:
26+
with self.__lock.write_lock():
2727
try:
2828
self.__listeners.remove(listener)
2929
except ValueError:
3030
pass # removing a listener that wasn't in the list is a no-op
3131

3232
def notify(self, value: Any):
33-
with self.__lock:
33+
with self.__lock.read_lock():
3434
listeners_copy = self.__listeners.copy()
3535
for listener in listeners_copy:
3636
try:

0 commit comments

Comments
 (0)