refactor: simplify WaitGroup

refactor: simplify WaitGroup
This commit is contained in:
cubexteam
2026-03-04 18:33:49 +03:00
committed by GitHub
parent 8e2d4f7b8c
commit 0d1baa1676

View File

@@ -11,31 +11,31 @@ import (
) )
var ( var (
loaded atomic.Bool initialized atomic.Bool
plugins []*Plugin plugins []*Plugin
) )
func Add(impl Impl) { func Add(impl Impl) {
if loaded.Load() { if initialized.Load() {
panic("cannot add plugin after server has started") panic("cannot add plugin after server has started")
} }
plugins = append(plugins, &Plugin{impl: impl}) plugins = append(plugins, &Plugin{impl: impl})
} }
func Initialize(log Logger, folder string) (func(context.Context) *sync.WaitGroup, error) { func Initialize(log Logger, folder string) (func(context.Context) *sync.WaitGroup, error) {
if !loaded.CompareAndSwap(false, true) { if !initialized.CompareAndSwap(false, true) {
panic("plugins already initialized") panic("plugins already initialized")
} }
log.Infof("Loading %d plugin(s)...", len(plugins)) log.Infof("Loading %d plugin(s)...", len(plugins))
names := map[string]struct{}{} seen := make(map[string]struct{}, len(plugins))
for _, p := range plugins { for _, p := range plugins {
key := strings.ToLower(p.impl.Name()) key := strings.ToLower(p.impl.Name())
if _, ok := names[key]; ok { if _, dup := seen[key]; dup {
return nil, fmt.Errorf("duplicate plugin name: %s", p.impl.Name()) return nil, fmt.Errorf("duplicate plugin name: %s", p.impl.Name())
} }
names[key] = struct{}{} seen[key] = struct{}{}
} }
wd, err := os.Getwd() wd, err := os.Getwd()
@@ -47,20 +47,20 @@ func Initialize(log Logger, folder string) (func(context.Context) *sync.WaitGrou
p.logger = log p.logger = log
p.directory = filepath.Join(wd, folder, p.impl.Name()) p.directory = filepath.Join(wd, folder, p.impl.Name())
if err := p.impl.Setup(p); err != nil { if err := p.impl.Setup(p); err != nil {
return nil, fmt.Errorf("plugin '%s' setup failed: %w", p.impl.Name(), err) return nil, fmt.Errorf("plugin %q setup failed: %w", p.impl.Name(), err)
} }
log.Infof("Plugin '%s' loaded.", p.impl.Name()) log.Infof("Plugin %q loaded.", p.impl.Name())
} }
return func(ctx context.Context) *sync.WaitGroup { return func(ctx context.Context) *sync.WaitGroup {
wg := &sync.WaitGroup{} var wg sync.WaitGroup
wg.Add(len(plugins))
for _, p := range plugins { for _, p := range plugins {
wg.Add(1)
go func(p *Plugin) { go func(p *Plugin) {
defer wg.Done() defer wg.Done()
p.impl.Run(ctx, p) p.impl.Run(ctx, p)
}(p) }(p)
} }
return wg return &wg
}, nil }, nil
} }