feat(party): give every application its own plugin instances (quick-261118-lbt)
- Register keeps a pristine shallow copy of each pointer-to-struct plugin; every Activate hands its application a fresh copy, so a second application no longer rebinds the first one's plugins - value plugins and pointers to non-struct types are used as is - party.RegisterFactory for plugins whose init builds maps or pointers - activate() and its tests are unchanged - README and plugin/lifecycle docs describe the per-application rule
This commit is contained in:
@@ -2,6 +2,7 @@ package party
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -21,40 +22,112 @@ type Plugin interface {
|
||||
}
|
||||
|
||||
// Activated is the ordered plugin list published by Activate. Commands look
|
||||
// it up at Run time with app.Lookup[Activated]().
|
||||
// it up at Run time with app.Lookup[Activated](). The plugins in it are the
|
||||
// application's own instances (see Activate).
|
||||
type Activated []Plugin
|
||||
|
||||
// registration is one entry of the process-wide registry: a pristine copy of
|
||||
// a pointer-to-struct plugin, a plugin used as is, or a factory.
|
||||
type registration struct {
|
||||
plugin Plugin
|
||||
copyable bool
|
||||
id string
|
||||
factory func() Plugin
|
||||
}
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
plugins []Plugin
|
||||
plugins []registration
|
||||
)
|
||||
|
||||
// Register appends a plugin to the process-wide registry. Plugins call this
|
||||
// Register adds a plugin to the process-wide registry. Plugins call this
|
||||
// from init() after a blank import; Activate selects and orders them.
|
||||
//
|
||||
// A pointer to a struct (the usual &Plugin{}) is copied: Register keeps a
|
||||
// pristine shallow copy and every Activate hands its application a fresh
|
||||
// copy of it, so a plugin keeps per-application state on its struct, never
|
||||
// in package variables. The value passed here is never handed to an
|
||||
// application. Fields set in the literal carry over; a map or pointer set
|
||||
// there is shared by every application, so a plugin that needs its own maps
|
||||
// or pointers per application uses RegisterFactory. A value plugin or a
|
||||
// pointer to a non-struct type is used as is. A nil plugin is ignored.
|
||||
func Register(p Plugin) {
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
entry := registration{plugin: p}
|
||||
if v := reflect.ValueOf(p); v.Kind() == reflect.Pointer && !v.IsNil() && v.Elem().Kind() == reflect.Struct {
|
||||
pristine := reflect.New(v.Elem().Type())
|
||||
pristine.Elem().Set(v.Elem())
|
||||
if cp, ok := pristine.Interface().(Plugin); ok {
|
||||
entry = registration{plugin: cp, copyable: true}
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
plugins = append(plugins, p)
|
||||
plugins = append(plugins, entry)
|
||||
}
|
||||
|
||||
func snapshot() []Plugin {
|
||||
// RegisterFactory adds a plugin built by fn to the process-wide registry. fn
|
||||
// is called on every Activate, so each application gets the plugin fn
|
||||
// returns; use it for plugins whose init builds maps or pointers that must
|
||||
// not be shared between applications. The returned plugin's ID must be id;
|
||||
// a nil result or another ID fails Activate. A nil fn is ignored.
|
||||
func RegisterFactory(id string, fn func() Plugin) {
|
||||
if fn == nil {
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
out := make([]Plugin, len(plugins))
|
||||
copy(out, plugins)
|
||||
return out
|
||||
plugins = append(plugins, registration{id: id, factory: fn})
|
||||
}
|
||||
|
||||
// snapshot returns one fresh instance per registration, in registration
|
||||
// order: a new copy of each pristine pointer-to-struct plugin, the result of
|
||||
// each factory, and the as-is plugins.
|
||||
func snapshot() ([]Plugin, error) {
|
||||
mu.Lock()
|
||||
entries := make([]registration, len(plugins))
|
||||
copy(entries, plugins)
|
||||
mu.Unlock()
|
||||
|
||||
out := make([]Plugin, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
switch {
|
||||
case e.factory != nil:
|
||||
p := e.factory()
|
||||
if p == nil {
|
||||
return nil, fmt.Errorf("party: factory for %q returned nil", e.id)
|
||||
}
|
||||
if got := p.ID(); got != e.id {
|
||||
return nil, fmt.Errorf("party: factory for %q returned plugin %q", e.id, got)
|
||||
}
|
||||
out = append(out, p)
|
||||
case e.copyable:
|
||||
src := reflect.ValueOf(e.plugin)
|
||||
fresh := reflect.New(src.Elem().Type())
|
||||
fresh.Elem().Set(src.Elem())
|
||||
out = append(out, fresh.Interface().(Plugin))
|
||||
default:
|
||||
out = append(out, e.plugin)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Activate resolves ids in manifest order, topologically sorts Requires(),
|
||||
// then runs every Register before any Boot.
|
||||
// then runs every Register before any Boot. Each call works on fresh plugin
|
||||
// instances (see Register and RegisterFactory), so two applications
|
||||
// activated in one process, also concurrently, never share a plugin struct.
|
||||
func Activate(app *backpack.App, ids []string) ([]Plugin, error) {
|
||||
if app == nil {
|
||||
return nil, fmt.Errorf("party: app is nil")
|
||||
}
|
||||
return activate(snapshot(), app, ids)
|
||||
instances, err := snapshot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return activate(instances, app, ids)
|
||||
}
|
||||
|
||||
func activate(registered []Plugin, app *backpack.App, ids []string) ([]Plugin, error) {
|
||||
|
||||
Reference in New Issue
Block a user