django-vcache 2.1.1__tar.gz → 2.2.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.
- {django_vcache-2.1.1 → django_vcache-2.2.0}/Cargo.lock +28 -10
- django_vcache-2.2.0/Cargo.toml +21 -0
- {django_vcache-2.1.1 → django_vcache-2.2.0}/PKG-INFO +1 -1
- django_vcache-2.2.0/crates/vcache-core/Cargo.toml +14 -0
- django_vcache-2.2.0/crates/vcache-core/examples/standalone.rs +29 -0
- django_vcache-2.2.0/crates/vcache-core/src/lib.rs +22 -0
- django_vcache-2.2.0/crates/vcache-core/src/result.rs +21 -0
- django_vcache-2.2.0/crates/vcache-core/src/runtime.rs +54 -0
- django_vcache-2.2.0/crates/vcache-pyo3/Cargo.toml +25 -0
- {django_vcache-2.1.1 → django_vcache-2.2.0/crates/vcache-pyo3}/src/async_bridge.rs +55 -134
- {django_vcache-2.1.1 → django_vcache-2.2.0/crates/vcache-pyo3}/src/client.rs +25 -6
- django_vcache-2.2.0/crates/vcache-pyo3/src/lib.rs +26 -0
- {django_vcache-2.1.1 → django_vcache-2.2.0}/django_vcache/backend.py +176 -5
- {django_vcache-2.1.1 → django_vcache-2.2.0}/pyproject.toml +2 -1
- django_vcache-2.1.1/.dockerignore +0 -4
- django_vcache-2.1.1/.gitignore +0 -23
- django_vcache-2.1.1/.gitlab-ci.yml +0 -149
- django_vcache-2.1.1/AGENTS.md +0 -122
- django_vcache-2.1.1/CHANGELOG.md +0 -117
- django_vcache-2.1.1/CLAUDE.md +0 -122
- django_vcache-2.1.1/Cargo.toml +0 -19
- django_vcache-2.1.1/Dockerfile +0 -36
- django_vcache-2.1.1/bench_asgi.sh +0 -87
- django_vcache-2.1.1/bench_compare.py +0 -453
- django_vcache-2.1.1/bench_slowpath.sh +0 -75
- django_vcache-2.1.1/benchmark.py +0 -209
- django_vcache-2.1.1/compose.bench.yml +0 -6
- django_vcache-2.1.1/compose.cluster.yml +0 -34
- django_vcache-2.1.1/compose.sentinel.yml +0 -13
- django_vcache-2.1.1/compose.tls.yml +0 -24
- django_vcache-2.1.1/compose.yml +0 -22
- django_vcache-2.1.1/docs/benchmarks.md +0 -56
- django_vcache-2.1.1/docs/configuration.md +0 -143
- django_vcache-2.1.1/docs/contributing.md +0 -54
- django_vcache-2.1.1/docs/getting-started.md +0 -52
- django_vcache-2.1.1/docs/guide.md +0 -38
- django_vcache-2.1.1/docs/index.md +0 -37
- django_vcache-2.1.1/docs/overrides/extra.css +0 -27
- django_vcache-2.1.1/docs/overrides/main.html +0 -33
- django_vcache-2.1.1/manage.py +0 -23
- django_vcache-2.1.1/mkdocs.yml +0 -40
- django_vcache-2.1.1/sample/__init__.py +0 -0
- django_vcache-2.1.1/sample/asgi.py +0 -16
- django_vcache-2.1.1/sample/management/__init__.py +0 -0
- django_vcache-2.1.1/sample/management/commands/__init__.py +0 -0
- django_vcache-2.1.1/sample/management/commands/benchmark.py +0 -196
- django_vcache-2.1.1/sample/management/commands/memtest.py +0 -211
- django_vcache-2.1.1/sample/settings.py +0 -45
- django_vcache-2.1.1/sample/settings_bench.py +0 -3
- django_vcache-2.1.1/sample/settings_django.py +0 -12
- django_vcache-2.1.1/sample/settings_dvalkey.py +0 -16
- django_vcache-2.1.1/sample/settings_v1.py +0 -14
- django_vcache-2.1.1/sample/test_sentinel_blocking.py +0 -26
- django_vcache-2.1.1/sample/tests.py +0 -513
- django_vcache-2.1.1/sample/tests_chaos.py +0 -380
- django_vcache-2.1.1/sample/tests_exceptions.py +0 -241
- django_vcache-2.1.1/sample/tests_fork.py +0 -212
- django_vcache-2.1.1/sample/urls.py +0 -22
- django_vcache-2.1.1/sample/views.py +0 -298
- django_vcache-2.1.1/sample/wsgi.py +0 -7
- django_vcache-2.1.1/src/lib.rs +0 -16
- django_vcache-2.1.1/tls/generate-certs.sh +0 -46
- django_vcache-2.1.1/uv.lock +0 -507
- {django_vcache-2.1.1 → django_vcache-2.2.0}/LICENSE +0 -0
- {django_vcache-2.1.1 → django_vcache-2.2.0}/README.md +0 -0
- {django_vcache-2.1.1 → django_vcache-2.2.0/crates/vcache-core}/src/connection.rs +0 -0
- {django_vcache-2.1.1 → django_vcache-2.2.0}/django_vcache/__init__.py +0 -0
|
@@ -172,16 +172,6 @@ dependencies = [
|
|
|
172
172
|
"syn",
|
|
173
173
|
]
|
|
174
174
|
|
|
175
|
-
[[package]]
|
|
176
|
-
name = "django-vcache"
|
|
177
|
-
version = "2.1.1"
|
|
178
|
-
dependencies = [
|
|
179
|
-
"pyo3",
|
|
180
|
-
"redis",
|
|
181
|
-
"rustls",
|
|
182
|
-
"tokio",
|
|
183
|
-
]
|
|
184
|
-
|
|
185
175
|
[[package]]
|
|
186
176
|
name = "dunce"
|
|
187
177
|
version = "1.0.5"
|
|
@@ -733,6 +723,14 @@ dependencies = [
|
|
|
733
723
|
"windows-sys 0.52.0",
|
|
734
724
|
]
|
|
735
725
|
|
|
726
|
+
[[package]]
|
|
727
|
+
name = "rlib-link-test"
|
|
728
|
+
version = "2.2.0"
|
|
729
|
+
dependencies = [
|
|
730
|
+
"pyo3",
|
|
731
|
+
"vcache-pyo3",
|
|
732
|
+
]
|
|
733
|
+
|
|
736
734
|
[[package]]
|
|
737
735
|
name = "rustls"
|
|
738
736
|
version = "0.23.37"
|
|
@@ -1005,6 +1003,26 @@ version = "1.0.4"
|
|
|
1005
1003
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1006
1004
|
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
|
1007
1005
|
|
|
1006
|
+
[[package]]
|
|
1007
|
+
name = "vcache-core"
|
|
1008
|
+
version = "2.2.0"
|
|
1009
|
+
dependencies = [
|
|
1010
|
+
"redis",
|
|
1011
|
+
"rustls",
|
|
1012
|
+
"tokio",
|
|
1013
|
+
]
|
|
1014
|
+
|
|
1015
|
+
[[package]]
|
|
1016
|
+
name = "vcache-pyo3"
|
|
1017
|
+
version = "2.2.0"
|
|
1018
|
+
dependencies = [
|
|
1019
|
+
"pyo3",
|
|
1020
|
+
"redis",
|
|
1021
|
+
"rustls",
|
|
1022
|
+
"tokio",
|
|
1023
|
+
"vcache-core",
|
|
1024
|
+
]
|
|
1025
|
+
|
|
1008
1026
|
[[package]]
|
|
1009
1027
|
name = "wasi"
|
|
1010
1028
|
version = "0.11.1+wasi-snapshot-preview1"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[workspace]
|
|
2
|
+
resolver = "2"
|
|
3
|
+
members = ["crates/vcache-core", "crates/vcache-pyo3"]
|
|
4
|
+
|
|
5
|
+
[workspace.package]
|
|
6
|
+
version = "2.2.0"
|
|
7
|
+
edition = "2024"
|
|
8
|
+
|
|
9
|
+
[workspace.dependencies]
|
|
10
|
+
tokio = { version = "1", features = ["rt-multi-thread"] }
|
|
11
|
+
redis = { version = "1", features = ["tokio-comp", "connection-manager", "cluster", "cluster-async", "tokio-rustls-comp", "tls-rustls-insecure", "cache-aio"] }
|
|
12
|
+
rustls = { version = "0.23", features = ["ring"] }
|
|
13
|
+
# All current members are cdylibs that ship as Python extension modules;
|
|
14
|
+
# the `extension-module` feature skips libpython linking. If a future
|
|
15
|
+
# member is a bin or test harness that embeds Python, drop this feature
|
|
16
|
+
# and re-add it in each cdylib crate's own [dependencies] block.
|
|
17
|
+
pyo3 = { version = "0.28", features = ["extension-module"] }
|
|
18
|
+
|
|
19
|
+
[profile.release]
|
|
20
|
+
lto = true
|
|
21
|
+
strip = true
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "vcache-core"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
description = "Pure-Rust valkey/redis async driver core. Backs the django-vcache Python package and is reusable from other Rust crates that want to share its tokio runtime and connection types."
|
|
6
|
+
license = "MIT"
|
|
7
|
+
|
|
8
|
+
[lib]
|
|
9
|
+
name = "vcache_core"
|
|
10
|
+
|
|
11
|
+
[dependencies]
|
|
12
|
+
tokio.workspace = true
|
|
13
|
+
redis.workspace = true
|
|
14
|
+
rustls.workspace = true
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//! Pure-Rust smoke test: vcache-core is consumable without pyo3.
|
|
2
|
+
//!
|
|
3
|
+
//! Connects to VALKEY_URL (default redis://valkey:6379/1), does set/get/del.
|
|
4
|
+
//! CI runs this so a regression in vcache-core's pure-Rust surface (rename,
|
|
5
|
+
//! visibility change, feature-flag mistake) fails here, before downstream
|
|
6
|
+
//! crates hit it on the next bump.
|
|
7
|
+
|
|
8
|
+
use vcache_core::{connect_standard, get_runtime};
|
|
9
|
+
|
|
10
|
+
fn main() {
|
|
11
|
+
let url = std::env::var("VALKEY_URL").unwrap_or_else(|_| "redis://valkey:6379/1".into());
|
|
12
|
+
get_runtime().block_on(async move {
|
|
13
|
+
let mut conn = connect_standard(&url, None, None)
|
|
14
|
+
.await
|
|
15
|
+
.expect("connect failed");
|
|
16
|
+
conn.set_bytes("vcache_core_smoke", b"ok".to_vec(), Some(10))
|
|
17
|
+
.await
|
|
18
|
+
.expect("set failed");
|
|
19
|
+
let got = conn
|
|
20
|
+
.get_bytes("vcache_core_smoke")
|
|
21
|
+
.await
|
|
22
|
+
.expect("get failed")
|
|
23
|
+
.expect("missing value");
|
|
24
|
+
assert_eq!(got, b"ok", "round-trip mismatch");
|
|
25
|
+
let deleted = conn.del("vcache_core_smoke").await.expect("del failed");
|
|
26
|
+
assert_eq!(deleted, 1);
|
|
27
|
+
println!("vcache-core standalone smoke: OK");
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//! Pure-Rust async valkey/redis driver core for django-vcache.
|
|
2
|
+
//!
|
|
3
|
+
//! This crate contains the connection logic, tokio runtime singleton, and
|
|
4
|
+
//! result types. It has no Python or pyo3 dependency, so other Rust crates
|
|
5
|
+
//! can link it as a library without dragging in Python.
|
|
6
|
+
//!
|
|
7
|
+
//! The thin pyo3 wrapper that exposes a Python-facing module lives in the
|
|
8
|
+
//! sibling ``vcache-pyo3`` crate within this workspace; that wrapper is what
|
|
9
|
+
//! gets built into ``django_vcache._driver``. A separate downstream crate
|
|
10
|
+
//! can depend on this crate directly to share the same tokio runtime and
|
|
11
|
+
//! connection pool implementation across Python and Rust call sites in a
|
|
12
|
+
//! single ``.so``.
|
|
13
|
+
|
|
14
|
+
pub mod connection;
|
|
15
|
+
pub mod result;
|
|
16
|
+
pub mod runtime;
|
|
17
|
+
|
|
18
|
+
pub use connection::{
|
|
19
|
+
connect_cluster, connect_sentinel, connect_standard, ClientCacheOpts, TlsOpts, ValkeyConn,
|
|
20
|
+
};
|
|
21
|
+
pub use result::RawResult;
|
|
22
|
+
pub use runtime::get_runtime;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//! Rust-native result enum returned by the async driver tasks.
|
|
2
|
+
//!
|
|
3
|
+
//! Conversion to Python objects lives in the pyo3 wrapper (vcache-pyo3),
|
|
4
|
+
//! so this enum has no GIL-acquired construction overhead and can be
|
|
5
|
+
//! produced and consumed entirely from non-Python rust code.
|
|
6
|
+
|
|
7
|
+
pub enum RawResult {
|
|
8
|
+
OptBytes(Option<Vec<u8>>),
|
|
9
|
+
Bool(bool),
|
|
10
|
+
Int(i64),
|
|
11
|
+
OptBytesList(Vec<Option<Vec<u8>>>),
|
|
12
|
+
BytesList(Vec<Vec<u8>>),
|
|
13
|
+
StringList(Vec<String>),
|
|
14
|
+
OptKeyAndBytesList(Option<(String, Vec<Vec<u8>>)>),
|
|
15
|
+
/// Connection/IO error → mapped to PyConnectionError by the pyo3 layer
|
|
16
|
+
/// (where it is swallowed by IGNORE_EXCEPTIONS).
|
|
17
|
+
Error(String),
|
|
18
|
+
/// Data/server error → PyRuntimeError (NOT swallowed; indicates real
|
|
19
|
+
/// server-side problems).
|
|
20
|
+
ServerError(String),
|
|
21
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//! Fork-safe shared tokio runtime.
|
|
2
|
+
//!
|
|
3
|
+
//! Fast path (99.99% of calls): atomic PID check + OnceLock::get() → &'static.
|
|
4
|
+
//! Slow path (first call or after fork): OnceLock init or Mutex-protected
|
|
5
|
+
//! runtime creation. After fork(), the parent's dead runtime is leaked via
|
|
6
|
+
//! Box::leak to avoid joining dead threads.
|
|
7
|
+
|
|
8
|
+
use std::sync::atomic::{AtomicU32, Ordering};
|
|
9
|
+
use std::sync::{Mutex, OnceLock};
|
|
10
|
+
use tokio::runtime::Runtime;
|
|
11
|
+
|
|
12
|
+
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
|
|
13
|
+
static RUNTIME_PID: AtomicU32 = AtomicU32::new(0);
|
|
14
|
+
static FORK_RUNTIME: Mutex<Option<(u32, &'static Runtime)>> = Mutex::new(None);
|
|
15
|
+
|
|
16
|
+
#[inline]
|
|
17
|
+
pub fn get_runtime() -> &'static Runtime {
|
|
18
|
+
let pid = std::process::id();
|
|
19
|
+
if RUNTIME_PID.load(Ordering::Relaxed) == pid {
|
|
20
|
+
return RUNTIME.get().unwrap();
|
|
21
|
+
}
|
|
22
|
+
init_or_fork_runtime(pid)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#[cold]
|
|
26
|
+
fn init_or_fork_runtime(pid: u32) -> &'static Runtime {
|
|
27
|
+
let stored = RUNTIME_PID.load(Ordering::Relaxed);
|
|
28
|
+
|
|
29
|
+
if stored == 0 {
|
|
30
|
+
let rt = RUNTIME.get_or_init(|| {
|
|
31
|
+
tokio::runtime::Builder::new_multi_thread()
|
|
32
|
+
.enable_all()
|
|
33
|
+
.build()
|
|
34
|
+
.expect("Failed to create tokio runtime")
|
|
35
|
+
});
|
|
36
|
+
RUNTIME_PID.store(pid, Ordering::Relaxed);
|
|
37
|
+
return rt;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let mut guard = FORK_RUNTIME.lock().unwrap();
|
|
41
|
+
if let Some((stored_pid, rt)) = *guard {
|
|
42
|
+
if stored_pid == pid {
|
|
43
|
+
return rt;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
let rt: &'static Runtime = Box::leak(Box::new(
|
|
47
|
+
tokio::runtime::Builder::new_multi_thread()
|
|
48
|
+
.enable_all()
|
|
49
|
+
.build()
|
|
50
|
+
.expect("Failed to create tokio runtime"),
|
|
51
|
+
));
|
|
52
|
+
*guard = Some((pid, rt));
|
|
53
|
+
rt
|
|
54
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "vcache-pyo3"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
description = "Pyo3 bindings exposing vcache-core as the django_vcache._driver Python extension."
|
|
6
|
+
license = "MIT"
|
|
7
|
+
|
|
8
|
+
[lib]
|
|
9
|
+
# Rust-level lib name. Maturin renames the cdylib output on install to
|
|
10
|
+
# match `[tool.maturin] module-name = "django_vcache._driver"` in
|
|
11
|
+
# pyproject.toml, so Python still imports django_vcache._driver while
|
|
12
|
+
# downstream Rust crates can `use vcache_pyo3::*`.
|
|
13
|
+
name = "vcache_pyo3"
|
|
14
|
+
# rlib so other Rust crates (e.g., glitchtip-rust) can depend on this
|
|
15
|
+
# crate and re-export the same #[pyclass]es into their own pymodule —
|
|
16
|
+
# avoiding duplicate driver code while preserving the standalone
|
|
17
|
+
# django_vcache._driver cdylib.
|
|
18
|
+
crate-type = ["cdylib", "rlib"]
|
|
19
|
+
|
|
20
|
+
[dependencies]
|
|
21
|
+
pyo3.workspace = true
|
|
22
|
+
tokio = { workspace = true, features = ["sync"] }
|
|
23
|
+
redis.workspace = true
|
|
24
|
+
rustls.workspace = true
|
|
25
|
+
vcache-core = { path = "../vcache-core" }
|
|
@@ -1,128 +1,59 @@
|
|
|
1
|
+
//! Python-bound async bridge: ``RustAwaitable`` (asyncio-compatible
|
|
2
|
+
//! awaitable) and the ``RawResult → Python`` conversion. The pure-rust
|
|
3
|
+
//! tokio runtime singleton and ``RawResult`` enum live in vcache-core.
|
|
4
|
+
|
|
1
5
|
use pyo3::prelude::*;
|
|
2
6
|
use pyo3::types::{PyBytes, PyList, PyString, PyTuple};
|
|
3
|
-
use std::sync::
|
|
4
|
-
use std::sync::{Arc, Mutex, OnceLock};
|
|
5
|
-
use tokio::runtime::Runtime;
|
|
7
|
+
use std::sync::{Arc, Mutex};
|
|
6
8
|
use tokio::sync::oneshot;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
///
|
|
11
|
-
///
|
|
12
|
-
///
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
#[cold]
|
|
29
|
-
fn init_or_fork_runtime(pid: u32) -> &'static Runtime {
|
|
30
|
-
let stored = RUNTIME_PID.load(Ordering::Relaxed);
|
|
31
|
-
|
|
32
|
-
if stored == 0 {
|
|
33
|
-
// First call: initialize via OnceLock (handles concurrent init safely)
|
|
34
|
-
let rt = RUNTIME.get_or_init(|| {
|
|
35
|
-
tokio::runtime::Builder::new_multi_thread()
|
|
36
|
-
.enable_all()
|
|
37
|
-
.build()
|
|
38
|
-
.expect("Failed to create tokio runtime")
|
|
39
|
-
});
|
|
40
|
-
RUNTIME_PID.store(pid, Ordering::Relaxed);
|
|
41
|
-
return rt;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Fork detected: parent's tokio threads are dead in this child.
|
|
45
|
-
// Create a fresh runtime and leak it (can't drop a dead runtime safely).
|
|
46
|
-
let mut guard = FORK_RUNTIME.lock().unwrap();
|
|
47
|
-
if let Some((stored_pid, rt)) = *guard {
|
|
48
|
-
if stored_pid == pid {
|
|
49
|
-
return rt;
|
|
9
|
+
use vcache_core::result::RawResult;
|
|
10
|
+
use vcache_core::runtime::get_runtime;
|
|
11
|
+
|
|
12
|
+
/// Convert a [`RawResult`] into a Python object. Free function rather than
|
|
13
|
+
/// an inherent impl on [`RawResult`] because that type lives in another
|
|
14
|
+
/// crate (orphan rules).
|
|
15
|
+
pub fn raw_result_into_py(raw: RawResult, py: Python<'_>) -> Result<Py<PyAny>, PyErr> {
|
|
16
|
+
match raw {
|
|
17
|
+
RawResult::OptBytes(Some(b)) => Ok(PyBytes::new(py, &b).into_any().unbind()),
|
|
18
|
+
RawResult::OptBytes(None) => Ok(py.None()),
|
|
19
|
+
RawResult::Bool(b) => Ok(b.into_pyobject(py).unwrap().to_owned().into_any().unbind()),
|
|
20
|
+
RawResult::Int(n) => Ok(n.into_pyobject(py).unwrap().into_any().unbind()),
|
|
21
|
+
RawResult::OptBytesList(items) => {
|
|
22
|
+
let py_items: Vec<Py<PyAny>> = items
|
|
23
|
+
.into_iter()
|
|
24
|
+
.map(|r| match r {
|
|
25
|
+
Some(bytes) => PyBytes::new(py, &bytes).into_any().unbind(),
|
|
26
|
+
None => py.None(),
|
|
27
|
+
})
|
|
28
|
+
.collect();
|
|
29
|
+
Ok(PyList::new(py, py_items)?.into_any().unbind())
|
|
50
30
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
));
|
|
58
|
-
*guard = Some((pid, rt));
|
|
59
|
-
rt
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// =========================================================================
|
|
63
|
-
// Result types — Rust-native, no GIL needed to construct
|
|
64
|
-
// =========================================================================
|
|
65
|
-
|
|
66
|
-
pub enum RawResult {
|
|
67
|
-
OptBytes(Option<Vec<u8>>),
|
|
68
|
-
Bool(bool),
|
|
69
|
-
Int(i64),
|
|
70
|
-
OptBytesList(Vec<Option<Vec<u8>>>),
|
|
71
|
-
BytesList(Vec<Vec<u8>>),
|
|
72
|
-
StringList(Vec<String>),
|
|
73
|
-
OptKeyAndBytesList(Option<(String, Vec<Vec<u8>>)>),
|
|
74
|
-
/// Connection/IO error → PyConnectionError (swallowed by IGNORE_EXCEPTIONS)
|
|
75
|
-
Error(String),
|
|
76
|
-
/// Data/server error → PyRuntimeError (NOT swallowed, indicates real problems)
|
|
77
|
-
ServerError(String),
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
impl RawResult {
|
|
81
|
-
pub fn into_py(self, py: Python<'_>) -> Result<Py<PyAny>, PyErr> {
|
|
82
|
-
match self {
|
|
83
|
-
RawResult::OptBytes(Some(b)) => Ok(PyBytes::new(py, &b).into_any().unbind()),
|
|
84
|
-
RawResult::OptBytes(None) => Ok(py.None()),
|
|
85
|
-
RawResult::Bool(b) => {
|
|
86
|
-
Ok(b.into_pyobject(py).unwrap().to_owned().into_any().unbind())
|
|
87
|
-
}
|
|
88
|
-
RawResult::Int(n) => Ok(n.into_pyobject(py).unwrap().into_any().unbind()),
|
|
89
|
-
RawResult::OptBytesList(items) => {
|
|
90
|
-
let py_items: Vec<Py<PyAny>> = items
|
|
91
|
-
.into_iter()
|
|
92
|
-
.map(|r| match r {
|
|
93
|
-
Some(bytes) => PyBytes::new(py, &bytes).into_any().unbind(),
|
|
94
|
-
None => py.None(),
|
|
95
|
-
})
|
|
96
|
-
.collect();
|
|
97
|
-
Ok(PyList::new(py, py_items)?.into_any().unbind())
|
|
98
|
-
}
|
|
99
|
-
RawResult::BytesList(items) => {
|
|
100
|
-
let py_items: Vec<Py<PyAny>> = items
|
|
101
|
-
.iter()
|
|
102
|
-
.map(|b| PyBytes::new(py, b).into_any().unbind())
|
|
103
|
-
.collect();
|
|
104
|
-
Ok(PyList::new(py, py_items)?.into_any().unbind())
|
|
105
|
-
}
|
|
106
|
-
RawResult::StringList(items) => {
|
|
107
|
-
let py_items: Vec<Py<PyAny>> = items
|
|
108
|
-
.iter()
|
|
109
|
-
.map(|s| PyString::new(py, s).into_any().unbind())
|
|
110
|
-
.collect();
|
|
111
|
-
Ok(PyList::new(py, py_items)?.into_any().unbind())
|
|
112
|
-
}
|
|
113
|
-
RawResult::OptKeyAndBytesList(Some((key, values))) => {
|
|
114
|
-
let py_values: Vec<Py<PyAny>> = values
|
|
115
|
-
.iter()
|
|
116
|
-
.map(|b| PyBytes::new(py, b).into_any().unbind())
|
|
117
|
-
.collect();
|
|
118
|
-
let py_key = PyString::new(py, &key).into_any().unbind();
|
|
119
|
-
let py_list = PyList::new(py, py_values)?.into_any().unbind();
|
|
120
|
-
Ok(PyTuple::new(py, [py_key, py_list])?.into_any().unbind())
|
|
121
|
-
}
|
|
122
|
-
RawResult::OptKeyAndBytesList(None) => Ok(py.None()),
|
|
123
|
-
RawResult::Error(e) => Err(pyo3::exceptions::PyConnectionError::new_err(e)),
|
|
124
|
-
RawResult::ServerError(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(e)),
|
|
31
|
+
RawResult::BytesList(items) => {
|
|
32
|
+
let py_items: Vec<Py<PyAny>> = items
|
|
33
|
+
.iter()
|
|
34
|
+
.map(|b| PyBytes::new(py, b).into_any().unbind())
|
|
35
|
+
.collect();
|
|
36
|
+
Ok(PyList::new(py, py_items)?.into_any().unbind())
|
|
125
37
|
}
|
|
38
|
+
RawResult::StringList(items) => {
|
|
39
|
+
let py_items: Vec<Py<PyAny>> = items
|
|
40
|
+
.iter()
|
|
41
|
+
.map(|s| PyString::new(py, s).into_any().unbind())
|
|
42
|
+
.collect();
|
|
43
|
+
Ok(PyList::new(py, py_items)?.into_any().unbind())
|
|
44
|
+
}
|
|
45
|
+
RawResult::OptKeyAndBytesList(Some((key, values))) => {
|
|
46
|
+
let py_values: Vec<Py<PyAny>> = values
|
|
47
|
+
.iter()
|
|
48
|
+
.map(|b| PyBytes::new(py, b).into_any().unbind())
|
|
49
|
+
.collect();
|
|
50
|
+
let py_key = PyString::new(py, &key).into_any().unbind();
|
|
51
|
+
let py_list = PyList::new(py, py_values)?.into_any().unbind();
|
|
52
|
+
Ok(PyTuple::new(py, [py_key, py_list])?.into_any().unbind())
|
|
53
|
+
}
|
|
54
|
+
RawResult::OptKeyAndBytesList(None) => Ok(py.None()),
|
|
55
|
+
RawResult::Error(e) => Err(pyo3::exceptions::PyConnectionError::new_err(e)),
|
|
56
|
+
RawResult::ServerError(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(e)),
|
|
126
57
|
}
|
|
127
58
|
}
|
|
128
59
|
|
|
@@ -234,12 +165,10 @@ impl RustAwaitable {
|
|
|
234
165
|
fn __next__(slf: Py<Self>, py: Python<'_>) -> PyResult<Py<PyAny>> {
|
|
235
166
|
let mut this = slf.borrow_mut(py);
|
|
236
167
|
|
|
237
|
-
// Cancelled — raise CancelledError.
|
|
238
168
|
if this.cancelled {
|
|
239
169
|
return Err(cancelled_error(py));
|
|
240
170
|
}
|
|
241
171
|
|
|
242
|
-
// Already resolved — re-deliver stored result.
|
|
243
172
|
if this.resolved {
|
|
244
173
|
if let Some(ref exc) = this.error {
|
|
245
174
|
return Err(PyErr::from_value(exc.bind(py).clone()));
|
|
@@ -255,13 +184,12 @@ impl RustAwaitable {
|
|
|
255
184
|
));
|
|
256
185
|
}
|
|
257
186
|
|
|
258
|
-
// Check result_slot (set by watcher task via Rust mutex, no GIL needed).
|
|
259
187
|
if let Some(ref cb) = this.cb {
|
|
260
188
|
let maybe = cb.result_slot.lock().unwrap().take();
|
|
261
189
|
if let Some(raw_result) = maybe {
|
|
262
190
|
this.cb = None;
|
|
263
191
|
return match raw_result {
|
|
264
|
-
Ok(raw) => match raw
|
|
192
|
+
Ok(raw) => match raw_result_into_py(raw, py) {
|
|
265
193
|
Ok(val) => deliver_value(&mut this, py, val),
|
|
266
194
|
Err(e) => deliver_error(&mut this, py, e),
|
|
267
195
|
},
|
|
@@ -274,12 +202,11 @@ impl RustAwaitable {
|
|
|
274
202
|
}
|
|
275
203
|
}
|
|
276
204
|
|
|
277
|
-
// Try to read result directly from the oneshot channel.
|
|
278
205
|
if let Some(rx) = this.rx.as_mut() {
|
|
279
206
|
match rx.try_recv() {
|
|
280
207
|
Ok(raw) => {
|
|
281
208
|
this.rx = None;
|
|
282
|
-
return match raw
|
|
209
|
+
return match raw_result_into_py(raw, py) {
|
|
283
210
|
Ok(val) => deliver_value(&mut this, py, val),
|
|
284
211
|
Err(e) => deliver_error(&mut this, py, e),
|
|
285
212
|
};
|
|
@@ -292,9 +219,7 @@ impl RustAwaitable {
|
|
|
292
219
|
pyo3::exceptions::PyRuntimeError::new_err("operation was dropped"),
|
|
293
220
|
);
|
|
294
221
|
}
|
|
295
|
-
Err(oneshot::error::TryRecvError::Empty) => {
|
|
296
|
-
// Not ready yet
|
|
297
|
-
}
|
|
222
|
+
Err(oneshot::error::TryRecvError::Empty) => {}
|
|
298
223
|
}
|
|
299
224
|
} else if this.resolved {
|
|
300
225
|
return Err(pyo3::exceptions::PyRuntimeError::new_err(
|
|
@@ -323,7 +248,6 @@ impl RustAwaitable {
|
|
|
323
248
|
let event_loop = asyncio.call_method0("get_running_loop")?;
|
|
324
249
|
this._asyncio_future_blocking = true;
|
|
325
250
|
|
|
326
|
-
// Spawn a lightweight watcher that awaits the result and wakes us.
|
|
327
251
|
let event_loop_ref = event_loop.clone().into_any().unbind();
|
|
328
252
|
let awaitable_ref = slf.clone_ref(py).into_any();
|
|
329
253
|
let result_slot = Arc::new(Mutex::new(None));
|
|
@@ -358,7 +282,7 @@ impl RustAwaitable {
|
|
|
358
282
|
///
|
|
359
283
|
/// Each callback is invoked via `context.run(callback, self)` when a
|
|
360
284
|
/// context was provided by `add_done_callback`, matching asyncio.Future's
|
|
361
|
-
/// contract.
|
|
285
|
+
/// contract. This ensures the callback (typically `Task.__step`) runs
|
|
362
286
|
/// in the correct `contextvars.Context`.
|
|
363
287
|
fn _wake(slf: Py<Self>, py: Python<'_>) {
|
|
364
288
|
let callbacks = {
|
|
@@ -385,9 +309,6 @@ impl RustAwaitable {
|
|
|
385
309
|
context,
|
|
386
310
|
});
|
|
387
311
|
}
|
|
388
|
-
// If no callback state yet (still in busy-yield), the callback
|
|
389
|
-
// will be added when we enter callback mode. In practice, asyncio
|
|
390
|
-
// only calls add_done_callback after we yield with future_blocking=True.
|
|
391
312
|
}
|
|
392
313
|
|
|
393
314
|
fn result(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
use pyo3::prelude::*;
|
|
2
2
|
use pyo3::types::PyBytes;
|
|
3
3
|
|
|
4
|
-
use crate::async_bridge::{
|
|
5
|
-
use
|
|
4
|
+
use crate::async_bridge::{raw_result_into_py, RustAwaitable};
|
|
5
|
+
use vcache_core::connection::{
|
|
6
6
|
connect_cluster, connect_sentinel, connect_standard, ClientCacheOpts, TlsOpts, ValkeyConn,
|
|
7
7
|
};
|
|
8
|
+
use vcache_core::result::RawResult;
|
|
9
|
+
use vcache_core::runtime::get_runtime;
|
|
8
10
|
|
|
9
11
|
/// Thin async Valkey driver. Python handles serialization and compression.
|
|
10
12
|
#[pyclass]
|
|
@@ -12,6 +14,25 @@ pub struct RustValkeyDriver {
|
|
|
12
14
|
connection: ValkeyConn,
|
|
13
15
|
}
|
|
14
16
|
|
|
17
|
+
impl RustValkeyDriver {
|
|
18
|
+
/// Cloneable handle to the underlying ``ValkeyConn``. Lets a sibling
|
|
19
|
+
/// crate that links ``vcache-pyo3`` as an rlib issue commands against
|
|
20
|
+
/// the same connection the Python wrapper exposes — without re-opening
|
|
21
|
+
/// the pool.
|
|
22
|
+
///
|
|
23
|
+
/// Runtime isolation caveat: ``RUNTIME`` is a per-``.so`` ``OnceLock``,
|
|
24
|
+
/// so a ``RustValkeyDriver`` constructed inside ``django_vcache._driver``
|
|
25
|
+
/// and one constructed inside a downstream rlib consumer's own ``.so``
|
|
26
|
+
/// do **not** share a tokio runtime or pool — they just share a class
|
|
27
|
+
/// name. Pick one ``.so`` per process; the
|
|
28
|
+
/// ``DRIVER_CLASS`` cache option exists so downstream consumers can
|
|
29
|
+
/// route through their own driver class without loading the bundled
|
|
30
|
+
/// driver at all.
|
|
31
|
+
pub fn inner(&self) -> ValkeyConn {
|
|
32
|
+
self.connection.clone()
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
15
36
|
// Async: create RustAwaitable, spawn tokio task, send result via oneshot.
|
|
16
37
|
// The tokio task never touches the GIL — zero Python interaction.
|
|
17
38
|
// If the result arrives before __next__, it's consumed directly (fast path).
|
|
@@ -797,8 +818,7 @@ impl RustValkeyDriver {
|
|
|
797
818
|
let result: redis::Value =
|
|
798
819
|
sync_op!(py, self, conn, conn.eval(script, &keys, &args).await)
|
|
799
820
|
.map_err(|e| to_py_err(e))?;
|
|
800
|
-
r_value(Ok(result))
|
|
801
|
-
.into_py(py)
|
|
821
|
+
raw_result_into_py(r_value(Ok(result)), py)
|
|
802
822
|
}
|
|
803
823
|
|
|
804
824
|
// =================================================================
|
|
@@ -830,8 +850,7 @@ impl RustValkeyDriver {
|
|
|
830
850
|
let result: redis::Value =
|
|
831
851
|
sync_op!(py, self, conn, conn.evalsha(sha, &keys, &args).await)
|
|
832
852
|
.map_err(|e| to_py_err(e))?;
|
|
833
|
-
r_value(Ok(result))
|
|
834
|
-
.into_py(py)
|
|
853
|
+
raw_result_into_py(r_value(Ok(result)), py)
|
|
835
854
|
}
|
|
836
855
|
|
|
837
856
|
#[pyo3(signature = (script))]
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// `pub` so downstream rlib consumers can re-export the #[pyclass]es into
|
|
2
|
+
// their own pymodule (e.g. a host extension that ships them under its own
|
|
3
|
+
// module path, sharing one .so / one tokio runtime with its other drivers
|
|
4
|
+
// such as a postgres pool).
|
|
5
|
+
pub mod async_bridge;
|
|
6
|
+
pub mod client;
|
|
7
|
+
|
|
8
|
+
pub use async_bridge::RustAwaitable;
|
|
9
|
+
pub use client::RustValkeyDriver;
|
|
10
|
+
|
|
11
|
+
use pyo3::prelude::*;
|
|
12
|
+
|
|
13
|
+
/// Install the rustls crypto provider for TLS support (rediss:// URLs).
|
|
14
|
+
/// Idempotent; downstream cdylibs that re-export RustValkeyDriver should
|
|
15
|
+
/// call this from their own pymodule init too.
|
|
16
|
+
pub fn install_crypto_provider() {
|
|
17
|
+
let _ = rustls::crypto::ring::default_provider().install_default();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#[pymodule]
|
|
21
|
+
fn _driver(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
22
|
+
install_crypto_provider();
|
|
23
|
+
m.add_class::<RustValkeyDriver>()?;
|
|
24
|
+
m.add_class::<RustAwaitable>()?;
|
|
25
|
+
Ok(())
|
|
26
|
+
}
|