Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lua transform): Explicitly call GC in lua transform #1990

Merged
2 commits merged into from
Mar 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/transforms/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ impl TransformConfig for LuaConfig {
}
}

// Lua's garbage collector sometimes seems to be not executed automatically on high event rates,
// which leads to leak-like RAM consumption pattern. This constant sets the number of invocations of
// the Lua transform after which GC would be called, thus ensuring that the RAM usage is not too high.
//
// This constant is larger than 1 because calling GC is an expensive operation, so doing it
// after each transform would have significant footprint on the performance.
const GC_INTERVAL: usize = 16;

pub struct Lua {
lua: rlua::Lua,
invocations_after_gc: usize,
}

impl Lua {
Expand Down Expand Up @@ -76,20 +85,28 @@ impl Lua {
})
.context(InvalidLua)?;

Ok(Self { lua })
Ok(Self {
lua,
invocations_after_gc: 0,
})
}

fn process(&self, event: Event) -> Result<Option<Event>, rlua::Error> {
self.lua.context(|ctx| {
fn process(&mut self, event: Event) -> Result<Option<Event>, rlua::Error> {
let result = self.lua.context(|ctx| {
let globals = ctx.globals();

globals.set("event", event)?;

let func = ctx.named_registry_value::<_, rlua::Function<'_>>("vector_func")?;
func.call(())?;

globals.get::<_, Option<Event>>("event")
})
});
self.invocations_after_gc += 1;
if self.invocations_after_gc % GC_INTERVAL == 0 {
self.lua.gc_collect()?;
self.invocations_after_gc = 0;
}
result
}
}

Expand Down Expand Up @@ -336,7 +353,7 @@ mod tests {

#[test]
fn lua_non_string_key_write() {
let transform = Lua::new(
let mut transform = Lua::new(
r#"
event[false] = "hello"
"#,
Expand All @@ -351,7 +368,7 @@ mod tests {

#[test]
fn lua_non_string_key_read() {
let transform = Lua::new(
let mut transform = Lua::new(
r#"
print(event[false])
"#,
Expand All @@ -366,7 +383,7 @@ mod tests {

#[test]
fn lua_script_error() {
let transform = Lua::new(
let mut transform = Lua::new(
r#"
error("this is an error")
"#,
Expand Down