plugin

package
v0.0.0-...-78c727e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsCircuitOpenError

func IsCircuitOpenError(err error) bool

IsCircuitOpenError checks if the error is a circuit breaker open error

func IsFuncNotFoundError

func IsFuncNotFoundError(err error) bool

IsFuncNotFoundError checks if the error is a function not found error

func IsPluginNotFoundError

func IsPluginNotFoundError(err error) bool

IsPluginNotFoundError checks if the error is a plugin not found error

func IsPluginTimeoutError

func IsPluginTimeoutError(err error) bool

IsPluginTimeoutError checks if the error is a plugin timeout error

func ValidateConfig

func ValidateConfig(config *Config) error

ValidateConfig validates the configuration to ensure it is valid

Types

type Bureau

type Bureau interface {
	Name() string
	Version() string
	Init(...interface{}) error
	Free() error
}

Bureau defines the interface that all plugins must implement

type CircuitBreaker

type CircuitBreaker struct {
	// contains filtered or unexported fields
}

CircuitBreaker implements the circuit breaker pattern

func NewCircuitBreaker

func NewCircuitBreaker(ctx context.Context, config CircuitBreakerConfig, logger Logger) *CircuitBreaker

func (*CircuitBreaker) Allow

func (cb *CircuitBreaker) Allow() bool

func (*CircuitBreaker) Close

func (cb *CircuitBreaker) Close()

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() CircuitState

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	Enabled         bool
	MaxFailures     int
	ResetInterval   time.Duration
	TimeoutDuration time.Duration
}

CircuitBreakerConfig defines configuration for the circuit breaker

func DefaultCircuitBreakerConfig

func DefaultCircuitBreakerConfig() CircuitBreakerConfig

DefaultCircuitBreakerConfig returns the default circuit breaker configuration

type CircuitState

type CircuitState int32
const (
	StateClosed   CircuitState = 0
	StateOpen     CircuitState = 1
	StateHalfOpen CircuitState = 2
)

type Config

type Config struct {
	PluginDir           string
	AllowHotReload      bool
	LogLevel            LogLevel
	EnableMetrics       bool
	DefaultPluginConfig PluginSpecificConfig
	PluginConfigs       map[string]PluginSpecificConfig
}

Config defines the configuration for plugin manager

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default plugin manager configuration

func (*Config) Clone

func (c *Config) Clone() *Config

Clone creates a deep copy of the configuration

func (*Config) GetPluginConfig

func (c *Config) GetPluginConfig(pluginName string) PluginSpecificConfig

GetPluginConfig gets the plugin configuration, returning the default configuration if no specific configuration is provided

type DefaultLogger

type DefaultLogger struct {
	// contains filtered or unexported fields
}

DefaultLogger provides a basic implementation of the Logger interface

func NewDefaultLogger

func NewDefaultLogger(level LogLevel) *DefaultLogger

NewDefaultLogger creates a default logger implementation

func (*DefaultLogger) Debug

func (l *DefaultLogger) Debug(msg string, args ...interface{})

func (*DefaultLogger) Error

func (l *DefaultLogger) Error(msg string, args ...interface{})

func (*DefaultLogger) Info

func (l *DefaultLogger) Info(msg string, args ...interface{})

func (*DefaultLogger) Warn

func (l *DefaultLogger) Warn(msg string, args ...interface{})

type ErrCircuitBreakerOpen

type ErrCircuitBreakerOpen struct {
	Name string
}

ErrCircuitBreakerOpen represents a circuit breaker open error

func (*ErrCircuitBreakerOpen) Error

func (e *ErrCircuitBreakerOpen) Error() string

type ErrCircuitOpen

type ErrCircuitOpen struct {
	Name string
}

ErrCircuitOpen represents an error when the circuit breaker is open

func (ErrCircuitOpen) Error

func (e ErrCircuitOpen) Error() string

type ErrFuncNotFound

type ErrFuncNotFound struct {
	Name string
}

ErrFuncNotFound represents an error when a function cannot be found

func (ErrFuncNotFound) Error

func (e ErrFuncNotFound) Error() string

type ErrPluginExists

type ErrPluginExists struct {
	Name string
}

ErrPluginExists represents an error when a plugin already exists

func (ErrPluginExists) Error

func (e ErrPluginExists) Error() string

type ErrPluginFree

type ErrPluginFree struct {
	Name string
	Err  error
}

ErrPluginFree represents an error during plugin cleanup

func (ErrPluginFree) Error

func (e ErrPluginFree) Error() string

type ErrPluginInit

type ErrPluginInit struct {
	Name string
	Err  error
}

ErrPluginInit represents an error during plugin initialization

func (ErrPluginInit) Error

func (e ErrPluginInit) Error() string

type ErrPluginNotFound

type ErrPluginNotFound struct {
	Name string
}

ErrPluginNotFound represents an error when a plugin cannot be found

func (ErrPluginNotFound) Error

func (e ErrPluginNotFound) Error() string

type ErrPluginTimeout

type ErrPluginTimeout struct {
	Name string
}

ErrPluginTimeout represents an error when a plugin operation times out

func (ErrPluginTimeout) Error

func (e ErrPluginTimeout) Error() string

type InvokeFunc

type InvokeFunc func(ctx context.Context, args ...interface{}) (interface{}, error)

InvokeFunc represents a plugin function with a context as its first parameter

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader handles plugin loading and validation

func NewLoader

func NewLoader(manager *Manager) *Loader

NewLoader creates a new plugin loader

func (*Loader) Load

func (l *Loader) Load(ctx context.Context, path string) (*Plugin, error)

Load loads a plugin from the specified path

type LogLevel

type LogLevel int

LogLevel defines the severity level for logging

const (
	LogLevelDebug LogLevel = iota
	LogLevelInfo
	LogLevelWarn
	LogLevelError
)

type Logger

type Logger interface {
	Debug(msg string, args ...interface{})
	Info(msg string, args ...interface{})
	Warn(msg string, args ...interface{})
	Error(msg string, args ...interface{})
}

Logger defines the interface for plugin logging

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles plugin lifecycle and operations

func NewManager

func NewManager(ctx context.Context, config *Config, opts ...ManagerOption) (*Manager, error)

NewManager creates a new plugin manager

func (*Manager) Call

func (m *Manager) Call(ctx context.Context, pluginName, funcName string, args ...interface{}) (interface{}, error)

Call invokes a plugin function with the given arguments

func (*Manager) Close

func (m *Manager) Close() error

Close gracefully shuts down the manager and all plugins

func (*Manager) DisableMetrics

func (m *Manager) DisableMetrics()

DisableMetrics disables metrics collection

func (*Manager) EnableMetrics

func (m *Manager) EnableMetrics()

EnableMetrics enables metrics collection

func (*Manager) GetBreakerStatus

func (m *Manager) GetBreakerStatus(pluginName string) bool

func (*Manager) GetMetrics

func (m *Manager) GetMetrics(pluginName string) (*PluginMethodMetrics, error)

GetMetrics returns metrics for a specific plugin

func (*Manager) GetPluginFunctions

func (m *Manager) GetPluginFunctions(pluginName string) ([]string, error)

GetPluginFunctions returns a list of available functions for a plugin

func (*Manager) GetPluginPath

func (m *Manager) GetPluginPath(name string) (string, bool)

GetPluginPath returns the path of a loaded plugin

func (*Manager) IsCircuitBreakerOpen

func (m *Manager) IsCircuitBreakerOpen(pluginName string) bool

IsCircuitBreakerOpen checks if the circuit breaker is open for a plugin

func (*Manager) IsMetricsEnabled

func (m *Manager) IsMetricsEnabled() bool

IsMetricsEnabled returns whether metrics collection is enabled

func (*Manager) ListPlugins

func (m *Manager) ListPlugins() []PluginInfo

ListPlugins returns a list of all loaded plugins

func (*Manager) LoadPlugin

func (m *Manager) LoadPlugin(path string) error

LoadPlugin loads a plugin from the specified path

func (*Manager) LoadPluginWithConfig

func (m *Manager) LoadPluginWithConfig(path string, config *PluginSpecificConfig) error

LoadPluginWithConfig loads a plugin with specific configuration

func (*Manager) ResetMetrics

func (m *Manager) ResetMetrics()

ResetMetrics resets all metrics

type ManagerOption

type ManagerOption func(*Manager)

ManagerOption defines a function type for configuring Manager

func WithLogger

func WithLogger(logger Logger) ManagerOption

WithLogger sets an external logger implementation

type MethodMetrics

type MethodMetrics struct {
	Count     atomic.Int64
	TotalTime atomic.Int64 // save nanoseconds
	MinTime   atomic.Int64 // save nanoseconds
	MaxTime   atomic.Int64 // save nanoseconds
}

MethodMetrics stores metrics for a single method using atomic operations

type Plugin

type Plugin struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Plugin wraps a plugin instance

func NewPlugin

func NewPlugin(b Bureau) *Plugin

func (*Plugin) AddRef

func (p *Plugin) AddRef()

AddRef increases the reference count

func (*Plugin) Call

func (p *Plugin) Call(ctx context.Context, name string, args ...interface{}) (interface{}, error)

Call calls the plugin function

func (*Plugin) DecRef

func (p *Plugin) DecRef() bool

DecRef decreases the reference count and returns whether it is 0

func (*Plugin) Free

func (p *Plugin) Free() error

func (*Plugin) GetFunctions

func (p *Plugin) GetFunctions() []string

GetFunctions returns a list of available functions

func (*Plugin) GetRefs

func (p *Plugin) GetRefs() int32

GetRefs gets the current reference count

func (*Plugin) Init

func (p *Plugin) Init(args ...interface{}) error

func (*Plugin) Name

func (p *Plugin) Name() string

func (*Plugin) RegisterFunc

func (p *Plugin) RegisterFunc(name string, fn InvokeFunc)

func (*Plugin) Version

func (p *Plugin) Version() string

type PluginInfo

type PluginInfo struct {
	Name     string
	Version  string
	State    PluginState
	RefCount int32
	Path     string
}

PluginInfo contains basic information about a loaded plugin

type PluginInstance

type PluginInstance struct {
	*Plugin
	// contains filtered or unexported fields
}

PluginInstance wraps a plugin with additional metadata

func (*PluginInstance) GetFunctions

func (pi *PluginInstance) GetFunctions() []string

GetFunctions returns a list of available functions

type PluginMethodMetrics

type PluginMethodMetrics struct {
	Methods sync.Map // map[string]*MethodMetrics
}

PluginMethodMetrics stores metrics for plugin methods

type PluginMetrics

type PluginMetrics struct {
	// contains filtered or unexported fields
}

PluginMetrics stores metrics for plugin calls

func NewPluginMetrics

func NewPluginMetrics(enabled bool) *PluginMetrics

NewPluginMetrics creates a new plugin metrics collector

func (*PluginMetrics) AddPlugin

func (m *PluginMetrics) AddPlugin(pluginName string)

AddPlugin adds a new plugin metrics record

func (*PluginMetrics) GetPluginMetrics

func (m *PluginMetrics) GetPluginMetrics(pluginName string) (*PluginMethodMetrics, error)

GetPluginMetrics returns metrics for a specific plugin

func (*PluginMetrics) IsEnabled

func (m *PluginMetrics) IsEnabled() bool

IsEnabled returns the enabled state

func (*PluginMetrics) RecordMetric

func (m *PluginMetrics) RecordMetric(pluginName, funcName string, duration time.Duration)

RecordMetric records a single method call

func (*PluginMetrics) SetEnabled

func (m *PluginMetrics) SetEnabled(enabled bool)

SetEnabled sets the enabled state

type PluginSpecificConfig

type PluginSpecificConfig struct {
	InitArgs           []interface{}
	CircuitBreaker     CircuitBreakerConfig
	MaxConcurrentCalls int
	PluginTimeout      time.Duration
	Options            map[string]interface{}
}

PluginSpecificConfig defines configuration for a specific plugin

func DefaultPluginSpecificConfig

func DefaultPluginSpecificConfig() PluginSpecificConfig

DefaultPluginSpecificConfig returns the default plugin specific configuration

type PluginState

type PluginState int

PluginState represents the state of a plugin

const (
	StateActive PluginState = iota
	StateDeprecated
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL