Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PreCompileLuaCallback ¶
func PreCompileLuaCallback() (err error)
PreCompileLuaCallback pre-compiles the Lua callback script and replaces the current LuaCallback with the new one. If the LoadableConfig has a Lua callback and the current LuaCallback is nil, a new instance of PreCompiledLuaCallback is created. The function calls NewLuaCallback to get the new pre-compiled Lua callback script. If an error occurs during the pre-compilation, the function returns the error. If no error occurs, the new Lua callback script replaces the current LuaCallback's LuaScript. The function returns nil if it executes successfully.
func RunCallbackLuaRequest ¶
RunCallbackLuaRequest is a function that runs a Lua callback request in a Gin context. It creates a new context with a specified timeout taken from the "lua_script_timeout" configuration. The function fetches a Lua State object from a pool of Lua states and ensures its safe return to the pool upon completion. The Lua state is configured with the coroutine-aware version of the context. Global variables are then set up for the Lua state, and a precompiled Lua script is executed. Any encountered error during the execution of the script is captured and returned. Finally, the Lua table containing globals is cleaned up to prevent memory leaks.
Parameters: ctx *gin.Context - The Gin context for the HTTP request.
Returns: err error - An error if any occurred during the execution of the function.
Types ¶
type PreCompiledLuaCallback ¶
type PreCompiledLuaCallback struct {
// LuaScript is a pointers to a precompiled Lua script *lua.FunctionProto.,
LuaScript *lua.FunctionProto
// Mu is a read/write mutex used to allow safe concurrent access to the LuaScript.
Mu sync.RWMutex
}
PreCompiledLuaCallback represents a type that holds a precompiled Lua script and allows safe concurrent access to the script.
var ( // LuaCallback represents a variable that holds a precompiled Lua script and allows safe concurrent access to the script. LuaCallback *PreCompiledLuaCallback )
func NewLuaCallback ¶
func NewLuaCallback() (*PreCompiledLuaCallback, error)
NewLuaCallback compiles a Lua script based on the provided file path and returns a new instance of PreCompiledLuaCallback with the compiled script. If there is an error during the compilation, the function returns nil and the error. The returned PreCompiledLuaCallback can be used to replace the LuaScript of the current LuaCallback.
Example usage:
compiledScript, err := NewLuaCallback()
if err != nil {
fmt.Println("Error:", err)
return
}
LuaCallback.Replace(compiledScript)
PreCompiledLuaCallback declaration:
type PreCompiledLuaCallback struct {
LuaScript *lua.FunctionProto
Mu sync.RWMutex
}
PreCompiledLuaCallback.Replace declaration:
func (p *PreCompiledLuaCallback) Replace(luaCallback *PreCompiledLuaCallback)
PreCompiledLuaCallback.GetPrecompiledScript declaration:
func (p *PreCompiledLuaCallback) GetPrecompiledScript() *lua.FunctionProto
lualib.CompileLua declaration:
func CompileLua(filePath string) (*lua.FunctionProto, error)
config.LoadableConfig declaration:
var LoadableConfig *File
LuaCallback declaration:
var LuaCallback *PreCompiledLuaCallback
func (*PreCompiledLuaCallback) GetPrecompiledScript ¶
func (p *PreCompiledLuaCallback) GetPrecompiledScript() *lua.FunctionProto
GetPrecompiledScript is a method of the PreCompiledLuaCallback struct. It returns the precompiled Lua script as a pointer to the lua.FunctionProto. The method locks the read/write mutex of the PreCompiledLuaCallback using RLock() before returning the LuaScript. It then unlocks the mutex using RUnlock() in a deferred statement.
Returns:
- LuaScript *lua.FunctionProto: The precompiled Lua script as a pointer to the lua.FunctionProto.
Example usage:
script := p.GetPrecompiledScript() // Use the script for further processing
func (*PreCompiledLuaCallback) Replace ¶
func (p *PreCompiledLuaCallback) Replace(luaCallback *PreCompiledLuaCallback)
Replace is a method of the PreCompiledLuaCallback struct that replaces the LuaScript of p with the LuaScript of luaCallback. The method locks the read/write mutex of the PreCompiledLuaCallback using Lock() before assigning the new LuaScript to the target PreCompiledLuaCallback. It then unlocks the mutex using Unlock() in a deferred statement.
Parameters:
- luaCallback *PreCompiledLuaCallback: The PreCompiledLuaCallback with the LuaScript to replace the existing one.
Example usage:
newLuaCallback := &PreCompiledLuaCallback{
LuaScript: compiledScript,
}
currentLuaCallback.Replace(newLuaCallback)
PreCompiledLuaCallback declaration:
type PreCompiledLuaCallback struct {
LuaScript *lua.FunctionProto
Mu sync.RWMutex
}
PreCompiledLuaCallback.GetPrecompiledScript declaration:
func (p *PreCompiledLuaCallback) GetPrecompiledScript() *lua.FunctionProto
config.LoadableConfig declaration:
var LoadableConfig *File
LuaCallback declaration:
var LuaCallback *PreCompiledLuaCallback
NewLuaCallback declaration:
func NewLuaCallback() (*PreCompiledLuaCallback, error)
lualib.CompileLua declaration:
func CompileLua(filePath string) (*lua.FunctionProto, error)