Moonshark/core/workers
2025-03-15 20:57:56 -05:00
..
context.go workers 2 2025-03-06 06:23:17 -06:00
init_test.go revert 424b778f28 2025-03-15 20:57:56 -05:00
job.go workers 2 2025-03-06 06:23:17 -06:00
modules.go lua state optimizations 2025-03-14 21:56:51 -05:00
pool.go revert 424b778f28 2025-03-15 20:57:56 -05:00
README.md workers 2 2025-03-06 06:23:17 -06:00
sandbox.go revert 424b778f28 2025-03-15 20:57:56 -05:00
worker.go revert 424b778f28 2025-03-15 20:57:56 -05:00
workers_test.go revert 424b778f28 2025-03-15 20:57:56 -05:00

Worker Pool

Pool

type Pool struct { ... }

// Create a pool with specified number of workers
func NewPool(numWorkers int) (*Pool, error)

// Submit a job with default context
func (p *Pool) Submit(bytecode []byte, ctx *Context) (any, error)

// Submit with timeout/cancellation support
func (p *Pool) SubmitWithContext(ctx context.Context, bytecode []byte, execCtx *Context) (any, error)

// Shutdown the pool
func (p *Pool) Shutdown() error

// Get number of active workers
func (p *Pool) ActiveWorkers() uint32

Context

type Context struct { ... }

// Create a new execution context
func NewContext() *Context

// Set a value
func (c *Context) Set(key string, value any)

// Get a value
func (c *Context) Get(key string) any

Basic Usage

// Create worker pool
pool, err := workers.NewPool(4)
if err != nil {
    log.Fatal(err)
}
defer pool.Shutdown()

// Compile bytecode (typically done once and reused)
state := luajit.New()
bytecode, err := state.CompileBytecode(`
    return ctx.message .. " from Lua"
`, "script")
state.Close()

// Set up execution context
ctx := workers.NewContext()
ctx.Set("message", "Hello")
ctx.Set("params", map[string]any{"id": "123"})

// Execute bytecode
result, err := pool.Submit(bytecode, ctx)
if err != nil {
    log.Fatal(err)
}

fmt.Println(result) // "Hello from Lua"

With Timeout

// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

// Execute with timeout
result, err := pool.SubmitWithContext(ctx, bytecode, execCtx)
if err != nil {
    // Handle timeout or error
}

In Lua Scripts

Inside Lua, the context is available as the global ctx table:

-- Access a simple value
local msg = ctx.message

-- Access nested values
local id = ctx.params.id

-- Return a result to Go
return {
    status = "success",
    data = msg
}

Important Notes

  • The pool is thread-safe; multiple goroutines can submit jobs concurrently
  • Each execution is isolated; global state is reset between executions
  • Bytecode should be compiled once and reused for better performance
  • Context values should be serializable to Lua (numbers, strings, booleans, maps, slices)