goroutine runner 1

This commit is contained in:
Sky Johnson 2025-03-28 21:56:58 -05:00
parent 78337988bd
commit 6fadf26bea
6 changed files with 266 additions and 282 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"runtime"
"syscall" "syscall"
"time" "time"
@ -151,7 +152,7 @@ func main() {
routesDir := cfg.GetString("routes_dir", "./routes") routesDir := cfg.GetString("routes_dir", "./routes")
staticDir := cfg.GetString("static_dir", "./static") staticDir := cfg.GetString("static_dir", "./static")
overrideDir := cfg.GetString("override_dir", "./override") overrideDir := cfg.GetString("override_dir", "./override")
bufferSize := cfg.GetInt("buffer_size", 20) poolSize := cfg.GetInt("pool_size", runtime.NumCPU())
if err := utils.EnsureDir(overrideDir); err != nil { if err := utils.EnsureDir(overrideDir); err != nil {
logger.Warning("Override directory doesn't exist, and could not create it: %v", err) logger.Warning("Override directory doesn't exist, and could not create it: %v", err)
@ -181,16 +182,23 @@ func main() {
logger.Fatal("Router initialization failed: %v", err) logger.Fatal("Router initialization failed: %v", err)
} }
// Initialize Lua runner // Initialize Lua runner with options
luaRunner, err := runner.NewRunner( runnerOpts := []runner.RunnerOption{
runner.WithBufferSize(bufferSize), runner.WithPoolSize(poolSize),
runner.WithLibDirs(libDirs...), runner.WithLibDirs(libDirs...),
runner.WithDebugEnabled(), }
)
// Add debug option conditionally
if debugMode {
runnerOpts = append(runnerOpts, runner.WithDebugEnabled())
logger.Debug("Debug logging enabled for Lua runner")
}
luaRunner, err := runner.NewRunner(runnerOpts...)
if err != nil { if err != nil {
logger.Fatal("Failed to initialize Lua runner: %v", err) logger.Fatal("Failed to initialize Lua runner: %v", err)
} }
logger.Server("Lua runner initialized with buffer size %d", bufferSize) logger.Server("Lua runner initialized with pool size %d", poolSize)
defer luaRunner.Close() defer luaRunner.Close()
// Set up file watchers if enabled // Set up file watchers if enabled

View File

@ -3,6 +3,7 @@ package config
import ( import (
"errors" "errors"
"fmt" "fmt"
"runtime"
luajit "git.sharkk.net/Sky/LuaJIT-to-Go" luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
) )
@ -22,6 +23,7 @@ type Config struct {
// Performance settings // Performance settings
BufferSize int BufferSize int
PoolSize int // Number of Lua states in the pool
// Feature flags // Feature flags
HTTPLoggingEnabled bool HTTPLoggingEnabled bool
@ -46,7 +48,7 @@ func New() *Config {
LibDirs: []string{"./libs"}, LibDirs: []string{"./libs"},
// Performance defaults // Performance defaults
BufferSize: 20, PoolSize: runtime.NumCPU(),
// Feature flag defaults // Feature flag defaults
HTTPLoggingEnabled: true, HTTPLoggingEnabled: true,
@ -233,9 +235,9 @@ func processConfigValue(state *luajit.State, config *Config, key string, valueTy
if strVal, ok := value.(string); ok { if strVal, ok := value.(string); ok {
config.OverrideDir = strVal config.OverrideDir = strVal
} }
case "buffer_size": case "pool_size":
if numVal, ok := value.(float64); ok { if numVal, ok := value.(float64); ok {
config.BufferSize = int(numVal) config.PoolSize = int(numVal)
} }
case "http_logging_enabled": case "http_logging_enabled":
if boolVal, ok := value.(bool); ok { if boolVal, ok := value.(bool); ok {
@ -357,6 +359,8 @@ func (c *Config) GetInt(key string, defaultValue int) int {
return c.Port return c.Port
case "buffer_size": case "buffer_size":
return c.BufferSize return c.BufferSize
case "pool_size":
return c.PoolSize
} }
// Fall back to values map for other keys // Fall back to values map for other keys
@ -608,11 +612,11 @@ func (c *Config) Set(key string, value any) {
if strVal, ok := value.(string); ok { if strVal, ok := value.(string); ok {
c.OverrideDir = strVal c.OverrideDir = strVal
} }
case "buffer_size": case "pool_size":
if numVal, ok := value.(float64); ok { if numVal, ok := value.(float64); ok {
c.BufferSize = int(numVal) c.PoolSize = int(numVal)
} else if intVal, ok := value.(int); ok { } else if intVal, ok := value.(int); ok {
c.BufferSize = intVal c.PoolSize = intVal
} }
case "http_logging_enabled": case "http_logging_enabled":
if boolVal, ok := value.(bool); ok { if boolVal, ok := value.(bool); ok {

View File

@ -1,15 +0,0 @@
package runner
// JobResult represents the result of a Lua script execution
type JobResult struct {
Value any // Return value from Lua
Error error // Error if any
}
// job represents a Lua script execution request
type job struct {
Bytecode []byte // Compiled LuaJIT bytecode
Context *Context // Execution context
ScriptPath string // Path to the original script (for require resolution)
Result chan<- JobResult // Channel to send result back
}

View File

@ -24,34 +24,36 @@ type StateInitFunc func(*luajit.State) error
// RunnerOption defines a functional option for configuring the LuaRunner // RunnerOption defines a functional option for configuring the LuaRunner
type RunnerOption func(*LuaRunner) type RunnerOption func(*LuaRunner)
// Result channel pool to reduce allocations // JobResult represents the result of a Lua script execution
var resultChanPool = sync.Pool{ type JobResult struct {
New: func() interface{} { Value any // Return value from Lua
return make(chan JobResult, 1) Error error // Error if any
},
} }
// LuaRunner runs Lua scripts using multiple Lua states in a round-robin fashion // StateWrapper wraps a Lua state with its sandbox
type StateWrapper struct {
state *luajit.State // The Lua state
sandbox *Sandbox // Associated sandbox
index int // Index for debugging
}
// LuaRunner runs Lua scripts using a pool of Lua states
type LuaRunner struct { type LuaRunner struct {
states []*luajit.State // Multiple Lua states for parallel execution states []*StateWrapper // Pool of Lua states
jobQueues []chan job // Each state has its own job queue stateSem chan int // Semaphore with state indexes
workerCount int // Number of worker states (default 4) poolSize int // Size of the state pool
nextWorker int32 // Atomic counter for round-robin distribution initFunc StateInitFunc // Optional function to initialize Lua states
moduleLoader *NativeModuleLoader // Native module loader for require
isRunning atomic.Bool // Flag indicating if the runner is active isRunning atomic.Bool // Flag indicating if the runner is active
mu sync.RWMutex // Mutex for thread safety mu sync.RWMutex // Mutex for thread safety
wg sync.WaitGroup // WaitGroup for clean shutdown
initFunc StateInitFunc // Optional function to initialize Lua states
bufferSize int // Size of each job queue buffer
moduleLoader *NativeModuleLoader // Native module loader for require
sandboxes []*Sandbox // Sandbox for each state
debug bool // Enable debug logging debug bool // Enable debug logging
} }
// WithBufferSize sets the job queue buffer size // WithPoolSize sets the state pool size
func WithBufferSize(size int) RunnerOption { func WithPoolSize(size int) RunnerOption {
return func(r *LuaRunner) { return func(r *LuaRunner) {
if size > 0 { if size > 0 {
r.bufferSize = size r.poolSize = size
} }
} }
} }
@ -83,22 +85,12 @@ func WithDebugEnabled() RunnerOption {
} }
} }
// WithWorkerCount sets the number of worker states (min 1) // NewRunner creates a new LuaRunner with a pool of states
func WithWorkerCount(count int) RunnerOption {
return func(r *LuaRunner) {
if count > 0 {
r.workerCount = count
}
}
}
// NewRunner creates a new LuaRunner with multiple worker states
func NewRunner(options ...RunnerOption) (*LuaRunner, error) { func NewRunner(options ...RunnerOption) (*LuaRunner, error) {
// Default configuration // Default configuration
runner := &LuaRunner{ runner := &LuaRunner{
bufferSize: 10, poolSize: runtime.GOMAXPROCS(0), // Default pool size to number of CPUs
workerCount: runtime.GOMAXPROCS(0), debug: false,
debug: false,
} }
// Apply options // Apply options
@ -106,11 +98,6 @@ func NewRunner(options ...RunnerOption) (*LuaRunner, error) {
opt(runner) opt(runner)
} }
// Initialize states and job queues
runner.states = make([]*luajit.State, runner.workerCount)
runner.jobQueues = make([]chan job, runner.workerCount)
runner.sandboxes = make([]*Sandbox, runner.workerCount)
// Set up module loader if not already initialized // Set up module loader if not already initialized
if runner.moduleLoader == nil { if runner.moduleLoader == nil {
requireConfig := &RequireConfig{ requireConfig := &RequireConfig{
@ -120,28 +107,23 @@ func NewRunner(options ...RunnerOption) (*LuaRunner, error) {
runner.moduleLoader = NewNativeModuleLoader(requireConfig) runner.moduleLoader = NewNativeModuleLoader(requireConfig)
} }
// Create job queues and initialize states // Initialize states and semaphore
for i := 0; i < runner.workerCount; i++ { runner.states = make([]*StateWrapper, runner.poolSize)
// Create job queue runner.stateSem = make(chan int, runner.poolSize)
runner.jobQueues[i] = make(chan job, runner.bufferSize)
// Create sandbox // Create and initialize all states
runner.sandboxes[i] = NewSandbox() for i := 0; i < runner.poolSize; i++ {
wrapper, err := runner.initState(i)
// Initialize state if err != nil {
if err := runner.initState(i, true); err != nil { runner.Close() // Clean up already created states
// Clean up if initialization fails
runner.Close()
return nil, err return nil, err
} }
// Start worker goroutine runner.states[i] = wrapper
runner.wg.Add(1) runner.stateSem <- i // Add index to semaphore
go runner.processJobs(i)
} }
runner.isRunning.Store(true) runner.isRunning.Store(true)
runner.nextWorker = 0
return runner, nil return runner, nil
} }
@ -153,222 +135,158 @@ func (r *LuaRunner) debugLog(format string, args ...interface{}) {
} }
} }
// initState initializes or reinitializes a specific Lua state // initState creates and initializes a new state
func (r *LuaRunner) initState(workerIndex int, initial bool) error { func (r *LuaRunner) initState(index int) (*StateWrapper, error) {
r.debugLog("Initializing Lua state %d (initial=%v)", workerIndex, initial) r.debugLog("Initializing Lua state %d", index)
// Clean up existing state if there is one // Create a new state
if r.states[workerIndex] != nil {
r.debugLog("Cleaning up existing state %d", workerIndex)
// Always call Cleanup before Close to properly free function pointers
r.states[workerIndex].Cleanup()
r.states[workerIndex].Close()
r.states[workerIndex] = nil
}
// Create fresh state
state := luajit.New() state := luajit.New()
if state == nil { if state == nil {
return errors.New("failed to create Lua state") return nil, errors.New("failed to create Lua state")
}
r.debugLog("Created new Lua state %d", index)
// Create sandbox
sandbox := NewSandbox()
if r.debug {
sandbox.EnableDebug()
} }
r.debugLog("Created new Lua state %d", workerIndex)
// Set up require paths and mechanism // Set up require paths and mechanism
if err := r.moduleLoader.SetupRequire(state); err != nil { if err := r.moduleLoader.SetupRequire(state); err != nil {
r.debugLog("Failed to set up require for state %d: %v", workerIndex, err) r.debugLog("Failed to set up require for state %d: %v", index, err)
state.Cleanup() state.Cleanup()
state.Close() state.Close()
return ErrInitFailed return nil, ErrInitFailed
} }
r.debugLog("Require system initialized for state %d", workerIndex) r.debugLog("Require system initialized for state %d", index)
// Initialize all core modules from the registry // Initialize all core modules from the registry
if err := GlobalRegistry.Initialize(state); err != nil { if err := GlobalRegistry.Initialize(state); err != nil {
r.debugLog("Failed to initialize core modules for state %d: %v", workerIndex, err) r.debugLog("Failed to initialize core modules for state %d: %v", index, err)
state.Cleanup() state.Cleanup()
state.Close() state.Close()
return ErrInitFailed return nil, ErrInitFailed
} }
r.debugLog("Core modules initialized for state %d", workerIndex) r.debugLog("Core modules initialized for state %d", index)
// Check if http module is properly registered
testResult, err := state.ExecuteWithResult(`
if type(http) == "table" and type(http.client) == "table" and
type(http.client.get) == "function" then
return true
else
return false
end
`)
if err != nil || testResult != true {
r.debugLog("HTTP module verification failed for state %d: %v, result: %v", workerIndex, err, testResult)
} else {
r.debugLog("HTTP module verified OK for state %d", workerIndex)
}
// Verify __http_request function
testResult, _ = state.ExecuteWithResult(`return type(__http_request)`)
r.debugLog("__http_request function for state %d is of type: %v", workerIndex, testResult)
// Set up sandbox after core modules are initialized // Set up sandbox after core modules are initialized
if err := r.sandboxes[workerIndex].Setup(state); err != nil { if err := sandbox.Setup(state); err != nil {
r.debugLog("Failed to set up sandbox for state %d: %v", workerIndex, err) r.debugLog("Failed to set up sandbox for state %d: %v", index, err)
state.Cleanup() state.Cleanup()
state.Close() state.Close()
return ErrInitFailed return nil, ErrInitFailed
} }
r.debugLog("Sandbox environment set up for state %d", workerIndex) r.debugLog("Sandbox environment set up for state %d", index)
// Preload all modules into package.loaded // Preload all modules into package.loaded
if err := r.moduleLoader.PreloadAllModules(state); err != nil { if err := r.moduleLoader.PreloadAllModules(state); err != nil {
r.debugLog("Failed to preload modules for state %d: %v", workerIndex, err) r.debugLog("Failed to preload modules for state %d: %v", index, err)
state.Cleanup() state.Cleanup()
state.Close() state.Close()
return errors.New("failed to preload modules") return nil, errors.New("failed to preload modules")
} }
r.debugLog("All modules preloaded for state %d", workerIndex) r.debugLog("All modules preloaded for state %d", index)
// Run init function if provided // Run init function if provided
if r.initFunc != nil { if r.initFunc != nil {
if err := r.initFunc(state); err != nil { if err := r.initFunc(state); err != nil {
r.debugLog("Custom init function failed for state %d: %v", workerIndex, err) r.debugLog("Custom init function failed for state %d: %v", index, err)
state.Cleanup() state.Cleanup()
state.Close() state.Close()
return ErrInitFailed return nil, ErrInitFailed
} }
r.debugLog("Custom init function completed for state %d", workerIndex) r.debugLog("Custom init function completed for state %d", index)
} }
// Test for HTTP module again after full initialization r.debugLog("State %d initialization complete", index)
testResult, err = state.ExecuteWithResult(`
if type(http) == "table" and type(http.client) == "table" and
type(http.client.get) == "function" then
return true
else
return false
end
`)
if err != nil || testResult != true {
r.debugLog("Final HTTP module verification failed for state %d: %v, result: %v", workerIndex, err, testResult)
} else {
r.debugLog("Final HTTP module verification OK for state %d", workerIndex)
}
r.states[workerIndex] = state return &StateWrapper{
r.debugLog("State %d initialization complete", workerIndex) state: state,
return nil sandbox: sandbox,
} index: index,
}, nil
// processJobs handles the job queue for a specific worker
func (r *LuaRunner) processJobs(workerIndex int) {
defer r.wg.Done()
defer func() {
if r.states[workerIndex] != nil {
r.debugLog("Cleaning up Lua state %d in processJobs", workerIndex)
r.states[workerIndex].Cleanup()
r.states[workerIndex].Close()
r.states[workerIndex] = nil
}
}()
for job := range r.jobQueues[workerIndex] {
// Execute the job and send result
result := r.executeJob(workerIndex, job)
select {
case job.Result <- result:
// Result sent successfully
default:
// Result channel closed or full, discard the result
}
}
}
// executeJob runs a script in the sandbox environment
func (r *LuaRunner) executeJob(workerIndex int, j job) JobResult {
// If the job has a script path, update script dir for module resolution
if j.ScriptPath != "" {
r.mu.Lock()
r.moduleLoader.config.ScriptDir = filepath.Dir(j.ScriptPath)
r.mu.Unlock()
}
// Convert context for sandbox
var ctx map[string]any
if j.Context != nil {
ctx = j.Context.Values
}
r.mu.RLock()
state := r.states[workerIndex]
sandbox := r.sandboxes[workerIndex]
r.mu.RUnlock()
if state == nil {
return JobResult{nil, errors.New("lua state is not initialized")}
}
// Execute in sandbox
value, err := sandbox.Execute(state, j.Bytecode, ctx)
return JobResult{value, err}
} }
// RunWithContext executes a Lua script with context and timeout // RunWithContext executes a Lua script with context and timeout
func (r *LuaRunner) RunWithContext(ctx context.Context, bytecode []byte, execCtx *Context, scriptPath string) (any, error) { func (r *LuaRunner) RunWithContext(ctx context.Context, bytecode []byte, execCtx *Context, scriptPath string) (any, error) {
r.mu.RLock()
if !r.isRunning.Load() { if !r.isRunning.Load() {
r.mu.RUnlock()
return nil, ErrRunnerClosed return nil, ErrRunnerClosed
} }
r.mu.RUnlock()
// Get a result channel from the pool // Create a result channel
resultChanInterface := resultChanPool.Get() resultChan := make(chan JobResult, 1)
resultChan := resultChanInterface.(chan JobResult)
// Make sure to clear any previous results // Get a state index with timeout
var stateIndex int
select { select {
case <-resultChan: case stateIndex = <-r.stateSem:
// Drain the channel if it has a value // Got a state
default:
// Channel is already empty
}
j := job{
Bytecode: bytecode,
Context: execCtx,
ScriptPath: scriptPath,
Result: resultChan,
}
// Choose worker in round-robin fashion
workerIndex := int(atomic.AddInt32(&r.nextWorker, 1) % int32(r.workerCount))
// Submit job with context
select {
case r.jobQueues[workerIndex] <- j:
// Job submitted
r.debugLog("Job submitted to worker %d", workerIndex)
case <-ctx.Done(): case <-ctx.Done():
// Return the channel to the pool before exiting
resultChanPool.Put(resultChan)
return nil, ctx.Err() return nil, ctx.Err()
} }
// Launch a goroutine to execute the job
go func() {
// Make sure to return the state to the pool when done
defer func() {
// Only return if runner is still open
if r.isRunning.Load() {
select {
case r.stateSem <- stateIndex:
// State returned to pool
default:
// Pool is full or closed (shouldn't happen)
}
}
}()
// Execute the job
var result JobResult
r.mu.RLock()
state := r.states[stateIndex]
r.mu.RUnlock()
if state == nil {
result = JobResult{nil, errors.New("state is not initialized")}
} else {
// Set script directory for module resolution
if scriptPath != "" {
r.mu.Lock()
r.moduleLoader.config.ScriptDir = filepath.Dir(scriptPath)
r.mu.Unlock()
}
// Convert context
var ctxMap map[string]any
if execCtx != nil {
ctxMap = execCtx.Values
}
// Execute in sandbox
value, err := state.sandbox.Execute(state.state, bytecode, ctxMap)
result = JobResult{value, err}
}
// Send result
select {
case resultChan <- result:
// Result sent
default:
// Result channel closed or full (shouldn't happen with buffered channel)
}
}()
// Wait for result with context // Wait for result with context
var result JobResult
select { select {
case result = <-resultChan: case result := <-resultChan:
// Got result return result.Value, result.Error
case <-ctx.Done(): case <-ctx.Done():
// Return the channel to the pool before exiting // Note: we can't cancel the Lua execution, but we can stop waiting for it
resultChanPool.Put(resultChan) // The state will be returned to the pool when the goroutine completes
return nil, ctx.Err() return nil, ctx.Err()
} }
// Return the channel to the pool
resultChanPool.Put(resultChan)
return result.Value, result.Error
} }
// Run executes a Lua script // Run executes a Lua script
@ -387,15 +305,26 @@ func (r *LuaRunner) Close() error {
r.isRunning.Store(false) r.isRunning.Store(false)
// Close all job queues // Drain the semaphore (non-blocking)
for i := 0; i < r.workerCount; i++ { for {
if r.jobQueues[i] != nil { select {
close(r.jobQueues[i]) case <-r.stateSem:
// Drained one slot
default:
// Empty
goto drained
} }
} }
drained:
// Wait for all workers to finish // Clean up all states
r.wg.Wait() for i := 0; i < len(r.states); i++ {
if r.states[i] != nil {
r.states[i].state.Cleanup()
r.states[i].state.Close()
r.states[i] = nil
}
}
return nil return nil
} }
@ -407,15 +336,54 @@ func (r *LuaRunner) NotifyFileChanged(filePath string) bool {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
// Reset all states on file changes // Check if runner is closed
if !r.isRunning.Load() {
return false
}
// Create a new semaphore
newSem := make(chan int, cap(r.stateSem))
// Drain the current semaphore (non-blocking)
for {
select {
case <-r.stateSem:
// Drained one slot
default:
// Empty
goto drained
}
}
drained:
r.stateSem = newSem
// Reinitialize all states
success := true success := true
for i := 0; i < r.workerCount; i++ { for i := 0; i < len(r.states); i++ {
err := r.initState(i, false) // Clean up old state
if r.states[i] != nil {
r.states[i].state.Cleanup()
r.states[i].state.Close()
}
// Initialize new state
wrapper, err := r.initState(i)
if err != nil { if err != nil {
r.debugLog("Failed to reinitialize state %d: %v", i, err) r.debugLog("Failed to reinitialize state %d: %v", i, err)
success = false success = false
} else { r.states[i] = nil
r.debugLog("State %d successfully reinitialized", i) continue
}
r.states[i] = wrapper
// Add to semaphore
select {
case newSem <- i:
// Added to semaphore
default:
// Semaphore full (shouldn't happen)
} }
} }
@ -429,9 +397,9 @@ func (r *LuaRunner) ResetModuleCache() {
r.mu.RLock() r.mu.RLock()
defer r.mu.RUnlock() defer r.mu.RUnlock()
for i := 0; i < r.workerCount; i++ { for i := 0; i < len(r.states); i++ {
if r.states[i] != nil { if r.states[i] != nil && r.states[i].state != nil {
r.moduleLoader.ResetModules(r.states[i]) r.moduleLoader.ResetModules(r.states[i].state)
} }
} }
} }
@ -444,9 +412,9 @@ func (r *LuaRunner) ReloadAllModules() error {
r.mu.RLock() r.mu.RLock()
defer r.mu.RUnlock() defer r.mu.RUnlock()
for i := 0; i < r.workerCount; i++ { for i := 0; i < len(r.states); i++ {
if r.states[i] != nil { if r.states[i] != nil && r.states[i].state != nil {
if err := r.moduleLoader.PreloadAllModules(r.states[i]); err != nil { if err := r.moduleLoader.PreloadAllModules(r.states[i].state); err != nil {
return err return err
} }
} }
@ -461,10 +429,10 @@ func (r *LuaRunner) RefreshModuleByName(modName string) bool {
defer r.mu.RUnlock() defer r.mu.RUnlock()
success := true success := true
for i := 0; i < r.workerCount; i++ { for i := 0; i < len(r.states); i++ {
if r.states[i] != nil { if r.states[i] != nil && r.states[i].state != nil {
r.debugLog("Refreshing module %s in state %d", modName, i) r.debugLog("Refreshing module %s in state %d", modName, i)
if err := r.states[i].DoString(`package.loaded["` + modName + `"] = nil`); err != nil { if err := r.states[i].state.DoString(`package.loaded["` + modName + `"] = nil`); err != nil {
success = false success = false
} }
} }
@ -475,8 +443,13 @@ func (r *LuaRunner) RefreshModuleByName(modName string) bool {
// AddModule adds a module to all sandbox environments // AddModule adds a module to all sandbox environments
func (r *LuaRunner) AddModule(name string, module any) { func (r *LuaRunner) AddModule(name string, module any) {
r.debugLog("Adding module %s to all sandboxes", name) r.debugLog("Adding module %s to all sandboxes", name)
for i := 0; i < r.workerCount; i++ { r.mu.RLock()
r.sandboxes[i].AddModule(name, module) defer r.mu.RUnlock()
for i := 0; i < len(r.states); i++ {
if r.states[i] != nil && r.states[i].sandbox != nil {
r.states[i].sandbox.AddModule(name, module)
}
} }
} }
@ -487,19 +460,22 @@ func (r *LuaRunner) GetModuleCount() int {
count := 0 count := 0
// Get module count from the first Lua state // Get count from the first available state
if r.states[0] != nil { for i := 0; i < len(r.states); i++ {
// Execute a Lua snippet to count modules if r.states[i] != nil && r.states[i].state != nil {
if res, err := r.states[0].ExecuteWithResult(` // Execute a Lua snippet to count modules
local count = 0 if res, err := r.states[i].state.ExecuteWithResult(`
for _ in pairs(package.loaded) do local count = 0
count = count + 1 for _ in pairs(package.loaded) do
end count = count + 1
return count end
`); err == nil { return count
if num, ok := res.(float64); ok { `); err == nil {
count = int(num) if num, ok := res.(float64); ok {
count = int(num)
}
} }
break
} }
} }

View File

@ -129,6 +129,7 @@ table tr:nth-child(even), tbody tr:nth-child(even) { background-color: rgba(0, 0
<tr><th>Active Routes</th><td>{{.Components.RouteCount}}</td></tr> <tr><th>Active Routes</th><td>{{.Components.RouteCount}}</td></tr>
<tr><th>Bytecode Size</th><td>{{ByteCount .Components.BytecodeBytes}}</td></tr> <tr><th>Bytecode Size</th><td>{{ByteCount .Components.BytecodeBytes}}</td></tr>
<tr><th>Loaded Modules</th><td>{{.Components.ModuleCount}}</td></tr> <tr><th>Loaded Modules</th><td>{{.Components.ModuleCount}}</td></tr>
<tr><th>State Pool Size</th><td>{{.Config.PoolSize}}</td></tr>
</table> </table>
</div> </div>
</div> </div>
@ -138,17 +139,27 @@ table tr:nth-child(even), tbody tr:nth-child(even) { background-color: rgba(0, 0
<div class="card"> <div class="card">
<table> <table>
<tr><th>Port</th><td>{{.Config.Port}}</td></tr> <tr><th>Port</th><td>{{.Config.Port}}</td></tr>
<tr><th>Pool Size</th><td>{{.Config.PoolSize}}</td></tr>
<tr><th>Debug Mode</th><td>{{.Config.Debug}}</td></tr> <tr><th>Debug Mode</th><td>{{.Config.Debug}}</td></tr>
<tr><th>Log Level</th><td>{{.Config.LogLevel}}</td></tr> <tr><th>Log Level</th><td>{{.Config.LogLevel}}</td></tr>
<tr><th>Routes Directory</th><td>{{.Config.RoutesDir}}</td></tr>
<tr><th>Static Directory</th><td>{{.Config.StaticDir}}</td></tr>
<tr><th>Override Directory</th><td>{{.Config.OverrideDir}}</td></tr>
<tr><th>Buffer Size</th><td>{{.Config.BufferSize}}</td></tr>
<tr><th>HTTP Logging</th><td>{{.Config.HTTPLoggingEnabled}}</td></tr> <tr><th>HTTP Logging</th><td>{{.Config.HTTPLoggingEnabled}}</td></tr>
<tr><th>Lib Directories</th><td>{{range .Config.LibDirs}}{{.}}<br>{{end}}</td></tr> <tr>
<tr><th>Watch Routes</th><td>{{index .Config.Watchers "routes"}}</td></tr> <th>Directories</th>
<tr><th>Watch Static</th><td>{{index .Config.Watchers "static"}}</td></tr> <td>
<tr><th>Watch Modules</th><td>{{index .Config.Watchers "modules"}}</td></tr> <div>Routes: {{.Config.RoutesDir}}</div>
<div>Static: {{.Config.StaticDir}}</div>
<div>Override: {{.Config.OverrideDir}}</div>
<div>Libs: {{range .Config.LibDirs}}{{.}}, {{end}}</div>
</td>
</tr>
<tr>
<th>Watchers</th>
<td>
<div>Routes: {{index .Config.Watchers "routes"}}</div>
<div>Static: {{index .Config.Watchers "static"}}</div>
<div>Modules: {{index .Config.Watchers "modules"}}</div>
</td>
</tr>
</table> </table>
</div> </div>
</div> </div>

2
luajit

@ -1 +1 @@
Subproject commit 13686b3e66b388a31d459fe95d1aa3bfa05aeb27 Subproject commit 0756cabcaaf1e33f2b8eb535e5a24e448c2501a9