From 3480a28731474253db3154e9a7d4a3a2a844cc40 Mon Sep 17 00:00:00 2001 From: Glen De Cauwsemaecker Date: Wed, 12 Apr 2017 20:08:58 -0500 Subject: [PATCH] support redis as a global var in lua scripts (#289) by supporting both ledis and redis as the global lua script var, we keep ledis support as it is, while also supporting redis clients, which will be sending scripts, whilst assuming redis is the global --- server/script.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/server/script.go b/server/script.go index ecd884a..ea4c7af 100644 --- a/server/script.go +++ b/server/script.go @@ -182,6 +182,22 @@ func (app *App) openScript() { w.l = l s.c.resp = w + setGlobalDBScriptVar(l, "ledis") + setGlobalDBScriptVar(l, "redis") + + setMapState(l, s) +} + +func (app *App) closeScript() { + app.script.l.Close() + delMapState(app.script.l) + app.script = nil +} + +var mapState = map[*lua.State]*script{} +var stateLock sync.Mutex + +func setGlobalDBScriptVar(l *lua.State, name string) { l.NewTable() l.PushString("call") l.PushGoFunction(luaCall) @@ -203,20 +219,9 @@ func (app *App) openScript() { l.PushGoFunction(luaStatusReply) l.SetTable(-3) - l.SetGlobal("ledis") - - setMapState(l, s) + l.SetGlobal(name) } -func (app *App) closeScript() { - app.script.l.Close() - delMapState(app.script.l) - app.script = nil -} - -var mapState = map[*lua.State]*script{} -var stateLock sync.Mutex - func setMapState(l *lua.State, s *script) { stateLock.Lock() defer stateLock.Unlock()