draincloud-core/internal/plugin/loader.go
2024-10-17 16:20:42 -04:00

47 lines
881 B
Go

package plugin
import (
"context"
"fmt"
"net"
"git.optclblast.xyz/draincloud/draincloud-core/internal/logger"
)
type PluginLoader struct {
l net.Listener
}
func (p *PluginLoader) Run(ctx context.Context) {
go p.run(ctx)
}
func (p *PluginLoader) run(ctx context.Context) {
for {
select {
case <-ctx.Done():
logger.Info(ctx, "[plugin_loader][loop] closing")
if err := p.l.Close(); err != nil {
logger.Error(ctx, "[plugin_loader][loop] failed to close listener", logger.Err(err))
}
default:
plugin, err := p.accept()
if err != nil {
logger.Error(ctx, "[plugin_loader][loop] accept error", logger.Err(err))
continue
}
}
}
}
func (p *PluginLoader) accept() (*Plugin, error) {
conn, err := p.l.Accept()
if err != nil {
return nil, fmt.Errorf("failed to accept new connection: %w", err)
}
return &Plugin{
conn: conn,
}, nil
}