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
This commit is contained in:
Glen De Cauwsemaecker 2017-04-12 20:08:58 -05:00 committed by siddontang
parent 5929802e2e
commit 3480a28731
1 changed files with 17 additions and 12 deletions

View File

@ -182,6 +182,22 @@ func (app *App) openScript() {
w.l = l w.l = l
s.c.resp = w 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.NewTable()
l.PushString("call") l.PushString("call")
l.PushGoFunction(luaCall) l.PushGoFunction(luaCall)
@ -203,20 +219,9 @@ func (app *App) openScript() {
l.PushGoFunction(luaStatusReply) l.PushGoFunction(luaStatusReply)
l.SetTable(-3) l.SetTable(-3)
l.SetGlobal("ledis") l.SetGlobal(name)
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 setMapState(l *lua.State, s *script) { func setMapState(l *lua.State, s *script) {
stateLock.Lock() stateLock.Lock()
defer stateLock.Unlock() defer stateLock.Unlock()