From 12e059bcc56e4db885a0e95ff648b65fa04239f4 Mon Sep 17 00:00:00 2001 From: drainpilled Date: Thu, 17 Oct 2024 16:20:42 -0400 Subject: [PATCH] tmp with plugins --- go.mod | 5 +- go.sum | 17 ++++--- internal/config/config.go | 47 ++++++++----------- .../config/static_provider/static_provider.go | 12 +++-- internal/plugin/domain/common.go | 11 +++++ internal/plugin/loader.go | 46 ++++++++++++++++++ internal/plugin/plugin.go | 12 +++++ internal/plugin/store.go | 22 +++++++++ 8 files changed, 130 insertions(+), 42 deletions(-) create mode 100644 internal/plugin/domain/common.go create mode 100644 internal/plugin/loader.go create mode 100644 internal/plugin/plugin.go create mode 100644 internal/plugin/store.go diff --git a/go.mod b/go.mod index 0a3394d..69a8cd7 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,11 @@ go 1.23.0 require ( github.com/fatih/color v1.17.0 + github.com/fsnotify/fsnotify v1.7.0 github.com/gin-gonic/gin v1.10.0 github.com/jackc/pgx/v5 v5.7.1 github.com/jmoiron/sqlx v1.4.0 + github.com/spf13/viper v1.19.0 golang.org/x/crypto v0.28.0 ) @@ -15,7 +17,6 @@ require ( github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect - github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -27,7 +28,6 @@ require ( github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect - github.com/kr/text v0.2.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -43,7 +43,6 @@ require ( github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/spf13/viper v1.19.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect diff --git a/go.sum b/go.sum index 5d2cdbf..61aa5a6 100644 --- a/go.sum +++ b/go.sum @@ -8,13 +8,14 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/ github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= @@ -35,8 +36,8 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= @@ -56,8 +57,8 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02 github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= -github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= @@ -82,9 +83,9 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= @@ -141,8 +142,6 @@ golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/config/config.go b/internal/config/config.go index bf8bad0..295be6f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,24 +20,35 @@ type Value interface { type DurationValue time.Duration -type FloatValue float32 +type FloatValue struct { + EmptyValue + val float32 +} -type StringValue string +func (v FloatValue) Float() float32 { + return v.val +} -func (v StringValue) Int() int { - return 0 +type StringValue struct { + EmptyValue + val string } func (v StringValue) String() string { - return string(v) + return v.val } -func (v StringValue) Float() float32 { - return 0 +type IntValue struct { + EmptyValue + val int } -func (v StringValue) Duration() time.Duration { - return 0 +func (v IntValue) Int() int { + return v.val +} + +func (v IntValue) Float() float32 { + return float32(v.val) } type EmptyValue struct{} @@ -57,21 +68,3 @@ func (v EmptyValue) Float() float32 { func (v EmptyValue) Duration() time.Duration { return 0 } - -type IntValue int - -func (v IntValue) Int() int { - return int(v) -} - -func (v IntValue) String() string { - return "" -} - -func (v IntValue) Float() float32 { - return float32(v) -} - -func (v IntValue) Duration() time.Duration { - return 0 -} diff --git a/internal/config/static_provider/static_provider.go b/internal/config/static_provider/static_provider.go index 31a2bb7..a7cfcc1 100644 --- a/internal/config/static_provider/static_provider.go +++ b/internal/config/static_provider/static_provider.go @@ -33,11 +33,17 @@ func (p *staticProvider) GetValue(ctx context.Context, key config.Key) config.Va switch val := rawVal.(type) { case int: - return config.IntValue(val) + return config.IntValue{ + Val: val, + } case string: - return config.StringValue(val) + return config.StringValue{ + Val: val, + } case float32: - return config.FloatValue(val) + return config.FloatValue{ + Val: val, + } default: return config.EmptyValue{} } diff --git a/internal/plugin/domain/common.go b/internal/plugin/domain/common.go new file mode 100644 index 0000000..492b871 --- /dev/null +++ b/internal/plugin/domain/common.go @@ -0,0 +1,11 @@ +package domain + +type InitPluginRequest struct { + Name string `json:"name"` + Version int `json:"version"` + Namespace string `json:"namespace"` + RequiredResolveParams []string `json:"required_resolve_params"` + OptionalResolveParams []string `json:"optional_resolve_params"` + WithActions bool `json:"with_actions"` + Async bool `json:"async"` +} diff --git a/internal/plugin/loader.go b/internal/plugin/loader.go new file mode 100644 index 0000000..b161a6a --- /dev/null +++ b/internal/plugin/loader.go @@ -0,0 +1,46 @@ +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 +} diff --git a/internal/plugin/plugin.go b/internal/plugin/plugin.go new file mode 100644 index 0000000..0ef5bc4 --- /dev/null +++ b/internal/plugin/plugin.go @@ -0,0 +1,12 @@ +package plugin + +import ( + "net" + + "git.optclblast.xyz/draincloud/draincloud-core/internal/plugin/domain" +) + +type Plugin struct { + conn net.Conn + md domain.InitPluginRequest +} diff --git a/internal/plugin/store.go b/internal/plugin/store.go new file mode 100644 index 0000000..ab8ae2e --- /dev/null +++ b/internal/plugin/store.go @@ -0,0 +1,22 @@ +package plugin + +import ( + "fmt" + "sync" +) + +type PluginStore struct { + m sync.RWMutex + plugins map[string]*Plugin +} + +func (s *PluginStore) Add(plugin *Plugin) { + s.m.Lock() + defer s.m.Unlock() + + s.plugins[PluginStoreKey(plugin.md.Namespace, plugin.md.Name, plugin.md.Version)] = plugin +} + +func PluginStoreKey(ns, name string, v int) string { + return fmt.Sprintf("%s.%s.%v", ns, name, v) +}