redis-allocator 0.4.0__py3-none-any.whl → 0.4.2__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.
@@ -1 +1 @@
1
- __version__ = '0.4.0'
1
+ __version__ = '0.4.2'
@@ -199,6 +199,8 @@ class RedisAllocatorObject(Generic[U]):
199
199
  self.key = new_obj.key
200
200
  self.params = new_obj.params
201
201
  self.open()
202
+ elif self.obj is not None:
203
+ logger.error("Failed to refresh the object %s", self.key)
202
204
 
203
205
  def refresh_until_healthy(self, timeout: Timeout = 120, max_attempts: int = 10, lock_duration: Timeout = 3600, cache_timeout: Timeout = 3600):
204
206
  """Refresh the object until it is healthy."""
@@ -226,9 +228,9 @@ class RedisAllocatorObject(Generic[U]):
226
228
  return None
227
229
  return self.obj.name
228
230
 
229
- def __del__(self):
230
- """Delete the object."""
231
- self.close()
231
+ # def __del__(self):
232
+ # """Delete the object."""
233
+ # self.close()
232
234
 
233
235
 
234
236
  class RedisAllocatorUpdater:
@@ -257,14 +259,14 @@ class RedisAllocatorUpdater:
257
259
  return len(self.params)
258
260
 
259
261
 
260
- class RedisAllocatorPolicy(ABC):
262
+ class RedisAllocatorPolicy(ABC, Generic[U]):
261
263
  """Abstract base class for Redis allocator policies.
262
264
 
263
265
  This class defines the interface for allocation policies that can be used
264
266
  with RedisAllocator to control allocation behavior.
265
267
  """
266
268
 
267
- def initialize(self, allocator: 'RedisAllocator'):
269
+ def initialize(self, allocator: 'RedisAllocator[U]'):
268
270
  """Initialize the policy with an allocator instance.
269
271
 
270
272
  Args:
@@ -273,9 +275,9 @@ class RedisAllocatorPolicy(ABC):
273
275
  pass
274
276
 
275
277
  @abstractmethod
276
- def malloc(self, allocator: 'RedisAllocator', timeout: Timeout = 120,
277
- obj: Optional[Any] = None, params: Optional[dict] = None,
278
- cache_timeout: Timeout = 3600) -> Optional[RedisAllocatorObject]:
278
+ def malloc(self, allocator: 'RedisAllocator[U]', timeout: Timeout = 120,
279
+ obj: Optional[U] = None, params: Optional[dict] = None,
280
+ cache_timeout: Timeout = 3600) -> Optional[RedisAllocatorObject[U]]:
279
281
  """Allocate a resource according to the policy.
280
282
 
281
283
  Args:
@@ -292,7 +294,7 @@ class RedisAllocatorPolicy(ABC):
292
294
  pass
293
295
 
294
296
  @abstractmethod
295
- def refresh_pool(self, allocator: 'RedisAllocator'):
297
+ def refresh_pool(self, allocator: 'RedisAllocator[U]'):
296
298
  """Refresh the allocation pool.
297
299
 
298
300
  This method is called periodically to update the pool with new resources.
@@ -302,7 +304,7 @@ class RedisAllocatorPolicy(ABC):
302
304
  """
303
305
  pass
304
306
 
305
- def check_health_once(self, r_obj: RedisAllocatorObject, duration: int = 3600) -> bool:
307
+ def check_health_once(self, r_obj: RedisAllocatorObject[U], duration: int = 3600) -> bool:
306
308
  """Check the health of the object."""
307
309
  with contextlib.closing(r_obj.open()):
308
310
  try:
@@ -318,8 +320,8 @@ class RedisAllocatorPolicy(ABC):
318
320
  r_obj.set_unhealthy(duration)
319
321
  raise
320
322
 
321
- def check_health(self, allocator: 'RedisAllocator', lock_duration: Timeout = 3600, max_threads: int = 8,
322
- obj_fn: Optional[Callable[[str], Any]] = None,
323
+ def check_health(self, allocator: 'RedisAllocator[U]', lock_duration: Timeout = 3600, max_threads: int = 8,
324
+ obj_fn: Optional[Callable[[str], U]] = None,
323
325
  params_fn: Optional[Callable[[str], dict]] = None) -> tuple[int, int]:
324
326
  """Check the health of the allocator.
325
327
 
@@ -349,7 +351,7 @@ class RedisAllocatorPolicy(ABC):
349
351
  return healthy, unhealthy
350
352
 
351
353
 
352
- class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy):
354
+ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy[U]):
353
355
  """Default implementation of RedisAllocatorPolicy.
354
356
 
355
357
  This policy provides the following features:
@@ -393,7 +395,7 @@ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy):
393
395
  self.objects: weakref.WeakValueDictionary[str, RedisAllocatorObject] = weakref.WeakValueDictionary()
394
396
  self.auto_close = auto_close
395
397
 
396
- def initialize(self, allocator: 'RedisAllocator'):
398
+ def initialize(self, allocator: 'RedisAllocator[U]'):
397
399
  """Initialize the policy with an allocator instance.
398
400
 
399
401
  Args:
@@ -403,9 +405,9 @@ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy):
403
405
  self._update_lock_key = f"{allocator._pool_str()}|policy_update_lock"
404
406
  atexit.register(lambda: self.finalize(self._allocator()))
405
407
 
406
- def malloc(self, allocator: 'RedisAllocator', timeout: Timeout = 120,
407
- obj: Optional[Any] = None, params: Optional[dict] = None,
408
- cache_timeout: Timeout = 3600) -> Optional[RedisAllocatorObject]:
408
+ def malloc(self, allocator: 'RedisAllocator[U]', timeout: Timeout = 120,
409
+ obj: Optional[U] = None, params: Optional[dict] = None,
410
+ cache_timeout: Timeout = 3600) -> Optional[RedisAllocatorObject[U]]:
409
411
  """Allocate a resource according to the policy.
410
412
 
411
413
  This implementation:
@@ -445,7 +447,7 @@ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy):
445
447
  return alloc_obj
446
448
 
447
449
  @cached(TTLCache(maxsize=64, ttl=5))
448
- def _try_refresh_pool(self, allocator: 'RedisAllocator'):
450
+ def _try_refresh_pool(self, allocator: 'RedisAllocator[U]'):
449
451
  """Try to refresh the pool if necessary and if we can acquire the lock.
450
452
 
451
453
  Args:
@@ -457,7 +459,7 @@ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy):
457
459
  # If we got here, we acquired the lock, so we can update the pool
458
460
  self.refresh_pool(allocator)
459
461
 
460
- def refresh_pool(self, allocator: 'RedisAllocator'):
462
+ def refresh_pool(self, allocator: 'RedisAllocator[U]'):
461
463
  """Refresh the allocation pool using the updater.
462
464
 
463
465
  Args:
@@ -478,7 +480,7 @@ class DefaultRedisAllocatorPolicy(RedisAllocatorPolicy):
478
480
  else:
479
481
  allocator.extend(keys, timeout=self.expiry_duration)
480
482
 
481
- def finalize(self, allocator: 'RedisAllocator'):
483
+ def finalize(self, allocator: 'RedisAllocator[U]'):
482
484
  """Finalize the policy."""
483
485
  for obj in self.objects.values():
484
486
  obj.close()
@@ -521,7 +523,7 @@ class RedisAllocator(RedisLockPool, Generic[U]):
521
523
  """
522
524
 
523
525
  def __init__(self, redis: Redis, prefix: str, suffix='allocator', eps=1e-6,
524
- shared=False, policy: Optional[RedisAllocatorPolicy] = None):
526
+ shared=False, policy: Optional[RedisAllocatorPolicy[U]] = None):
525
527
  """Initializes the RedisAllocator.
526
528
 
527
529
  Args:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: redis-allocator
3
- Version: 0.4.0
3
+ Version: 0.4.2
4
4
  Summary: Redis-based resource allocation system.
5
5
  Home-page: https://github.com/invoker-bot/RedisAllocator-python
6
6
  Author: Invoker Bot
@@ -1,15 +1,15 @@
1
1
  redis_allocator/__init__.py,sha256=TVjUm-8YEu_MQD_PkfeIKiVknpCJBrUY9cWN1LlaZcU,1016
2
- redis_allocator/_version.py,sha256=2eiWQI55fd-roDdkt4Hvl9WzrTJ4xQo33VzFud6D03U,22
3
- redis_allocator/allocator.py,sha256=Bo4Ck7mqg-Z8KdQH-iqeHfdEbyhMqBD0c2g5T3E5sBU,48730
2
+ redis_allocator/_version.py,sha256=zrcTwxP3SA3F1Js78afrfeaZeJr8w2hLtFtEZoUqUmg,22
3
+ redis_allocator/allocator.py,sha256=RL1opmUfi5OFDmCe43O_aK--st0Wfi55t4rK7YzT2S4,48889
4
4
  redis_allocator/lock.py,sha256=fqf6WUWHKYenEArWopMIF6kWEnDfADC-bZvnQImsQVo,27400
5
5
  redis_allocator/task_queue.py,sha256=8DjNr2uxhzCsHatV_CHOeGh7_K9pqQZFApSbe2blRO0,14989
6
- redis_allocator-0.4.0.dist-info/licenses/LICENSE,sha256=Wt4X1rHpffQfEiyWcDUx8BMLjXxfPqaiYZ7Lgsj7L4c,1068
6
+ redis_allocator-0.4.2.dist-info/licenses/LICENSE,sha256=Wt4X1rHpffQfEiyWcDUx8BMLjXxfPqaiYZ7Lgsj7L4c,1068
7
7
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  tests/conftest.py,sha256=Ts82uylQSzP_GcaN0E02o3xcFdjw20cXNzh3RAdYKW4,3967
9
- tests/test_allocator.py,sha256=hFKgLe_yONtEjjm6zssUnhK0tzihG_1xZMziztHmqqA,22404
9
+ tests/test_allocator.py,sha256=_aoSDkzyjiUyzw6WoO2bMV1Ow9ymLOOMSJrp7TENU70,22139
10
10
  tests/test_lock.py,sha256=MDMRNN46VhWqkHUIhYOMEDgZkFFCW_WjwRLTOjkFF-Q,46952
11
11
  tests/test_task_queue.py,sha256=Fh5naikFajfOvL6GngEy_TPfOYCYZolZfVwtR6T4dTY,31710
12
- redis_allocator-0.4.0.dist-info/METADATA,sha256=rXeA3Nsb6eie8H5G10HXLlLI8jCRs45I1C06seN-Sgs,21727
13
- redis_allocator-0.4.0.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
14
- redis_allocator-0.4.0.dist-info/top_level.txt,sha256=0hXzU7sK5FCeSolTEYxThOt3HOybnwaXv1FLRJvHVgI,22
15
- redis_allocator-0.4.0.dist-info/RECORD,,
12
+ redis_allocator-0.4.2.dist-info/METADATA,sha256=2i_PpL_MD-TCX1yVddl9YObBPtatBrRQQNYImBAeB5M,21727
13
+ redis_allocator-0.4.2.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
14
+ redis_allocator-0.4.2.dist-info/top_level.txt,sha256=0hXzU7sK5FCeSolTEYxThOt3HOybnwaXv1FLRJvHVgI,22
15
+ redis_allocator-0.4.2.dist-info/RECORD,,
tests/test_allocator.py CHANGED
@@ -214,13 +214,8 @@ class TestRedisAllocatorObject:
214
214
  obj.close()
215
215
  assert test_object.closed
216
216
  obj.close() # Should not raise
217
-
218
- def test_del(self, redis_allocator: RedisAllocator, test_object: _TestObject, mocker: MockFixture):
219
- """Test the __del__ method."""
220
- obj = RedisAllocatorObject(redis_allocator, "test_key", test_object, {})
221
- obj.close = mocker.MagicMock()
222
- obj.__del__()
223
- obj.close.assert_called_once()
217
+ obj.refresh()
218
+ assert not test_object.closed
224
219
 
225
220
 
226
221
  class TestRedisAllocator: