From 3c56a0eeba3fc9d8175e8df017402383c769161f Mon Sep 17 00:00:00 2001 From: tom-fitz <16616192+tom-fitz@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:49:09 -0600 Subject: [PATCH 1/7] feat(#10): collecting and sending failed links back to cloud provider via server) --- examples/keyvalue-inmemory/go.mod | 2 +- examples/keyvalue-inmemory/keyvalue.go | 6 ++-- examples/keyvalue-inmemory/link.go | 44 ++++++++++++++++++++++++++ examples/keyvalue-inmemory/main.go | 18 +++++++++-- 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 examples/keyvalue-inmemory/link.go diff --git a/examples/keyvalue-inmemory/go.mod b/examples/keyvalue-inmemory/go.mod index bdb6375..6c8479f 100644 --- a/examples/keyvalue-inmemory/go.mod +++ b/examples/keyvalue-inmemory/go.mod @@ -1,6 +1,6 @@ module github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory -go 1.22.3 +go 1.20 require ( github.com/wasmCloud/provider-sdk-go v0.0.0-20240124183610-1a92f8d04935 diff --git a/examples/keyvalue-inmemory/keyvalue.go b/examples/keyvalue-inmemory/keyvalue.go index ac38890..9298070 100644 --- a/examples/keyvalue-inmemory/keyvalue.go +++ b/examples/keyvalue-inmemory/keyvalue.go @@ -17,8 +17,10 @@ var ( type Provider struct { sync.Map - sourceLinks map[string]provider.InterfaceLinkDefinition - targetLinks map[string]provider.InterfaceLinkDefinition + sourceLinks map[string]provider.InterfaceLinkDefinition + targetLinks map[string]provider.InterfaceLinkDefinition + failedSourceLinks map[string]provider.InterfaceLinkDefinition + failedTargetLinks map[string]provider.InterfaceLinkDefinition } func Ok[T any](v T) *wrpc.Result[T, store.Error] { diff --git a/examples/keyvalue-inmemory/link.go b/examples/keyvalue-inmemory/link.go new file mode 100644 index 0000000..d3dff27 --- /dev/null +++ b/examples/keyvalue-inmemory/link.go @@ -0,0 +1,44 @@ +package main + +import ( + "github.com/wasmCloud/provider-sdk-go" + "log" +) + +func (p *Provider) establishSourceLink(link provider.InterfaceLinkDefinition) error { + if _, exists := p.sourceLinks[link.Target]; exists { + log.Println("Source link already exists, ignoring duplicate", link) + return nil + } + + if err := p.validateSourceLink(link); err != nil { + return err + } + + p.sourceLinks[link.Target] = link + return nil +} + +func (p *Provider) establishTargetLink(link provider.InterfaceLinkDefinition) error { + if _, exists := p.targetLinks[link.SourceID]; exists { + log.Println("Target link already exists, ignoring duplicate", link) + return nil + } + + if err := p.validateTargetLink(link); err != nil { + return err + } + + p.targetLinks[link.SourceID] = link + return nil +} + +func (p *Provider) validateSourceLink(link provider.InterfaceLinkDefinition) error { + // TODO: Add validation checks + return nil +} + +func (p *Provider) validateTargetLink(link provider.InterfaceLinkDefinition) error { + // TODO: Add validation checks + return nil +} diff --git a/examples/keyvalue-inmemory/main.go b/examples/keyvalue-inmemory/main.go index a216b31..4ef768f 100644 --- a/examples/keyvalue-inmemory/main.go +++ b/examples/keyvalue-inmemory/main.go @@ -20,8 +20,10 @@ func main() { func run() error { p := &Provider{ - sourceLinks: make(map[string]provider.InterfaceLinkDefinition), - targetLinks: make(map[string]provider.InterfaceLinkDefinition), + sourceLinks: make(map[string]provider.InterfaceLinkDefinition), + targetLinks: make(map[string]provider.InterfaceLinkDefinition), + failedSourceLinks: make(map[string]provider.InterfaceLinkDefinition), + failedTargetLinks: make(map[string]provider.InterfaceLinkDefinition), } wasmcloudprovider, err := provider.New( @@ -69,12 +71,24 @@ func run() error { func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error { log.Println("Handling new source link", link) + err := p.establishSourceLink(link) + if err != nil { + log.Println("Failed to establish source link", link, err) + p.failedSourceLinks[link.Target] = link + return err + } p.sourceLinks[link.Target] = link return nil } func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error { log.Println("Handling new target link", link) + err := p.establishTargetLink(link) + if err != nil { + log.Println("Failed to establish target link", link, err) + p.failedTargetLinks[link.SourceID] = link + return err + } p.targetLinks[link.SourceID] = link return nil } From 6fcf46839ff1660c65b424032924c31180ceedcf Mon Sep 17 00:00:00 2001 From: Thomas Fitzgerald <16616192+tom-fitz@users.noreply.github.com> Date: Thu, 13 Jun 2024 12:17:19 -0600 Subject: [PATCH 2/7] Update go.mod fixing go.mod --- examples/keyvalue-inmemory/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/keyvalue-inmemory/go.mod b/examples/keyvalue-inmemory/go.mod index 6c8479f..bdb6375 100644 --- a/examples/keyvalue-inmemory/go.mod +++ b/examples/keyvalue-inmemory/go.mod @@ -1,6 +1,6 @@ module github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory -go 1.20 +go 1.22.3 require ( github.com/wasmCloud/provider-sdk-go v0.0.0-20240124183610-1a92f8d04935 From 496b1e99f91bf898f9ec99cfee2be9d3799e07eb Mon Sep 17 00:00:00 2001 From: tom-fitz <16616192+tom-fitz@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:12:27 -0600 Subject: [PATCH 3/7] 10: starting the link refactor --- examples/keyvalue-inmemory/go.mod | 4 ++- examples/keyvalue-inmemory/link.go | 56 ++++++++++++++++++++++++------ examples/keyvalue-inmemory/main.go | 46 ------------------------ provider.go | 4 +-- 4 files changed, 51 insertions(+), 59 deletions(-) diff --git a/examples/keyvalue-inmemory/go.mod b/examples/keyvalue-inmemory/go.mod index 6c8479f..20c1f0e 100644 --- a/examples/keyvalue-inmemory/go.mod +++ b/examples/keyvalue-inmemory/go.mod @@ -1,6 +1,8 @@ module github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory -go 1.20 +go 1.22.2 + +toolchain go1.22.4 require ( github.com/wasmCloud/provider-sdk-go v0.0.0-20240124183610-1a92f8d04935 diff --git a/examples/keyvalue-inmemory/link.go b/examples/keyvalue-inmemory/link.go index d3dff27..70271af 100644 --- a/examples/keyvalue-inmemory/link.go +++ b/examples/keyvalue-inmemory/link.go @@ -6,11 +6,6 @@ import ( ) func (p *Provider) establishSourceLink(link provider.InterfaceLinkDefinition) error { - if _, exists := p.sourceLinks[link.Target]; exists { - log.Println("Source link already exists, ignoring duplicate", link) - return nil - } - if err := p.validateSourceLink(link); err != nil { return err } @@ -20,11 +15,6 @@ func (p *Provider) establishSourceLink(link provider.InterfaceLinkDefinition) er } func (p *Provider) establishTargetLink(link provider.InterfaceLinkDefinition) error { - if _, exists := p.targetLinks[link.SourceID]; exists { - log.Println("Target link already exists, ignoring duplicate", link) - return nil - } - if err := p.validateTargetLink(link); err != nil { return err } @@ -42,3 +32,49 @@ func (p *Provider) validateTargetLink(link provider.InterfaceLinkDefinition) err // TODO: Add validation checks return nil } + +func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error { + log.Println("Handling new source link", link) + err := p.establishSourceLink(link) + if err != nil { + log.Println("Failed to establish source link", link, err) + p.failedSourceLinks[link.Target] = link + return err + } + p.sourceLinks[link.Target] = link + return nil +} + +func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error { + log.Println("Handling new target link", link) + err := p.establishTargetLink(link) + if err != nil { + log.Println("Failed to establish target link", link, err) + p.failedTargetLinks[link.SourceID] = link + return err + } + p.targetLinks[link.SourceID] = link + return nil +} + +func (p *Provider) handleDelSourceLink(link provider.InterfaceLinkDefinition) error { + log.Println("Handling del source link", link) + delete(p.sourceLinks, link.Target) + return nil +} + +func (p *Provider) handleDelTargetLink(link provider.InterfaceLinkDefinition) error { + log.Println("Handling del target link", link) + delete(p.targetLinks, link.SourceID) + return nil +} + +func (p *Provider) handleHealthCheck() string { + log.Println("Handling health check") + return "provider healthy" +} + +func (p *Provider) handleShutdown() error { + log.Println("Handling shutdown") + return nil +} diff --git a/examples/keyvalue-inmemory/main.go b/examples/keyvalue-inmemory/main.go index 4ef768f..e0cdeee 100644 --- a/examples/keyvalue-inmemory/main.go +++ b/examples/keyvalue-inmemory/main.go @@ -68,49 +68,3 @@ func run() error { return nil } - -func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error { - log.Println("Handling new source link", link) - err := p.establishSourceLink(link) - if err != nil { - log.Println("Failed to establish source link", link, err) - p.failedSourceLinks[link.Target] = link - return err - } - p.sourceLinks[link.Target] = link - return nil -} - -func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error { - log.Println("Handling new target link", link) - err := p.establishTargetLink(link) - if err != nil { - log.Println("Failed to establish target link", link, err) - p.failedTargetLinks[link.SourceID] = link - return err - } - p.targetLinks[link.SourceID] = link - return nil -} - -func (p *Provider) handleDelSourceLink(link provider.InterfaceLinkDefinition) error { - log.Println("Handling del source link", link) - delete(p.sourceLinks, link.Target) - return nil -} - -func (p *Provider) handleDelTargetLink(link provider.InterfaceLinkDefinition) error { - log.Println("Handling del target link", link) - delete(p.targetLinks, link.SourceID) - return nil -} - -func (p *Provider) handleHealthCheck() string { - log.Println("Handling health check") - return "provider healthy" -} - -func (p *Provider) handleShutdown() error { - log.Println("Handling shutdown") - return nil -} diff --git a/provider.go b/provider.go index 69a8579..707971c 100644 --- a/provider.go +++ b/provider.go @@ -97,8 +97,8 @@ func New(options ...ProviderHandler) (*WasmcloudProvider, error) { } // partition links based on if the provider is the source or target - sourceLinks := []InterfaceLinkDefinition{} - targetLinks := []InterfaceLinkDefinition{} + var sourceLinks []InterfaceLinkDefinition + var targetLinks []InterfaceLinkDefinition // Loop over the numbers for _, link := range hostData.LinkDefinitions { From ef8d7489d7dcd93dc15240e8fb8d9f645bf58350 Mon Sep 17 00:00:00 2001 From: tom-fitz <16616192+tom-fitz@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:15:15 -0600 Subject: [PATCH 4/7] 10: fixing up go.mod version --- examples/keyvalue-inmemory/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/keyvalue-inmemory/go.mod b/examples/keyvalue-inmemory/go.mod index 20c1f0e..8263228 100644 --- a/examples/keyvalue-inmemory/go.mod +++ b/examples/keyvalue-inmemory/go.mod @@ -1,6 +1,6 @@ module github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory -go 1.22.2 +go 1.22.3 toolchain go1.22.4 From 6095e93fb3335e939db57d7e2225e3bed61c6f3a Mon Sep 17 00:00:00 2001 From: tom-fitz <16616192+tom-fitz@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:17:32 -0600 Subject: [PATCH 5/7] 10: removing tool chain addition to go.mod. Sorry IDE was configured different for a different project --- examples/keyvalue-inmemory/go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/keyvalue-inmemory/go.mod b/examples/keyvalue-inmemory/go.mod index 8263228..bdb6375 100644 --- a/examples/keyvalue-inmemory/go.mod +++ b/examples/keyvalue-inmemory/go.mod @@ -2,8 +2,6 @@ module github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory go 1.22.3 -toolchain go1.22.4 - require ( github.com/wasmCloud/provider-sdk-go v0.0.0-20240124183610-1a92f8d04935 github.com/wrpc/wrpc/go v0.0.0-20240508154835-d040797e7796 From fbee7a84410d4d827920fe2f92c24bf2b82703ee Mon Sep 17 00:00:00 2001 From: tom-fitz <16616192+tom-fitz@users.noreply.github.com> Date: Fri, 14 Jun 2024 18:17:24 -0600 Subject: [PATCH 6/7] 10: refactoring link logic to isolate in link.go --- examples/keyvalue-inmemory/main.go | 2 -- provider.go | 44 +++--------------------------- 2 files changed, 4 insertions(+), 42 deletions(-) diff --git a/examples/keyvalue-inmemory/main.go b/examples/keyvalue-inmemory/main.go index e0cdeee..d1bc42d 100644 --- a/examples/keyvalue-inmemory/main.go +++ b/examples/keyvalue-inmemory/main.go @@ -1,5 +1,3 @@ -//go:generate wit-bindgen-wrpc go --out-dir bindings --package github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory/bindings wit - package main import ( diff --git a/provider.go b/provider.go index 707971c..beb050b 100644 --- a/provider.go +++ b/provider.go @@ -336,56 +336,20 @@ func (wp *WasmcloudProvider) cleanupNatsSubscriptions() error { } func (wp *WasmcloudProvider) putLink(l InterfaceLinkDefinition) error { - // Ignore duplicate links - if wp.isLinked(l.SourceID, l.Target) { - wp.Logger.Info("ignoring duplicate link", "link", l) - return nil - } - - wp.lock.Lock() - defer wp.lock.Unlock() if l.SourceID == wp.Id { - err := wp.putSourceLinkFunc(l) - if err != nil { - return err - } - - wp.sourceLinks[l.Target] = l + return wp.putSourceLinkFunc(l) } else if l.Target == wp.Id { - err := wp.putTargetLinkFunc(l) - if err != nil { - return err - } - - wp.targetLinks[l.SourceID] = l - } else { - wp.Logger.Info("received link that isn't for this provider, ignoring", "link", l) + return wp.putTargetLinkFunc(l) } - return nil } func (wp *WasmcloudProvider) deleteLink(l InterfaceLinkDefinition) error { - wp.lock.Lock() - defer wp.lock.Unlock() if l.SourceID == wp.Id { - err := wp.delSourceLinkFunc(l) - if err != nil { - return err - } - - delete(wp.sourceLinks, l.Target) + return wp.delSourceLinkFunc(l) } else if l.Target == wp.Id { - err := wp.delTargetLinkFunc(l) - if err != nil { - return err - } - - delete(wp.targetLinks, l.SourceID) - } else { - wp.Logger.Info("received link delete that isn't for this provider, ignoring", "link", l) + return wp.delTargetLinkFunc(l) } - return nil } From f4a933b40829ebd3e6e4f83d0ee4484f53c76be6 Mon Sep 17 00:00:00 2001 From: tom-fitz <16616192+tom-fitz@users.noreply.github.com> Date: Sat, 15 Jun 2024 11:50:30 -0600 Subject: [PATCH 7/7] refactored lniks and provider.go --- provider.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/provider.go b/provider.go index beb050b..82520e8 100644 --- a/provider.go +++ b/provider.go @@ -336,19 +336,35 @@ func (wp *WasmcloudProvider) cleanupNatsSubscriptions() error { } func (wp *WasmcloudProvider) putLink(l InterfaceLinkDefinition) error { + // Ignore duplicate links + if wp.isLinked(l.SourceID, l.Target) { + wp.Logger.Info("ignoring duplicate link", "link", l) + return nil + } + + wp.lock.Lock() + defer wp.lock.Unlock() + if l.SourceID == wp.Id { return wp.putSourceLinkFunc(l) } else if l.Target == wp.Id { return wp.putTargetLinkFunc(l) + } else { + wp.Logger.Info("received link that isn't for this provider, ignoring", "link", l) } + return nil } func (wp *WasmcloudProvider) deleteLink(l InterfaceLinkDefinition) error { + wp.lock.Lock() + defer wp.lock.Unlock() if l.SourceID == wp.Id { return wp.delSourceLinkFunc(l) } else if l.Target == wp.Id { return wp.delTargetLinkFunc(l) + } else { + wp.Logger.Info("received link delete that isn't for this provider, ignoring", "link", l) } return nil }