-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
191 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package function | ||
|
||
import ( | ||
"RedisShake/internal/config" | ||
"RedisShake/internal/entry" | ||
"RedisShake/internal/log" | ||
lua "github.com/yuin/gopher-lua" | ||
"strings" | ||
) | ||
|
||
var luaString string | ||
|
||
func Init() { | ||
luaString = config.Opt.Function | ||
luaString = strings.TrimSpace(luaString) | ||
if len(luaString) == 0 { | ||
log.Infof("no function script") | ||
return | ||
} | ||
} | ||
|
||
// DB | ||
// GROUP | ||
// CMD | ||
// KEYS | ||
// SLOTS | ||
// ARGV | ||
|
||
// shake.call(DB, ARGV) | ||
// shake.log() | ||
|
||
func RunFunction(e *entry.Entry) []*entry.Entry { | ||
entries := make([]*entry.Entry, 0) | ||
if len(luaString) == 0 { | ||
entries = append(entries, e) | ||
return entries | ||
} | ||
|
||
L := lua.NewState() | ||
L.SetGlobal("DB", lua.LNumber(e.DbId)) | ||
L.SetGlobal("GROUP", lua.LString(e.Group)) | ||
L.SetGlobal("CMD", lua.LString(e.CmdName)) | ||
keys := L.NewTable() | ||
for _, key := range e.Keys { | ||
keys.Append(lua.LString(key)) | ||
} | ||
L.SetGlobal("KEYS", keys) | ||
slots := L.NewTable() | ||
for _, slot := range e.Slots { | ||
slots.Append(lua.LNumber(slot)) | ||
} | ||
L.SetGlobal("SLOTS", slots) | ||
argv := L.NewTable() | ||
for _, arg := range e.Argv { | ||
argv.Append(lua.LString(arg)) | ||
} | ||
L.SetGlobal("ARGV", argv) | ||
shake := L.NewTypeMetatable("shake") | ||
L.SetGlobal("shake", shake) | ||
|
||
L.SetField(shake, "call", L.NewFunction(func(ls *lua.LState) int { | ||
db := ls.ToInt(1) | ||
argv := ls.ToTable(2) | ||
var argvStrings []string | ||
for i := 1; i <= argv.Len(); i++ { | ||
argvStrings = append(argvStrings, argv.RawGetInt(i).String()) | ||
} | ||
entries = append(entries, &entry.Entry{ | ||
DbId: db, | ||
Argv: argvStrings, | ||
}) | ||
return 0 | ||
})) | ||
L.SetField(shake, "log", L.NewFunction(func(ls *lua.LState) int { | ||
log.Infof("lua log: %v", ls.ToString(1)) | ||
return 0 | ||
})) | ||
err := L.DoString(luaString) | ||
if err != nil { | ||
log.Panicf("load function script failed: %v", err) | ||
} | ||
|
||
return entries | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.