filter

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2024 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

LuaPool is a pool of Lua state instances.

Functions

func PreCompileLuaFilters

func PreCompileLuaFilters() (err error)

PreCompileLuaFilters is a function that pre-compiles Lua filters. It iterates over the filters available in the configuration. For each filter, it creates a new LuaFilter instance passing the filter name and script path, and then adds it to the LuaFilters. Note: If LuaFilters is nil, a new instance of PreCompiledLuaFilters is created. If LuaFilters already exists, it's reset before the new filters are added. If an error occurs when creating a new LuaFilter, it returns immediately with that error. It returns nil if no error occurs.

Returns:

error if any error occurs while initializing the Lua filters

Types

type LuaBackendServer

type LuaBackendServer struct {
	Protocol  string
	IP        string
	Port      int
	HAProxyV2 bool
	TLS       bool
}

type LuaFilter

type LuaFilter struct {
	// Name is a string that represents the name of the Lua filter.
	Name string

	// CompiledScript is a pointer to a FunctionProto struct from the go-lua package.
	// It represents a compiled Lua function that can be executed by a Lua VM.
	CompiledScript *lua.FunctionProto
}

LuaFilter represents a struct for managing Lua filters. It contains fields for filter name and a compiled Lua script.

func NewLuaFilter

func NewLuaFilter(name string, scriptPath string) (*LuaFilter, error)

NewLuaFilter creates a new instance of LuaFilter. It requires two parameters: name and scriptPath. The name parameter is a string representing the name of the LuaFilter. If it is empty, an error is returned. The scriptPath parameter is a string representing the path to a Lua script file. If the scriptPath is empty, an error is returned. If the scriptPath is valid, the Lua script at the given path is compiled. If script compilation fails, it returns the related error. If both parameters are valid and the script compilation is successful, a pointer to the LuaFilter instance is returned. The returned LuaFilter instance includes the provided name and the compiled script.

type PreCompiledLuaFilters

type PreCompiledLuaFilters struct {
	// LuaScripts is a slice of pointers to LuaFilter,
	// each of which represents a precompiled Lua script.
	LuaScripts []*LuaFilter

	// Mu is a read/write mutex used to allow safe concurrent access to the LuaScripts.
	Mu sync.RWMutex
}

PreCompiledLuaFilters represents a collection of precompiled Lua scripts along with a mutex for handling concurrent access to the script data.

var LuaFilters *PreCompiledLuaFilters

LuaFilters holds pre-compiled Lua scripts for use across the application. It allows faster access and execution of frequently used scripts.

func (*PreCompiledLuaFilters) Add

func (a *PreCompiledLuaFilters) Add(luaFilter *LuaFilter)

Add appends a LuaFilter to the LuaScripts in the PreCompiledLuaFilters. It ensures thread-safety by obtaining a lock before performing the operation, and then unlocking once the operation is complete.

Parameters:

luaFilter: The LuaFilter instance that should be added.

Usage:

luaFilters := &PreCompiledLuaFilters{}
filter := &LuaFilter{}
luaFilters.Add(filter)

func (*PreCompiledLuaFilters) Reset

func (a *PreCompiledLuaFilters) Reset()

Reset clears the LuaScripts slice of a PreCompiledLuaFilters object.The method also prevents race conditions by Locking the Mutex before executing, and Unlocking once it has finished. Existing entries in the slice are discarded.

type Request

type Request struct {
	BackendServers []*config.BackendServer

	UsedBackendAddress *string

	UsedBackendPort *int

	// Log is used to capture logging information.
	Logs *lualib.CustomLogKeyValue

	// Context includes context data from the caller.
	*lualib.Context

	*lualib.CommonRequest
}

func (*Request) CallFilterLua

func (r *Request) CallFilterLua(ctx *gin.Context) (action bool, backendResult *lualib.LuaBackendResult, err error)

CallFilterLua attempts to execute Lua scripts defined in LuaFilters. It returns true if at least one of the scripts executed successfully, otherwise it returns false. The error return value is used to indicate any issues with the Lua filters.

It initially checks if any LuaFilters are defined. If none are found, it returns false with an ErrNoFiltersDefined error. It then creates a new Lua state and sets up the necessary global variables and request context. Scripts from the LuaFilters are executed in sequence within the provided context until a script executes successfully or all scripts have been attempted. If the context has been cancelled, the function returns without executing any more scripts. If a script returns an error, it is skipped and the next script is tried.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL