43 lines
994 B
Go
43 lines
994 B
Go
package runner
|
|
|
|
import "sync"
|
|
|
|
// Context represents execution context for a Lua script
|
|
type Context struct {
|
|
// Generic map for any context values (route params, HTTP request info, etc.)
|
|
Values map[string]any
|
|
}
|
|
|
|
// Context pool to reduce allocations
|
|
var contextPool = sync.Pool{
|
|
New: func() interface{} {
|
|
return &Context{
|
|
Values: make(map[string]any, 16), // Pre-allocate with reasonable capacity
|
|
}
|
|
},
|
|
}
|
|
|
|
// NewContext creates a new context, potentially reusing one from the pool
|
|
func NewContext() *Context {
|
|
return contextPool.Get().(*Context)
|
|
}
|
|
|
|
// Release returns the context to the pool after clearing its values
|
|
func (c *Context) Release() {
|
|
// Clear all values to prevent data leakage
|
|
for k := range c.Values {
|
|
delete(c.Values, k)
|
|
}
|
|
contextPool.Put(c)
|
|
}
|
|
|
|
// Set adds a value to the context
|
|
func (c *Context) Set(key string, value any) {
|
|
c.Values[key] = value
|
|
}
|
|
|
|
// Get retrieves a value from the context
|
|
func (c *Context) Get(key string) any {
|
|
return c.Values[key]
|
|
}
|