Documentation
¶
Overview ¶
Package plugins provides the core plugin system for vcfg configuration management. It defines interfaces, types, and structures for plugin registration, lifecycle management, and configuration handling. The plugin system allows extending vcfg functionality through modular components.
Package plugins provides a comprehensive plugin management system that supports automatic discovery, registration, and lifecycle management of plugins. This file implements the PluginManager which handles plugin instances and their configurations.
Package plugins provides a global plugin registry system for managing plugin types and their corresponding configuration factories. This file implements the core registration mechanism that allows plugins to be discovered and instantiated dynamically.
Package plugins provides utility functions for plugin management and configuration handling. This file contains helper functions for plugin key generation, field path handling, configuration type detection, and reflection-based operations.
Index ¶
- func ListPluginTypes() []string
- func RegisterPluginType[P PluginPtr[PT], C ConfigPtr[CT], PT any, CT any](pluginType string, p P, c C, opts ...RegisterOptions)
- func ToPtr[T any](t T) *T
- func UnregisterPluginType(pluginType string)
- type BaseConfig
- type Config
- type ConfigPtr
- type Plugin
- type PluginEntry
- type PluginManager
- func (pm *PluginManager[T]) Clone() map[string]*PluginEntry
- func (pm *PluginManager[T]) DiscoverAndRegister(config *T) error
- func (pm *PluginManager[T]) Reload(ctx context.Context, oldConfig, newConfig *T) error
- func (pm *PluginManager[T]) Shutdown(ctx context.Context) error
- func (pm *PluginManager[T]) Startup(ctx context.Context) error
- type PluginPtr
- type RegisterOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ListPluginTypes ¶
func ListPluginTypes() []string
ListPluginTypes returns a list of all registered plugin type names
func RegisterPluginType ¶
func RegisterPluginType[P PluginPtr[PT], C ConfigPtr[CT], PT any, CT any](pluginType string, p P, c C, opts ...RegisterOptions)
RegisterPluginType registers a plugin type with its corresponding configuration type in the global registry. It uses Go generics to ensure type safety between plugins and their configurations.
Type parameters:
- P: Plugin pointer type that implements PluginPtr[PT]
- C: Config pointer type that implements ConfigPtr[CT]
- PT: The actual plugin type
- CT: The actual config type
Parameters:
- pluginType: The string identifier for the plugin type. If empty, it will be automatically derived from the config type name.
- p: A pointer to the plugin instance used as a prototype
- c: A pointer to the config instance used as a prototype
- opts: Optional registration options for customizing the registration behavior
The function panics if a plugin type is already registered to prevent conflicts.
func UnregisterPluginType ¶
func UnregisterPluginType(pluginType string)
UnregisterPluginType removes a plugin type from the registry
Types ¶
type BaseConfig ¶
type BaseConfig struct {
// Type identifies the plugin type for registration and instantiation
Type string `json:"type,omitempty" yaml:"type,omitempty" koanf:"type"`
}
BaseConfig provides the fundamental configuration structure that all plugin configurations must embed. It contains the plugin type identifier.
type Config ¶
type Config interface {
// contains filtered or unexported methods
}
Config defines the interface for plugin configuration structures. All plugin configurations must embed BaseConfig and implement this interface.
type ConfigPtr ¶
ConfigPtr is a generic constraint that ensures a type is both a Config and a pointer type. This is used for type-safe configuration registration.
type Plugin ¶
type Plugin interface {
// Startup initializes the plugin with the provided configuration.
// It should perform all necessary setup operations and return an error
// if initialization fails.
Startup(ctx context.Context, config any) error
// Reload updates the plugin configuration at runtime.
// It allows plugins to adapt to configuration changes without restart.
Reload(ctx context.Context, config any) error
// Shutdown gracefully terminates the plugin.
// It should clean up resources and perform necessary cleanup operations.
Shutdown(ctx context.Context) error
}
Plugin defines the core interface that all vcfg plugins must implement. It provides lifecycle management methods for plugin initialization, configuration reloading, and cleanup.
type PluginEntry ¶
type PluginEntry struct {
// Plugin is the actual plugin instance
Plugin Plugin
// Config is the configuration associated with this plugin instance
Config Config
// PluginType identifies the type of this plugin
PluginType string
// InstanceName is the unique name for this plugin instance
InstanceName string
// ConfigPath is the configuration path where this plugin's config is located
ConfigPath string
// contains filtered or unexported fields
}
PluginEntry represents a registered plugin instance with its configuration and runtime state. It tracks both the plugin and its associated metadata.
type PluginManager ¶
type PluginManager[T any] struct { // contains filtered or unexported fields }
PluginManager manages plugin instances and their lifecycle for a specific configuration type T. It provides thread-safe operations for plugin discovery, registration, initialization, and cleanup. The manager uses a map to store plugin entries with composite keys.
func NewPluginManager ¶
func NewPluginManager[T any]() *PluginManager[T]
NewPluginManager creates a new plugin manager instance for configuration type T. The manager is initialized with an empty plugin registry and is ready to discover and manage plugin instances.
func (*PluginManager[T]) Clone ¶
func (pm *PluginManager[T]) Clone() map[string]*PluginEntry
Clone returns information about all registered plugins in the global registry
func (*PluginManager[T]) DiscoverAndRegister ¶
func (pm *PluginManager[T]) DiscoverAndRegister(config *T) error
DiscoverAndRegister automatically discovers plugin configurations from the provided config struct and registers corresponding plugin instances. It uses reflection to traverse the config structure and creates plugin instances for fields that implement the Config interface.
func (*PluginManager[T]) Reload ¶
func (pm *PluginManager[T]) Reload(ctx context.Context, oldConfig, newConfig *T) error
Reload intelligently handles configuration changes by automatically detecting which plugins need to be reloaded based on their configuration changes. This method uses reflection to recursively iterate through configuration struct fields and automatically reloads plugins when their corresponding configuration implements the Config interface and has changed.
type PluginPtr ¶
PluginPtr is a generic constraint that ensures a type is both a Plugin and a pointer type. This is used for type-safe plugin registration.
type RegisterOptions ¶
type RegisterOptions struct {
// AutoDiscover enables automatic discovery and registration of this plugin type
AutoDiscover bool
}
RegisterOptions contains options for plugin type registration.