Can Kong/NGINX be configured to not save Lua "state" between requests?

I’m using a Lua library I found on GitHub which parses XML into a Lua table in a custom Kong plug-in I’ve written. It seems to work great when I run it interactively, but when I use it in my Kong plug-in, it seems to retain “state” from the previous request, in that the Lua table which the XML parsing library generates has data from the current request and all previous requests. For example, after starting Kong, the table has what I would expect after the first request:

{
  string = {
    "example@foo.com",
    _attr = {
      xmlns = "http://foo.com.BARRSD/"
    }
  }
}

But after the next request, the table has the new data plus what it had before:

{
  string = {
    "example@foo.com",
    {
      "example@foo.com",
      _attr = {
        xmlns = "http://foo.com.BARRSD/"
      }
    },
    _attr = {
      xmlns = "http://foo.com.BARRSD/"
    }
  }
}

I’ve posted a question/issue in that GitHub repo to ask whether there’s a way to “reset” the table using the library, but my question here is whether there’s a configuration setting for Kong/NGINX to have it use a new Lua “state” for each request. This is my plug-in code called by the handler module:

local xml2lua = require("xml2lua")
--Uses a handler that converts the XML to a Lua table
local handler = require("xmlhandler.tree")
local rapidjson = require('rapidjson')

local _M = {}

local function read_xml_body(xml_body)
  if xml_body then
    local parser = xml2lua.parser(handler)
    parser:parse(xml_body)
    require 'pl.pretty'.dump(handler.root)
    return handler.root
  end
end

function _M.transform_xml_body(conf, buffered_data)
  local xml_as_lua = read_xml_body(buffered_data)
  if xml_as_lua == nil then
    return
  end
  return rapidjson.encode(xml_as_lua)
end

return _M

Thanks!

Wanted to post that I was able to get it working, but I did have to use a method in one of the included packages to effectively create a “clean slate”. So it would seem Lua objects can hang around between requests.


© 2019 Kong Inc.    Terms  •  Privacy  •  FAQ