Documentation
¶
Index ¶
- Constants
- Variables
- type AsyncData
- type Entity
- type Envelope
- type Function
- type Options
- type Plugin
- type PluginInfo
- type Socket
- func (s *Socket) Async() (*AsyncData, error)
- func (s *Socket) Broadcast(id, method string, payload any) (map[string]any, map[string]error)
- func (s *Socket) Call(id, name, method string, payload any) (any, error)
- func (s *Socket) Connected(full bool) map[string]*PluginInfo
- func (s *Socket) PluginInfo(name string) *PluginInfo
- func (s *Socket) RegisterCallback(callback func(info *PluginInfo, graceful bool))
- func (s *Socket) Serve(listener net.Listener, opts ...Options)
- func (s *Socket) Shutdown(id string) error
- func (s *Socket) Unplug(id string, name string)
- func (s *Socket) WaitFor(name string, timeout time.Duration) bool
Constants ¶
const ( MethodAsync = "Async" MethodShutdown = "Shutdown" )
Variables ¶
var ( ErrShutdown = errors.New("connection is shut down") ErrMethodNotFound = errors.New("method not found") )
Functions ¶
This section is empty.
Types ¶
type AsyncData ¶
type AsyncData struct {
Name string `json:"name,omitempty"`
Payload any `json:"payload,omitempty"`
}
AsyncData represents data received asynchronously from a plugin.
type Entity ¶ added in v0.0.10
type Entity struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Mandatory bool `json:"mandatory,omitempty"`
Fields []Entity `json:"fields,omitempty"`
}
func TypeDescription ¶ added in v0.0.10
type Envelope ¶
Envelope represents a message structure used as an RPC call/return. It is used internally.
type Function ¶ added in v0.0.10
type Options ¶ added in v0.0.12
type Options interface {
// contains filtered or unexported methods
}
func WithKeySequencer ¶ added in v0.0.12
WithKeySequencer returns an option that applies a function to modify repeated plugin names, enabling multiple connections with the same base name by generating unique variations.
The `fn` parameter is used to transform the name when a conflict is detected, and `attempts` defines the maximum number of times the function will be applied before failing to register the plugin.
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin represents a plugin instance that handles communication via a connection. It manages a map of registered methods and uses reflection to invoke them based on incoming requests.
func (*Plugin) Start ¶
func (p *Plugin) Start(v any, info *PluginInfo, conn io.ReadWriteCloser, ctxKey any) error
Start registers the provided plugin receiver uses reflection to inspect the receiver's methods and ensure that they follow the expected signature. It checks for methods that look schematically like:
func (t *T) MethodName(ctx context.Context, in T1) (out T2, err error)
and / or
func (t *T) UseAsyncHook(hook chan any)
where T1 and T2 are valid exported (or builtin) types. T1 must not be a channel, function or non-empty interface type. Similarly, T2 must not be a channel or function type.
Methods that do not match the required signatures are ignored.
If the plugin has at least one valid method it establishes a connection, sends a handshake containing plugin information to the socket and enters the listener for incoming requests.
If no valid methods are found, it returns an error.
If the plugin uses the async hook, a goroutine is started to handle hook-related values.
type PluginInfo ¶
type PluginInfo struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
Functions map[string]Function `json:"functions,omitempty"`
}
PluginInfo holds metadata about a plugin.
func (*PluginInfo) DeepCopy ¶ added in v0.0.11
func (p *PluginInfo) DeepCopy(full bool) *PluginInfo
DeepCopy creates a copy of the PluginInfo instance. If `full` is true, it performs a deep copy of all Functions; otherwise, only Name and Version are copied.
type Socket ¶
type Socket struct {
// contains filtered or unexported fields
}
Socket represents a server that manages plugin connections and communications.
func (*Socket) Async ¶
Async retrieves the next message from the async channel. It returns connection closed error when socket is stopped.
func (*Socket) Broadcast ¶
Broadcast invokes the named method with the provided payload on all running plugins, waits for them to complete, and returns their return and error status.
func (*Socket) Call ¶
Call invokes the named method of the specified plugin with the provided payload, waits for it to complete, and returns its return and error status.
func (*Socket) Connected ¶
func (s *Socket) Connected(full bool) map[string]*PluginInfo
Connected returns a list of connected plugins. If `full` is true, all Functions are included; otherwise, only Name and Version are returned.
func (*Socket) PluginInfo ¶ added in v0.0.11
func (s *Socket) PluginInfo(name string) *PluginInfo
PluginInfo returns a full plugin info based on its name.
func (*Socket) RegisterCallback ¶ added in v0.0.14
func (s *Socket) RegisterCallback(callback func(info *PluginInfo, graceful bool))
RegisterCallback registers a callback function that will be called whenever a plugin is disconnected.
callback: a function that receives plugin information and a graceful flag.
- info: details about the disconnected plugin;
- graceful: indicates whether the plugin was disconnected normally (true) via a stop request or unexpectedly (false) due to a crash or termination.
func (*Socket) Shutdown ¶
Shutdown gracefully stops all running plugins and stops the Socket. An optional `id` can be provided to trace the request.
func (*Socket) Unplug ¶
Unplug sends a stop request to a plugin based on its name. An optional `id` can be provided to trace the request.