database-wrapper-redis 0.2.33__tar.gz → 0.2.38__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: database_wrapper_redis
3
- Version: 0.2.33
3
+ Version: 0.2.38
4
4
  Summary: database_wrapper for Redis database
5
5
  Author-email: Gints Murans <gm@gm.lv>
6
6
  License: GNU General Public License v3.0 (GPL-3.0)
@@ -32,8 +32,8 @@ Classifier: Topic :: Software Development
32
32
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
33
  Requires-Python: >=3.8
34
34
  Description-Content-Type: text/markdown
35
- Requires-Dist: database_wrapper==0.2.33
36
- Requires-Dist: redis
35
+ Requires-Dist: database_wrapper==0.2.38
36
+ Requires-Dist: redis>=8.0
37
37
 
38
38
  # database_wrapper_redis
39
39
 
@@ -1,3 +1,4 @@
1
+ import asyncio
1
2
  import logging
2
3
  from contextvars import ContextVar
3
4
  from threading import Event
@@ -146,36 +147,42 @@ class KVDbBase:
146
147
  def __enter__(self) -> RedisClient:
147
148
  self.logger.debug("Creating redis connection")
148
149
 
150
+ # Connections are kept in a per-context stack so that nested
151
+ # context managers each get and close their own connection
149
152
  connection = self.new_connection()
150
- self.context_connection.set(connection)
153
+ stack = self.context_connection.get() or ()
154
+ self.context_connection.set(stack + (connection,))
151
155
  return connection
152
156
 
153
157
  def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
154
158
  self.logger.debug("Releasing redis connection")
155
159
 
156
- redis_conn = self.context_connection.get()
157
- if redis_conn:
158
- redis_conn.close()
159
-
160
- # Reset context
161
- self.context_connection.set(None)
160
+ stack = self.context_connection.get() or ()
161
+ if stack:
162
+ redis_conn = stack[-1]
163
+ self.context_connection.set(stack[:-1])
164
+ if redis_conn:
165
+ redis_conn.close()
162
166
 
163
167
  async def __aenter__(self) -> RedisClientAsync:
164
168
  self.logger.debug("Creating redis connection")
165
169
 
170
+ # Connections are kept in a per-context stack so that nested
171
+ # context managers each get and close their own connection
166
172
  connection = await self.new_connection()
167
- self.context_connection.set(connection)
173
+ stack = self.context_connection.get() or ()
174
+ self.context_connection.set(stack + (connection,))
168
175
  return connection
169
176
 
170
177
  async def __aexit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
171
178
  self.logger.debug("Releasing redis connection")
172
179
 
173
- redis_conn = self.context_connection.get()
174
- if redis_conn:
175
- await redis_conn.aclose()
176
-
177
- # Reset context
178
- self.context_connection.set(None)
180
+ stack = self.context_connection.get() or ()
181
+ if stack:
182
+ redis_conn = stack[-1]
183
+ self.context_connection.set(stack[:-1])
184
+ if redis_conn:
185
+ await redis_conn.aclose()
179
186
 
180
187
  ###############
181
188
  ### Helpers ###
@@ -216,7 +223,7 @@ class KVDbBase:
216
223
 
217
224
 
218
225
  class RedisDb(KVDbBase):
219
- context_connection: ContextVar[RedisClient | None]
226
+ context_connection: ContextVar[tuple[RedisClient, ...] | None]
220
227
 
221
228
  _connection: RedisClient
222
229
 
@@ -267,15 +274,27 @@ class RedisDb(KVDbBase):
267
274
  self.logger.error(f"{logBuffer}ERR ({e})")
268
275
  self.shutdown_requested.wait(self.timeout)
269
276
  tries += 1
270
- if tries > 3:
277
+ if tries > self.config["instant_retries"]:
271
278
  raise
272
279
  continue
273
280
 
274
281
  raise Exception("Cancelled connection creation")
275
282
 
283
+ def close(self) -> None:
284
+ """
285
+ Close connection to the database.
286
+ """
287
+ try:
288
+ if self._connection:
289
+ self._connection.close()
290
+ except Exception as e:
291
+ self.logger.debug(f"Error while closing connection: {e}")
292
+ finally:
293
+ self._connection = None
294
+
276
295
 
277
296
  class RedisDbAsync(KVDbBase):
278
- context_connection: ContextVar[RedisClientAsync | None]
297
+ context_connection: ContextVar[tuple[RedisClientAsync, ...] | None]
279
298
 
280
299
  _connection: RedisClientAsync | None
281
300
 
@@ -297,7 +316,8 @@ class RedisDbAsync(KVDbBase):
297
316
  self.logger.debug("Dealloc")
298
317
 
299
318
  # Just to be sure as async does not have __del__
300
- del self._connection
319
+ if hasattr(self, "_connection"):
320
+ del self._connection
301
321
 
302
322
  async def new_connection(self) -> RedisClientAsync:
303
323
  tries = 0
@@ -330,9 +350,11 @@ class RedisDbAsync(KVDbBase):
330
350
  await redis_conn.close()
331
351
 
332
352
  self.logger.error(f"ERR ({e})", extra={"clean": True})
333
- self.shutdown_requested.wait(self.timeout)
353
+ # Event.wait() would block the whole event loop; the loop condition
354
+ # re-checks shutdown_requested after the sleep
355
+ await asyncio.sleep(self.timeout)
334
356
  tries += 1
335
- if tries > 3:
357
+ if tries > self.config["instant_retries"]:
336
358
  raise
337
359
  continue
338
360
 
@@ -359,7 +381,7 @@ class RedisDbAsync(KVDbBase):
359
381
 
360
382
  class RedisDbWithPool(KVDbBase):
361
383
  pool: RedisConnectionPool
362
- context_connection: ContextVar[RedisClient | None]
384
+ context_connection: ContextVar[tuple[RedisClient, ...] | None]
363
385
 
364
386
  _connection: RedisClient | None
365
387
 
@@ -411,7 +433,7 @@ class RedisDbWithPool(KVDbBase):
411
433
  self.logger.error(f"{logBuffer}ERR ({e})")
412
434
  self.shutdown_requested.wait(self.timeout)
413
435
  tries += 1
414
- if tries > 3:
436
+ if tries > self.config["instant_retries"]:
415
437
  raise
416
438
  continue
417
439
 
@@ -430,7 +452,7 @@ class RedisDbWithPool(KVDbBase):
430
452
 
431
453
  class RedisDbWithPoolAsync(KVDbBase):
432
454
  pool: RedisConnectionPoolAsync
433
- context_connection: ContextVar[RedisClientAsync | None]
455
+ context_connection: ContextVar[tuple[RedisClientAsync, ...] | None]
434
456
 
435
457
  _connection: RedisClientAsync | None
436
458
 
@@ -468,8 +490,11 @@ class RedisDbWithPoolAsync(KVDbBase):
468
490
  self.logger.debug("Dealloc")
469
491
 
470
492
  # Just to be sure as async does not have __del__
471
- del self._connection
472
- del self.pool
493
+ if hasattr(self, "_connection"):
494
+ del self._connection
495
+
496
+ if hasattr(self, "pool"):
497
+ del self.pool
473
498
 
474
499
  async def new_connection(self) -> RedisClientAsync:
475
500
  tries = 0
@@ -487,9 +512,11 @@ class RedisDbWithPoolAsync(KVDbBase):
487
512
  await redis_conn.close()
488
513
 
489
514
  self.logger.error(f"ERR ({e})", extra={"clean": True})
490
- self.shutdown_requested.wait(self.timeout)
515
+ # Event.wait() would block the whole event loop; the loop condition
516
+ # re-checks shutdown_requested after the sleep
517
+ await asyncio.sleep(self.timeout)
491
518
  tries += 1
492
- if tries > 3:
519
+ if tries > self.config["instant_retries"]:
493
520
  raise
494
521
  continue
495
522
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: database_wrapper_redis
3
- Version: 0.2.33
3
+ Version: 0.2.38
4
4
  Summary: database_wrapper for Redis database
5
5
  Author-email: Gints Murans <gm@gm.lv>
6
6
  License: GNU General Public License v3.0 (GPL-3.0)
@@ -32,8 +32,8 @@ Classifier: Topic :: Software Development
32
32
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
33
  Requires-Python: >=3.8
34
34
  Description-Content-Type: text/markdown
35
- Requires-Dist: database_wrapper==0.2.33
36
- Requires-Dist: redis
35
+ Requires-Dist: database_wrapper==0.2.38
36
+ Requires-Dist: redis>=8.0
37
37
 
38
38
  # database_wrapper_redis
39
39
 
@@ -0,0 +1,2 @@
1
+ database_wrapper==0.2.38
2
+ redis>=8.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "database_wrapper_redis"
7
- version = "0.2.33"
7
+ version = "0.2.38"
8
8
  description = "database_wrapper for Redis database"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -32,7 +32,7 @@ classifiers = [
32
32
  "Topic :: Software Development :: Libraries :: Python Modules",
33
33
  ]
34
34
  keywords = ["database", "wrapper", "python", "redis"]
35
- dependencies = ["database_wrapper == 0.2.33", "redis"]
35
+ dependencies = ["database_wrapper == 0.2.38", "redis >= 8.0"]
36
36
 
37
37
  [project.urls]
38
38
  Homepage = "https://github.com/gintsmurans/py_database_wrapper"
@@ -1,2 +0,0 @@
1
- database_wrapper==0.2.33
2
- redis