Documentation
¶
Overview ¶
Package pluggo provides a framework for creating and communicating with plugin systems using HTTP-based communication. It allows for dynamic loading and execution of plugins as separate processes that communicate over HTTP.
The package consists of two main components: - Client: for launching and communicating with plugins - Plugin: for creating plugins that can be launched by the client
Plugins are executable files that start an HTTP server and communicate their port number to the client via stdout. The client then uses HTTP requests to execute functions within the plugin.
Index ¶
- Constants
- type Client
- type ClientOption
- type Connection
- type Function
- type FunctionExecutionError
- type FunctionHandler
- type FunctionListError
- type FunctionLookupError
- type FunctionNotFoundError
- type Handler
- type Plugin
- type PluginExecutionError
- type PluginNotFoundError
- type Schema
- type Schemas
- type Validator
Constants ¶
const ( // DefaultFunctionExecutionTimeout is the HTTP timeout for requests the launcher makes to the plugin (health + exec) DefaultFunctionExecutionTimeout = 2 * time.Minute // DefaultHealthCheckTimeout is the total time the launcher will wait for the plugin to become healthy DefaultHealthCheckTimeout = 5 * time.Second // DefaultHealthCheckInterval defines how often to retry hitting /_healthz while waiting DefaultHealthCheckInterval = 150 * time.Millisecond )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client manages the lifecycle and communication with a plugin process. It handles launching the plugin executable, establishing HTTP communication, health checking, and graceful shutdown.
func New ¶
func New(path string, opts ...ClientOption) *Client
New creates a new Client instance with the specified plugin path and optional configuration. The path should point to an executable file that implements the plugin protocol. Options can be provided to customize timeouts and other behavior.
func (*Client) Close ¶
Close gracefully shuts down the plugin process and cleans up resources. It cancels the plugin's context and kills the process if it's still running. This method is safe to call multiple times.
func (*Client) Connection ¶
func (c *Client) Connection() *Connection
Connection returns the current HTTP connection details for the plugin. Returns nil if the plugin is not currently running or connected.
func (*Client) HealthCheck ¶
func (c *Client) HealthCheck() <-chan struct{}
create a function that return a channel to the user and start a goroutine that periodically check the health endpoint of the plugin. If the plugin becomes unhealthy it closes the channel to notify the user.
func (*Client) Open ¶
Open launches the plugin process and establishes communication. It performs the following steps: 1. Validates that the plugin file exists and is executable 2. Starts the plugin process 3. Reads the HTTP port from the plugin's stdout 4. Establishes HTTP connection and waits for the plugin to become healthy
Returns an error if any step fails. The plugin process will be terminated automatically if initialization fails.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption is a function that configures a Client during creation.
func WithFunctionExecutionTimeout ¶
func WithFunctionExecutionTimeout(timeout time.Duration) ClientOption
WithFunctionExecutionTimeout sets the HTTP request timeout for function execution calls.
func WithHealthCheckInterval ¶
func WithHealthCheckInterval(interval time.Duration) ClientOption
WithHealthCheckInterval sets the interval between health check attempts during plugin startup.
func WithHealthCheckTimeout ¶
func WithHealthCheckTimeout(timeout time.Duration) ClientOption
WithHealthCheckTimeout sets the total timeout duration for waiting for the plugin to become healthy.
func WithHeartbeatInterval ¶
func WithHeartbeatInterval(interval time.Duration) ClientOption
WithHeartbeatInterval sets the interval between heartbeat checks for the plugin.
type Connection ¶
Connection represents an active HTTP connection to a plugin server. It contains the base URL and configuration for communication with the plugin.
type Function ¶
type Function[T, R any] struct { // contains filtered or unexported fields }
Function represents a typed function that can be called on a remote plugin. T is the input type and R is the output type for the function. It handles JSON serialization/deserialization and HTTP communication automatically.
func NewFunction ¶
func NewFunction[T, R any](name string, clientConnection *Connection) *Function[T, R]
NewFunction creates a new typed function client for calling a specific function on a plugin. The function will serialize input of type T to JSON, send it to the plugin, and deserialize the response into type R.
func (*Function[T, R]) Call ¶
Call executes the function with the provided input and returns the result. The input is serialized to JSON, sent to the plugin via HTTP POST, and the response is deserialized back to the expected output type.
func (*Function[T, R]) Schema ¶
Schema retrieves the JSON schema definition for this function's input and output types. This provides introspection capabilities to understand the expected data structure.
func (*Function[T, R]) SetTimeout ¶
SetTimeout configures the HTTP timeout for this specific function. This overrides the default timeout set in the connection.
type FunctionExecutionError ¶
FunctionExecutionError is returned when there's an error executing a function within a plugin.
func (*FunctionExecutionError) Error ¶
func (e *FunctionExecutionError) Error() string
Error implements the error interface for FunctionExecutionError.
type FunctionHandler ¶
type FunctionHandler[T, R any] struct { // contains filtered or unexported fields }
FunctionHandler wraps a user-provided function with HTTP handling capabilities. It provides automatic JSON serialization/deserialization, input validation, and schema generation for plugin functions.
func NewFunctionHandler ¶
func NewFunctionHandler[T, R any](fn func(context.Context, *T) (*R, error), validator *Validator[T]) *FunctionHandler[T, R]
NewFunctionHandler creates a new function handler that wraps a user function with HTTP request/response handling, JSON processing, and optional input validation. The handler automatically generates JSON schemas for input and output types.
func (*FunctionHandler[T, R]) Handler ¶
func (m *FunctionHandler[T, R]) Handler() *Handler
Handler returns the underlying HTTP handler and schema information. This is used internally by the plugin framework to register the function.
type FunctionListError ¶
type FunctionListError struct {
Err error
}
FunctionListError is returned when there's an error retrieving the list of available functions from a plugin.
func (*FunctionListError) Error ¶
func (e *FunctionListError) Error() string
Error implements the error interface for FunctionListError.
type FunctionLookupError ¶
FunctionLookupError is returned when there's an error looking up or accessing a specific function.
func (*FunctionLookupError) Error ¶
func (e *FunctionLookupError) Error() string
Error implements the error interface for FunctionLookupError.
type FunctionNotFoundError ¶
type FunctionNotFoundError struct {
Function string
}
FunctionNotFoundError is returned when attempting to call a function that doesn't exist in the plugin.
func (*FunctionNotFoundError) Error ¶
func (e *FunctionNotFoundError) Error() string
Error implements the error interface for FunctionNotFoundError.
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin represents a plugin server that can host multiple functions. It manages the HTTP server, function registration, and provides health check and schema introspection endpoints.
func NewPlugin ¶
func NewPlugin() *Plugin
NewPlugin creates a new plugin instance with default configuration. It sets up the HTTP server, logging, health check endpoint, and schema endpoint.
func (*Plugin) AddFunction ¶
AddFunction registers a new function with the plugin server. The function becomes available at the endpoint /{functionName} and its schema at /{functionName}/_schemas. Function names are validated to ensure they contain only safe characters.
type PluginExecutionError ¶
type PluginExecutionError struct {
Err error
}
PluginExecutionError is returned when there's an error starting, running, or communicating with a plugin.
func (*PluginExecutionError) Error ¶
func (e *PluginExecutionError) Error() string
Error implements the error interface for PluginExecutionError.
type PluginNotFoundError ¶
type PluginNotFoundError struct {
Err error
}
PluginNotFoundError is returned when the specified plugin file cannot be found or accessed.
func (*PluginNotFoundError) Error ¶
func (e *PluginNotFoundError) Error() string
Error implements the error interface for PluginNotFoundError.
type Schema ¶
Schema represents the input and output JSON schemas for a plugin function. This provides introspection capabilities for clients to understand the expected data structures.
type Validator ¶
type Validator[T any] struct { // contains filtered or unexported fields }
Validator provides JSON schema validation for input data. It uses a compiled JSON schema to validate incoming data against the expected structure before deserialization.
func NewValidator ¶
NewValidator creates a new validator for type T by generating a JSON schema from the provided struct. The validator can then be used to validate JSON input before deserialization.
func (*Validator[T]) Validate ¶
func (v *Validator[T]) Validate(data any) *jsonschema.EvaluationResult
Validate checks the provided data against the compiled JSON schema. It returns an evaluation result that contains validation status and any errors found during validation.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
basic/plugin
command
|
|
|
health
command
|
|
|
health/plugin
command
|
|
|
scan
command
|
|
|
scan/plugins/reverse
command
|
|
|
scan/plugins/uppercase
command
|