Documentation
¶
Overview ¶
Example ¶
Example demonstrates how to use JsExecutor with QuickJS engine. It shows how to initialize the executor, start it, execute a simple JS function, and stop the executor.
package main import ( "fmt" jsexecutor "github.com/buke/js-executor" quickjsengine "github.com/buke/js-executor/engines/quickjs-go" ) func main() { // Prepare a simple JS function as an initialization script jsScript := &jsexecutor.JsScript{ FileName: "hello.js", Content: `function hello(name) { return "Hello, " + name + "!"; }`, } // Create a QuickJS engine builder and a new executor with the jsScript executor, err := jsexecutor.NewExecutor( jsexecutor.WithJsEngine(quickjsengine.NewFactory( quickjsengine.WithEnableModuleImport(true), quickjsengine.WithCanBlock(true), )), jsexecutor.WithJsScripts(jsScript), ) if err != nil { fmt.Printf("Failed to create executor: %v\n", err) return } // Start the executor if err := executor.Start(); err != nil { fmt.Printf("Failed to start executor: %v\n", err) return } // Prepare a JS request to call the hello function req := &jsexecutor.JsRequest{ Id: "1", Service: "hello", Args: []interface{}{"World"}, } // Execute the JS request resp, err := executor.Execute(req) if err != nil { fmt.Printf("Execution error: %v\n", err) return } fmt.Printf("Result: %v\n", resp.Result) // Stop the executor if err := executor.Stop(); err != nil { fmt.Printf("Failed to stop executor: %v\n", err) return } }
Output: Result: Hello, World!
Index ¶
- Constants
- func WithCreateThreshold(threshold float64) func(*JsExecutor)
- func WithExecuteTimeout(timeout time.Duration) func(*JsExecutor)
- func WithJsEngine(engineFactory JsEngineFactory) func(*JsExecutor)
- func WithJsScripts(scripts ...*JsScript) func(*JsExecutor)
- func WithLogger(logger *slog.Logger) func(*JsExecutor)
- func WithMaxExecutions(max uint32) func(*JsExecutor)
- func WithMaxPoolSize(size uint32) func(*JsExecutor)
- func WithMinPoolSize(size uint32) func(*JsExecutor)
- func WithQueueSize(size uint32) func(*JsExecutor)
- func WithSelectThreshold(threshold float64) func(*JsExecutor)
- func WithThreadTTL(ttl time.Duration) func(*JsExecutor)
- type JsEngine
- type JsEngineFactory
- type JsEngineOption
- type JsExecutor
- func (e *JsExecutor) AppendJsScripts(scripts ...*JsScript)
- func (e *JsExecutor) Execute(request *JsRequest) (*JsResponse, error)
- func (e *JsExecutor) GetJsScripts() []*JsScript
- func (e *JsExecutor) Reload(scripts ...*JsScript) error
- func (e *JsExecutor) SetJsScripts(scripts []*JsScript)
- func (e *JsExecutor) Start() error
- func (e *JsExecutor) Stop() error
- type JsExecutorOption
- type JsRequest
- type JsResponse
- type JsScript
Examples ¶
Constants ¶
const ThreadIdKey = "__threadId"
ThreadIdKey is the context key for specifying thread ID in requests.
Variables ¶
This section is empty.
Functions ¶
func WithCreateThreshold ¶
func WithCreateThreshold(threshold float64) func(*JsExecutor)
WithCreateThreshold sets the queue load threshold for creating new threads.
func WithExecuteTimeout ¶
func WithExecuteTimeout(timeout time.Duration) func(*JsExecutor)
WithExecuteTimeout sets the timeout for task execution.
func WithJsEngine ¶
func WithJsEngine(engineFactory JsEngineFactory) func(*JsExecutor)
WithJsEngine configures the JavaScript engine builder and options.
func WithJsScripts ¶ added in v0.1.11
func WithJsScripts(scripts ...*JsScript) func(*JsExecutor)
WithJsScripts configures the initialization scripts.
func WithLogger ¶
func WithLogger(logger *slog.Logger) func(*JsExecutor)
WithLogger configures the logger for the executor.
func WithMaxExecutions ¶
func WithMaxExecutions(max uint32) func(*JsExecutor)
WithMaxExecutions sets the maximum executions per thread before cleanup.
func WithMaxPoolSize ¶
func WithMaxPoolSize(size uint32) func(*JsExecutor)
WithMaxPoolSize sets the maximum number of threads in the pool.
func WithMinPoolSize ¶
func WithMinPoolSize(size uint32) func(*JsExecutor)
WithMinPoolSize sets the minimum number of threads in the pool.
func WithQueueSize ¶
func WithQueueSize(size uint32) func(*JsExecutor)
WithQueueSize sets the size of the task queue per thread.
func WithSelectThreshold ¶
func WithSelectThreshold(threshold float64) func(*JsExecutor)
WithSelectThreshold sets the queue load threshold for skipping busy threads.
func WithThreadTTL ¶
func WithThreadTTL(ttl time.Duration) func(*JsExecutor)
WithThreadTTL sets the time-to-live for idle threads.
Types ¶
type JsEngine ¶
type JsEngine interface { // Load loads scripts into the engine Load(scripts []*JsScript) error // Execute executes a JavaScript request and returns the response Execute(req *JsRequest) (*JsResponse, error) // Close closes the engine and releases resources Close() error }
JsEngine represents a JavaScript execution engine
type JsEngineFactory ¶
JsEngineFactory defines a function type for creating JavaScript engines
type JsEngineOption ¶ added in v0.1.11
JsEngineOption represents a configuration option for a JavaScript engine
type JsExecutor ¶
type JsExecutor struct {
// contains filtered or unexported fields
}
JsExecutor manages a pool of JavaScript execution threads.
func NewExecutor ¶
func NewExecutor(opts ...func(*JsExecutor)) (*JsExecutor, error)
NewExecutor creates a new JavaScript executor with the given options.
func (*JsExecutor) AppendJsScripts ¶ added in v0.1.11
func (e *JsExecutor) AppendJsScripts(scripts ...*JsScript)
AppendJsScripts appends new scripts to the existing initialization scripts.
func (*JsExecutor) Execute ¶
func (e *JsExecutor) Execute(request *JsRequest) (*JsResponse, error)
Execute executes a JavaScript request and returns the response.
func (*JsExecutor) GetJsScripts ¶ added in v0.1.11
func (e *JsExecutor) GetJsScripts() []*JsScript
GetJsScripts returns the current initialization scripts (no copy, read-only).
func (*JsExecutor) Reload ¶
func (e *JsExecutor) Reload(scripts ...*JsScript) error
Reload reloads all threads with new initialization scripts.
func (*JsExecutor) SetJsScripts ¶ added in v0.1.11
func (e *JsExecutor) SetJsScripts(scripts []*JsScript)
SetJsScripts atomically sets new initialization scripts.
func (*JsExecutor) Start ¶
func (e *JsExecutor) Start() error
Start initializes and starts the executor thread pool.
func (*JsExecutor) Stop ¶
func (e *JsExecutor) Stop() error
Stop stops the executor and shuts down all threads.
type JsExecutorOption ¶
type JsExecutorOption struct {
// contains filtered or unexported fields
}
JsExecutorOption contains configuration options for the JavaScript executor.
type JsRequest ¶
type JsRequest struct { Id string `json:"id"` // Unique identifier for the request Service string `json:"service"` // Service/function name to call Args []interface{} `json:"args"` // Arguments to pass to the function Context map[string]interface{} `json:"context"` // Additional context data }
JsRequest represents a JavaScript execution request
type JsResponse ¶
type JsResponse struct { Id string `json:"id"` // Request ID that this response corresponds to Result interface{} `json:"result"` // Execution result Context map[string]interface{} `json:"context"` // Updated context data }
JsResponse represents the result of JavaScript execution