redis-allocator 0.5.1__tar.gz → 0.6.0__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 (22) hide show
  1. {redis_allocator-0.5.1/redis_allocator.egg-info → redis_allocator-0.6.0}/PKG-INFO +1 -1
  2. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/pyproject.toml +1 -1
  3. redis_allocator-0.6.0/redis_allocator/_version.py +1 -0
  4. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/redis_allocator/allocator.py +10 -7
  5. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/redis_allocator/lock.py +0 -14
  6. {redis_allocator-0.5.1 → redis_allocator-0.6.0/redis_allocator.egg-info}/PKG-INFO +1 -1
  7. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/tests/test_allocator.py +1 -1
  8. redis_allocator-0.5.1/redis_allocator/_version.py +0 -1
  9. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/LICENSE +0 -0
  10. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/README.md +0 -0
  11. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/redis_allocator/__init__.py +0 -0
  12. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/redis_allocator/task_queue.py +0 -0
  13. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/redis_allocator.egg-info/SOURCES.txt +0 -0
  14. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/redis_allocator.egg-info/dependency_links.txt +0 -0
  15. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/redis_allocator.egg-info/requires.txt +0 -0
  16. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/redis_allocator.egg-info/top_level.txt +0 -0
  17. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/setup.cfg +0 -0
  18. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/setup.py +0 -0
  19. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/tests/__init__.py +0 -0
  20. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/tests/conftest.py +0 -0
  21. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/tests/test_lock.py +0 -0
  22. {redis_allocator-0.5.1 → redis_allocator-0.6.0}/tests/test_task_queue.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: redis-allocator
3
- Version: 0.5.1
3
+ Version: 0.6.0
4
4
  Summary: Redis-based resource allocation system.
5
5
  Home-page: https://github.com/invoker-bot/RedisAllocator-python
6
6
  Author: Invoker Bot
@@ -4,7 +4,7 @@ build-backend = 'setuptools.build_meta'
4
4
 
5
5
  [tool.commitizen]
6
6
  name = "cz_conventional_commits"
7
- version = "0.5.1"
7
+ version = "0.6.0"
8
8
  tag_format = "v$version"
9
9
 
10
10
  [tool.commitizen.version_files]
@@ -0,0 +1 @@
1
+ __version__ = '0.6.0'
@@ -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
- for _ in range(self.updater.params):
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 = self.updater()
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)
@@ -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."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: redis-allocator
3
- Version: 0.5.1
3
+ Version: 0.6.0
4
4
  Summary: Redis-based resource allocation system.
5
5
  Home-page: https://github.com/invoker-bot/RedisAllocator-python
6
6
  Author: Invoker Bot
@@ -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 +0,0 @@
1
- __version__ = '0.5.1'
File without changes