Struct LuaHost
pub struct LuaHost<T> { /* private fields */ }Expand description
kevy-side per-shard Lua host. Wraps a kevy_lua::Bridge plus
the scoped-pointer plumbing.
T is whatever shard state the dispatch closure needs (Store,
KeyspaceStore, …). It must outlive every LuaHost::eval call
(trivially true: kevy holds the &mut T while delegating).
use std::collections::HashMap;
use kevy_lua_host::LuaHost;
type Keyspace = HashMap<Vec<u8>, Vec<u8>>;
let mut host = LuaHost::<Keyspace>::new(|ks, argv, _ro| {
ks.insert(argv[1].to_vec(), argv[2].to_vec());
b"+OK\r\n".to_vec()
});
let mut shard = Keyspace::new();
host.eval(&mut shard, b"return redis.call('SET', KEYS[1], ARGV[1])", &[b"k"], &[b"v"]);
assert_eq!(shard.get(&b"k"[..]), Some(&b"v".to_vec()));Implementations§
§impl<T: 'static> LuaHost<T>
impl<T: 'static> LuaHost<T>
pub fn new<F>(dispatch_fn: F) -> Self
pub fn new<F>(dispatch_fn: F) -> Self
Build a host with dispatch_fn as the redis.call backend.
dispatch_fn receives a &mut T (the current shard context),
the script’s argv (command + args), and the read_only flag.
It must return RESP reply bytes — production callers route to
kevy’s dispatch, tests just return canned replies.
The closure receives &mut T via with_current, so it
must be Fn(&mut T, …) -> … rather than FnMut. (kevy’s
dispatch path is &mut self, so Fn(&mut T, …) is exactly
what we need.)
// the context here is a log of every command the script sent
let mut host = kevy_lua_host::LuaHost::<Vec<Vec<u8>>>::new(|log, argv, _ro| {
log.push(argv[0].to_vec());
b"+OK\r\n".to_vec()
});
let mut log = Vec::new();
host.eval(&mut log, b"redis.call('PING') return redis.call('ECHO')", &[], &[]);
assert_eq!(log, [b"PING".to_vec(), b"ECHO".to_vec()]);pub fn eval(
&mut self,
ctx: &mut T,
script: &[u8],
keys: &[&[u8]],
args: &[&[u8]],
) -> Reply
pub fn eval( &mut self, ctx: &mut T, script: &[u8], keys: &[&[u8]], args: &[&[u8]], ) -> Reply
Run a script. Scoped-installs ctx so the dispatch closure
can find it via with_current, then delegates to
Bridge::eval.
let mut host = kevy_lua_host::LuaHost::<i64>::new(|n, _argv, _ro| {
*n += 1;
format!(":{n}\r\n").into_bytes()
});
let mut counter = 10;
let reply = host.eval(&mut counter, b"return redis.call('INCR', KEYS[1])", &[b"c"], &[]);
assert_eq!(reply, b":11\r\n");
assert_eq!(counter, 11);pub fn eval_ro(
&mut self,
ctx: &mut T,
script: &[u8],
keys: &[&[u8]],
args: &[&[u8]],
) -> Reply
pub fn eval_ro( &mut self, ctx: &mut T, script: &[u8], keys: &[&[u8]], args: &[&[u8]], ) -> Reply
Read-only counterpart of Self::eval: the dispatch closure
receives read_only = true.
let mut host = kevy_lua_host::LuaHost::<Vec<bool>>::new(|seen, _argv, ro| {
seen.push(ro);
b"+OK\r\n".to_vec()
});
let mut seen = Vec::new();
host.eval(&mut seen, b"return redis.call('GET', 'k')", &[], &[]);
host.eval_ro(&mut seen, b"return redis.call('GET', 'k')", &[], &[]);
assert_eq!(seen, [false, true]);pub fn evalsha(
&mut self,
ctx: &mut T,
sha1: ScriptSha1,
keys: &[&[u8]],
args: &[&[u8]],
) -> Reply
pub fn evalsha( &mut self, ctx: &mut T, sha1: ScriptSha1, keys: &[&[u8]], args: &[&[u8]], ) -> Reply
Run a previously-loaded script by SHA1.
let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
let sha = host.script_load(b"return tonumber(ARGV[1]) * 2");
assert_eq!(host.evalsha(&mut (), sha, &[], &[b"21"]), b":42\r\n");
assert!(host.evalsha(&mut (), [0; 20], &[], &[]).starts_with(b"-NOSCRIPT"));pub fn evalsha_ro(
&mut self,
ctx: &mut T,
sha1: ScriptSha1,
keys: &[&[u8]],
args: &[&[u8]],
) -> Reply
pub fn evalsha_ro( &mut self, ctx: &mut T, sha1: ScriptSha1, keys: &[&[u8]], args: &[&[u8]], ) -> Reply
Read-only EVALSHA: the dispatch closure receives
read_only = true.
let mut host = kevy_lua_host::LuaHost::<Vec<bool>>::new(|seen, _argv, ro| {
seen.push(ro);
b"+OK\r\n".to_vec()
});
let sha = host.script_load(b"return redis.call('GET', 'k')");
let mut seen = Vec::new();
host.evalsha_ro(&mut seen, sha, &[], &[]);
assert_eq!(seen, [true]);pub fn script_load(&mut self, script: &[u8]) -> ScriptSha1
pub fn script_load(&mut self, script: &[u8]) -> ScriptSha1
SCRIPT LOAD — cache without running. No context needed.
let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
let sha = host.script_load(b"return 1");
assert_eq!(sha, kevy_lua::sha1::sha1(b"return 1"));
assert_eq!(host.script_exists(&[sha]), [true]);pub fn script_exists(&self, sha1s: &[ScriptSha1]) -> Vec<bool>
pub fn script_exists(&self, sha1s: &[ScriptSha1]) -> Vec<bool>
SCRIPT EXISTS.
let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
host.eval(&mut (), b"return 1", &[], &[]);
let ran = kevy_lua::sha1::sha1(b"return 1");
assert_eq!(host.script_exists(&[ran, [0; 20]]), [true, false]);pub fn script_flush(&mut self, mode: FlushMode)
pub fn script_flush(&mut self, mode: FlushMode)
SCRIPT FLUSH.
let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
let sha = host.script_load(b"return 1");
host.script_flush(kevy_lua::FlushMode::Sync);
assert_eq!(host.script_exists(&[sha]), [false]);pub fn set_instr_budget(&mut self, n: i64)
pub fn set_instr_budget(&mut self, n: i64)
Forward kevy_lua::Bridge::set_instr_budget — set the
per-Vm instruction cap. The operator wires [lua] time_limit_ms here at server startup.
let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
host.set_instr_budget(1_000);
assert!(host.eval(&mut (), b"while true do end", &[], &[]).starts_with(b"-"));pub fn set_allowed_dialects(&mut self, versions: &[LuaVersion])
pub fn set_allowed_dialects(&mut self, versions: &[LuaVersion])
Forward kevy_lua::Bridge::set_allowed_dialects.
use kevy_lua::LuaVersion;
let mut host = kevy_lua_host::LuaHost::<()>::new(|_, _argv, _ro| Vec::new());
host.set_allowed_dialects(&[LuaVersion::Lua51, LuaVersion::Lua54]);
assert_eq!(host.eval(&mut (), b"#!lua version=5.4\nreturn 3 // 2", &[], &[]), b":1\r\n");
assert!(host.eval(&mut (), b"#!lua version=5.3\nreturn 1", &[], &[]).starts_with(b"-ERR"));