django-vcache 2.2.0__tar.gz → 2.3.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.
@@ -725,7 +725,7 @@ dependencies = [
725
725
 
726
726
  [[package]]
727
727
  name = "rlib-link-test"
728
- version = "2.2.0"
728
+ version = "2.3.0"
729
729
  dependencies = [
730
730
  "pyo3",
731
731
  "vcache-pyo3",
@@ -1005,7 +1005,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
1005
1005
 
1006
1006
  [[package]]
1007
1007
  name = "vcache-core"
1008
- version = "2.2.0"
1008
+ version = "2.3.0"
1009
1009
  dependencies = [
1010
1010
  "redis",
1011
1011
  "rustls",
@@ -1014,7 +1014,7 @@ dependencies = [
1014
1014
 
1015
1015
  [[package]]
1016
1016
  name = "vcache-pyo3"
1017
- version = "2.2.0"
1017
+ version = "2.3.0"
1018
1018
  dependencies = [
1019
1019
  "pyo3",
1020
1020
  "redis",
@@ -3,7 +3,7 @@ resolver = "2"
3
3
  members = ["crates/vcache-core", "crates/vcache-pyo3"]
4
4
 
5
5
  [workspace.package]
6
- version = "2.2.0"
6
+ version = "2.3.0"
7
7
  edition = "2024"
8
8
 
9
9
  [workspace.dependencies]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: django-vcache
3
- Version: 2.2.0
3
+ Version: 2.3.0
4
4
  Classifier: Development Status :: 5 - Production/Stable
5
5
  Classifier: Framework :: Django
6
6
  Classifier: Framework :: Django :: 5.0
@@ -12,3 +12,8 @@ name = "vcache_core"
12
12
  tokio.workspace = true
13
13
  redis.workspace = true
14
14
  rustls.workspace = true
15
+
16
+ [dev-dependencies]
17
+ # For the runtime-provider integration test (builds a sentinel runtime to
18
+ # verify get_runtime delegates to an installed provider).
19
+ tokio.workspace = true
@@ -19,4 +19,4 @@ pub use connection::{
19
19
  connect_cluster, connect_sentinel, connect_standard, ClientCacheOpts, TlsOpts, ValkeyConn,
20
20
  };
21
21
  pub use result::RawResult;
22
- pub use runtime::get_runtime;
22
+ pub use runtime::{get_runtime, set_runtime_provider};
@@ -13,8 +13,34 @@ static RUNTIME: OnceLock<Runtime> = OnceLock::new();
13
13
  static RUNTIME_PID: AtomicU32 = AtomicU32::new(0);
14
14
  static FORK_RUNTIME: Mutex<Option<(u32, &'static Runtime)>> = Mutex::new(None);
15
15
 
16
+ /// Optional external runtime provider. Unset by default, so vanilla vcache
17
+ /// builds and owns its own runtime (below) — standalone users configure
18
+ /// nothing. When an embedding crate (e.g. a host `.so` that also runs a
19
+ /// postgres pool and wants a single process-wide tokio runtime) installs a
20
+ /// provider via [`set_runtime_provider`], [`get_runtime`] delegates to it
21
+ /// instead.
22
+ static PROVIDER: OnceLock<fn() -> &'static Runtime> = OnceLock::new();
23
+
24
+ /// Install an external runtime provider so vcache shares another crate's
25
+ /// runtime rather than building its own. Opt-in; the first caller wins and
26
+ /// later calls are a no-op. Must be called before the first [`get_runtime`]
27
+ /// (module-import time is ideal) to take effect.
28
+ ///
29
+ /// The provider is a `fn` pointer, **not** a `&'static Runtime`, on purpose: a
30
+ /// stored reference would dangle after `fork()` (it would point at the
31
+ /// parent's dead runtime in every prefork worker). A `fn` pointer survives
32
+ /// `fork()` and is re-invoked on each call, so it always resolves the *current*
33
+ /// process's fork-safe runtime — provided the supplied function is itself
34
+ /// fork-safe (as both vcache's own [`get_runtime`] and gt_rust's are).
35
+ pub fn set_runtime_provider(provider: fn() -> &'static Runtime) {
36
+ let _ = PROVIDER.set(provider);
37
+ }
38
+
16
39
  #[inline]
17
40
  pub fn get_runtime() -> &'static Runtime {
41
+ if let Some(provider) = PROVIDER.get() {
42
+ return provider();
43
+ }
18
44
  let pid = std::process::id();
19
45
  if RUNTIME_PID.load(Ordering::Relaxed) == pid {
20
46
  return RUNTIME.get().unwrap();
@@ -0,0 +1,45 @@
1
+ //! Integration test for the opt-in runtime provider hook.
2
+ //!
3
+ //! This runs as its own test binary (separate process) on purpose: the
4
+ //! provider is a process-wide `OnceLock`, so installing one here cannot be
5
+ //! undone and must not leak into the in-crate unit tests' view of
6
+ //! `get_runtime`. A dedicated integration binary gives it an isolated process.
7
+
8
+ use std::sync::OnceLock;
9
+ use tokio::runtime::Runtime;
10
+ use vcache_core::{get_runtime, set_runtime_provider};
11
+
12
+ static SENTINEL: OnceLock<Runtime> = OnceLock::new();
13
+
14
+ /// A fork-safe-by-construction provider: a leaked, process-wide runtime we can
15
+ /// identity-compare against what `get_runtime` hands back.
16
+ fn sentinel_runtime() -> &'static Runtime {
17
+ SENTINEL.get_or_init(|| {
18
+ tokio::runtime::Builder::new_multi_thread()
19
+ .enable_all()
20
+ .build()
21
+ .expect("failed to build sentinel runtime")
22
+ })
23
+ }
24
+
25
+ #[test]
26
+ fn get_runtime_delegates_to_installed_provider() {
27
+ // Before any get_runtime() call, install the provider.
28
+ set_runtime_provider(sentinel_runtime);
29
+
30
+ let rt = get_runtime();
31
+ // Delegation: get_runtime returned our provider's runtime, not vcache's own.
32
+ assert!(
33
+ std::ptr::eq(rt, sentinel_runtime()),
34
+ "get_runtime should delegate to the installed provider"
35
+ );
36
+ // And it is a working runtime.
37
+ assert_eq!(rt.block_on(async { 20 + 22 }), 42);
38
+
39
+ // First-caller-wins: a second install is a no-op; delegation is stable.
40
+ fn other() -> &'static Runtime {
41
+ unreachable!("the first provider must win")
42
+ }
43
+ set_runtime_provider(other);
44
+ assert!(std::ptr::eq(get_runtime(), sentinel_runtime()));
45
+ }
@@ -8,6 +8,11 @@ pub mod client;
8
8
  pub use async_bridge::RustAwaitable;
9
9
  pub use client::RustValkeyDriver;
10
10
 
11
+ // Re-export so a host cdylib (e.g. gt_rust) can make vcache share its runtime
12
+ // without taking a separate dependency on vcache-core. Opt-in and default-off;
13
+ // see vcache_core::runtime::set_runtime_provider.
14
+ pub use vcache_core::set_runtime_provider;
15
+
11
16
  use pyo3::prelude::*;
12
17
 
13
18
  /// Install the rustls crypto provider for TLS support (rediss:// URLs).
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "django-vcache"
7
- version = "2.2.0"
7
+ version = "2.3.0"
8
8
  description = "A specialized, lightweight Django cache backend for Valkey."
9
9
  readme = "README.md"
10
10
  authors = [{ name = "David Burke", email = "david@burkesoftware.com" }]
File without changes
File without changes