Skip to content

Commit

Permalink
Use a new instance from the Lua pool when setting up a handler
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Oct 9, 2016
1 parent 40897a9 commit e89ea7e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions luahandler.go
Expand Up @@ -20,12 +20,16 @@ func exportLuaHandlerFunctions(L *lua.LState, filename string, perm pinterface.I
handleFunc := L.ToFunction(2)

wrappedHandleFunc := func(w http.ResponseWriter, req *http.Request) {

L2 := luapool.Get()
defer luapool.Put(L2)

// Set up a new Lua state with the current http.ResponseWriter and *http.Request
exportCommonFunctions(w, req, filename, perm, L, luapool, nil, cache, httpStatus, pongomutex)
exportCommonFunctions(w, req, filename, perm, L2, luapool, nil, cache, httpStatus, pongomutex)

// Then run the given Lua function
L.Push(handleFunc)
if err := L.PCall(0, lua.MultRet, nil); err != nil {
L2.Push(handleFunc)
if err := L2.PCall(0, lua.MultRet, nil); err != nil {
// Non-fatal error
log.Error("Handler for "+handlePath+" failed:", err)
}
Expand Down

0 comments on commit e89ea7e

Please sign in to comment.