redis-allocator 0.5.1__py3-none-any.whl → 0.6.0__py3-none-any.whl
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.
- redis_allocator/_version.py +1 -1
- redis_allocator/allocator.py +10 -7
- redis_allocator/lock.py +0 -14
- {redis_allocator-0.5.1.dist-info → redis_allocator-0.6.0.dist-info}/METADATA +1 -1
- redis_allocator-0.6.0.dist-info/RECORD +15 -0
- {redis_allocator-0.5.1.dist-info → redis_allocator-0.6.0.dist-info}/WHEEL +1 -1
- tests/test_allocator.py +1 -1
- redis_allocator-0.5.1.dist-info/RECORD +0 -15
- {redis_allocator-0.5.1.dist-info → redis_allocator-0.6.0.dist-info}/licenses/LICENSE +0 -0
- {redis_allocator-0.5.1.dist-info → redis_allocator-0.6.0.dist-info}/top_level.txt +0 -0
redis_allocator/_version.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
__version__ = '0.
|
1
|
+
__version__ = '0.6.0'
|
redis_allocator/allocator.py
CHANGED
@@ -17,17 +17,16 @@ Key features:
|
|
17
17
|
4. Support for an updater to refresh the pool's keys periodically
|
18
18
|
5. Policy-based control of allocation behavior through RedisAllocatorPolicy
|
19
19
|
"""
|
20
|
+
import random
|
20
21
|
import atexit
|
21
22
|
import logging
|
22
23
|
import weakref
|
23
24
|
import contextlib
|
24
25
|
from abc import ABC, abstractmethod
|
25
|
-
from typing import Any, Callable
|
26
|
+
from typing import Any, Callable, Optional, TypeVar, Generic, Sequence, Iterable
|
26
27
|
from functools import cached_property
|
27
28
|
from threading import current_thread
|
28
29
|
from concurrent.futures import ThreadPoolExecutor
|
29
|
-
from typing import (Optional, TypeVar, Generic,
|
30
|
-
Sequence, Iterable)
|
31
30
|
from redis import StrictRedis as Redis
|
32
31
|
from cachetools import cached, TTLCache
|
33
32
|
from .lock import RedisLockPool, Timeout
|
@@ -412,8 +411,7 @@ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy[U]):
|
|
412
411
|
|
413
412
|
def refresh_pool_all(self, allocator: 'RedisAllocator[U]'):
|
414
413
|
allocator.clear()
|
415
|
-
|
416
|
-
self.refresh_pool(allocator)
|
414
|
+
self.refresh_pool(allocator, n=len(self.updater.params))
|
417
415
|
|
418
416
|
def malloc(self, allocator: 'RedisAllocator[U]', timeout: Timeout = 120,
|
419
417
|
obj: Optional[U] = None, params: Optional[dict] = None,
|
@@ -469,7 +467,7 @@ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy[U]):
|
|
469
467
|
# If we got here, we acquired the lock, so we can update the pool
|
470
468
|
self.refresh_pool(allocator)
|
471
469
|
|
472
|
-
def refresh_pool(self, allocator: 'RedisAllocator[U]'):
|
470
|
+
def refresh_pool(self, allocator: 'RedisAllocator[U]', shuffle=True, n=1):
|
473
471
|
"""Refresh the allocation pool using the updater.
|
474
472
|
|
475
473
|
Args:
|
@@ -478,12 +476,17 @@ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy[U]):
|
|
478
476
|
if self.updater is None:
|
479
477
|
return
|
480
478
|
|
481
|
-
keys =
|
479
|
+
keys = []
|
480
|
+
for _ in range(n):
|
481
|
+
keys.extend(self.updater())
|
482
482
|
|
483
483
|
if len(keys) == 0:
|
484
484
|
logger.warning("No keys to update to the pool")
|
485
485
|
return
|
486
486
|
|
487
|
+
if shuffle:
|
488
|
+
random.shuffle(keys)
|
489
|
+
|
487
490
|
# Update the pool based on the number of keys
|
488
491
|
if len(self.updater) == 1:
|
489
492
|
allocator.assign(keys, timeout=self.expiry_duration)
|
redis_allocator/lock.py
CHANGED
@@ -62,7 +62,6 @@ class BaseLock(ABC):
|
|
62
62
|
Returns:
|
63
63
|
The current status of the key.
|
64
64
|
"""
|
65
|
-
pass
|
66
65
|
|
67
66
|
@abstractmethod
|
68
67
|
def update(self, key: str, value='1', timeout: Timeout = 120):
|
@@ -73,7 +72,6 @@ class BaseLock(ABC):
|
|
73
72
|
value: The value to set for the key.
|
74
73
|
timeout: The lock timeout in seconds.
|
75
74
|
"""
|
76
|
-
pass
|
77
75
|
|
78
76
|
@abstractmethod
|
79
77
|
def lock(self, key: str, value: str = '1', timeout: Timeout = 120) -> bool:
|
@@ -87,7 +85,6 @@ class BaseLock(ABC):
|
|
87
85
|
Returns:
|
88
86
|
True if the ownership of the key is successfully acquired, False otherwise.
|
89
87
|
"""
|
90
|
-
pass
|
91
88
|
|
92
89
|
@abstractmethod
|
93
90
|
def is_locked(self, key: str) -> bool:
|
@@ -99,7 +96,6 @@ class BaseLock(ABC):
|
|
99
96
|
Returns:
|
100
97
|
True if the key is locked, False otherwise.
|
101
98
|
"""
|
102
|
-
pass
|
103
99
|
|
104
100
|
@abstractmethod
|
105
101
|
def lock_value(self, key: str) -> Optional[str]:
|
@@ -111,7 +107,6 @@ class BaseLock(ABC):
|
|
111
107
|
Returns:
|
112
108
|
The value of the key if the key is locked, None otherwise.
|
113
109
|
"""
|
114
|
-
pass
|
115
110
|
|
116
111
|
@abstractmethod
|
117
112
|
def rlock(self, key: str, value: str = '1', timeout=120) -> bool:
|
@@ -127,7 +122,6 @@ class BaseLock(ABC):
|
|
127
122
|
Returns:
|
128
123
|
True if the ownership of the key is successfully acquired, False otherwise.
|
129
124
|
"""
|
130
|
-
pass
|
131
125
|
|
132
126
|
@abstractmethod
|
133
127
|
def unlock(self, key: str) -> bool:
|
@@ -139,7 +133,6 @@ class BaseLock(ABC):
|
|
139
133
|
Returns:
|
140
134
|
True if the key is successfully released, False if the key is not locked.
|
141
135
|
"""
|
142
|
-
pass
|
143
136
|
|
144
137
|
@abstractmethod
|
145
138
|
def _conditional_setdel(self, op: str, key: str, value: float, set_value: Optional[float] = None,
|
@@ -157,7 +150,6 @@ class BaseLock(ABC):
|
|
157
150
|
Returns:
|
158
151
|
Whether the operation was successful.
|
159
152
|
"""
|
160
|
-
pass
|
161
153
|
|
162
154
|
def setgt(self, key: str, value: float, set_value: Optional[float] = None, ex: Optional[int] = None) -> bool:
|
163
155
|
"""Sets a new value when the comparison value is greater than the current value."""
|
@@ -229,32 +221,26 @@ class BaseLockPool(BaseLock, metaclass=ABCMeta):
|
|
229
221
|
@abstractmethod
|
230
222
|
def extend(self, keys: Optional[Sequence[str]] = None):
|
231
223
|
"""Extend the pool with the specified keys."""
|
232
|
-
pass
|
233
224
|
|
234
225
|
@abstractmethod
|
235
226
|
def shrink(self, keys: Sequence[str]):
|
236
227
|
"""Shrink the pool by removing the specified keys."""
|
237
|
-
pass
|
238
228
|
|
239
229
|
@abstractmethod
|
240
230
|
def assign(self, keys: Optional[Sequence[str]] = None):
|
241
231
|
"""Assign keys to the pool, replacing any existing keys."""
|
242
|
-
pass
|
243
232
|
|
244
233
|
@abstractmethod
|
245
234
|
def clear(self):
|
246
235
|
"""Empty the pool."""
|
247
|
-
pass
|
248
236
|
|
249
237
|
@abstractmethod
|
250
238
|
def keys(self) -> Iterable[str]:
|
251
239
|
"""Get the keys in the pool."""
|
252
|
-
pass
|
253
240
|
|
254
241
|
@abstractmethod
|
255
242
|
def _get_key_lock_status(self, keys: Iterable[str]) -> Iterable[bool]:
|
256
243
|
"""Get the lock status of the specified keys."""
|
257
|
-
pass
|
258
244
|
|
259
245
|
def values_lock_status(self) -> Iterable[bool]:
|
260
246
|
"""Get the lock status of all keys in the pool."""
|
@@ -0,0 +1,15 @@
|
|
1
|
+
redis_allocator/__init__.py,sha256=TVjUm-8YEu_MQD_PkfeIKiVknpCJBrUY9cWN1LlaZcU,1016
|
2
|
+
redis_allocator/_version.py,sha256=CBY3jsC-9HCm7eZ6CKD-sYLCejqOJ1pYWPQM4LGIXcI,22
|
3
|
+
redis_allocator/allocator.py,sha256=DMB7roghIMlbPPMSE8dMgfX-u7R0hKEzx1vuIlmxi0A,53495
|
4
|
+
redis_allocator/lock.py,sha256=pq1nVPJ4JT5vATF5-5rNZ4eplsBNeHXSxo0-aj-cxsE,27218
|
5
|
+
redis_allocator/task_queue.py,sha256=8DjNr2uxhzCsHatV_CHOeGh7_K9pqQZFApSbe2blRO0,14989
|
6
|
+
redis_allocator-0.6.0.dist-info/licenses/LICENSE,sha256=Wt4X1rHpffQfEiyWcDUx8BMLjXxfPqaiYZ7Lgsj7L4c,1068
|
7
|
+
tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
8
|
+
tests/conftest.py,sha256=Ts82uylQSzP_GcaN0E02o3xcFdjw20cXNzh3RAdYKW4,3967
|
9
|
+
tests/test_allocator.py,sha256=TalMWgLhJw2NjwDMX5HJvIhDDmTGtSBvJjteOxCZatc,23365
|
10
|
+
tests/test_lock.py,sha256=MDMRNN46VhWqkHUIhYOMEDgZkFFCW_WjwRLTOjkFF-Q,46952
|
11
|
+
tests/test_task_queue.py,sha256=Fh5naikFajfOvL6GngEy_TPfOYCYZolZfVwtR6T4dTY,31710
|
12
|
+
redis_allocator-0.6.0.dist-info/METADATA,sha256=xe0yY79O-I4DnojK2Nt2l8NI_8mcXl6NEMuWzyYhOEY,21727
|
13
|
+
redis_allocator-0.6.0.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
|
14
|
+
redis_allocator-0.6.0.dist-info/top_level.txt,sha256=0hXzU7sK5FCeSolTEYxThOt3HOybnwaXv1FLRJvHVgI,22
|
15
|
+
redis_allocator-0.6.0.dist-info/RECORD,,
|
tests/test_allocator.py
CHANGED
@@ -434,7 +434,7 @@ class TestRedisAllocator:
|
|
434
434
|
allocator_with_policy.gc() # some times should be called twice to remove the expired items
|
435
435
|
redis_client.register_script("print('-------------------------------')")()
|
436
436
|
print(self.get_redis_pool_state(allocator_with_policy, redis_client))
|
437
|
-
allocator_with_policy.policy.refresh_pool(allocator_with_policy)
|
437
|
+
allocator_with_policy.policy.refresh_pool(allocator_with_policy, shuffle=False)
|
438
438
|
assert len(allocator_with_policy) == 4
|
439
439
|
allocator_with_policy.gc()
|
440
440
|
assert self.get_redis_pool_state(allocator_with_policy, redis_client) == self.generate_pool_state(
|
@@ -1,15 +0,0 @@
|
|
1
|
-
redis_allocator/__init__.py,sha256=TVjUm-8YEu_MQD_PkfeIKiVknpCJBrUY9cWN1LlaZcU,1016
|
2
|
-
redis_allocator/_version.py,sha256=s8Yq9Om1yBxrMA7xYQ5Y13Paeuxnq99NxhyjuPlnH6A,22
|
3
|
-
redis_allocator/allocator.py,sha256=TTjr8jcQpJt96RJkssRU60z0m1aUsqBlEud87W4DCkY,53414
|
4
|
-
redis_allocator/lock.py,sha256=fqf6WUWHKYenEArWopMIF6kWEnDfADC-bZvnQImsQVo,27400
|
5
|
-
redis_allocator/task_queue.py,sha256=8DjNr2uxhzCsHatV_CHOeGh7_K9pqQZFApSbe2blRO0,14989
|
6
|
-
redis_allocator-0.5.1.dist-info/licenses/LICENSE,sha256=Wt4X1rHpffQfEiyWcDUx8BMLjXxfPqaiYZ7Lgsj7L4c,1068
|
7
|
-
tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
8
|
-
tests/conftest.py,sha256=Ts82uylQSzP_GcaN0E02o3xcFdjw20cXNzh3RAdYKW4,3967
|
9
|
-
tests/test_allocator.py,sha256=zOBdaOhlpLsv63J4QZ9vOhL1k1gu_vO68x4CpPDa9_4,23350
|
10
|
-
tests/test_lock.py,sha256=MDMRNN46VhWqkHUIhYOMEDgZkFFCW_WjwRLTOjkFF-Q,46952
|
11
|
-
tests/test_task_queue.py,sha256=Fh5naikFajfOvL6GngEy_TPfOYCYZolZfVwtR6T4dTY,31710
|
12
|
-
redis_allocator-0.5.1.dist-info/METADATA,sha256=yI2vLDxF_5JULEbkUZ5XkVsIriC8qEwtLjbGDmp6Neg,21727
|
13
|
-
redis_allocator-0.5.1.dist-info/WHEEL,sha256=pxyMxgL8-pra_rKaQ4drOZAegBVuX-G_4nRHjjgWbmo,91
|
14
|
-
redis_allocator-0.5.1.dist-info/top_level.txt,sha256=0hXzU7sK5FCeSolTEYxThOt3HOybnwaXv1FLRJvHVgI,22
|
15
|
-
redis_allocator-0.5.1.dist-info/RECORD,,
|
File without changes
|
File without changes
|