Skip to content

Commit

Permalink
feat: remove redundant SubscriberStore
Browse files Browse the repository at this point in the history
Forest now provides the Archive type and the ExtendedStore type
directly, which eliminates the need for this extra store
interface and implementation.
  • Loading branch information
whereswaldon committed May 17, 2020
1 parent c8287a8 commit a418884
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 259 deletions.
3 changes: 2 additions & 1 deletion cmd/relay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"git.sr.ht/~whereswaldon/forest-go"
"git.sr.ht/~whereswaldon/forest-go/grove"
"git.sr.ht/~whereswaldon/forest-go/store"
sprout "git.sr.ht/~whereswaldon/sprout-go"
"git.sr.ht/~whereswaldon/sprout-go/watch"
)
Expand Down Expand Up @@ -71,7 +72,7 @@ and will establish Sprout connections to all addresses provided as arguments.
if err != nil {
log.Fatalf("Failed to create grove at %s: %v", *grovePath, err)
}
messages := sprout.NewSubscriberStore(grove)
messages := store.NewArchive(grove)
defer messages.Destroy()

// track node ids of nodes that we've recently inserted into the grove so that
Expand Down
239 changes: 0 additions & 239 deletions subscriber_store.go

This file was deleted.

6 changes: 4 additions & 2 deletions supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"
"time"

"git.sr.ht/~whereswaldon/forest-go/store"
)

// LaunchSupervisedWorker launches a worker in a new goroutine that will
Expand All @@ -13,7 +15,7 @@ import (
// `logger`.
//
// BUG(whereswaldon): this interface is experimental and likely to change.
func LaunchSupervisedWorker(done <-chan struct{}, addr string, store SubscribableStore, tlsConfig *tls.Config, logger *log.Logger) {
func LaunchSupervisedWorker(done <-chan struct{}, addr string, s store.ExtendedStore, tlsConfig *tls.Config, logger *log.Logger) {
go func() {
firstAttempt := true
for {
Expand All @@ -27,7 +29,7 @@ func LaunchSupervisedWorker(done <-chan struct{}, addr string, store Subscribabl
logger.Printf("Failed to connect to %s: %v", addr, err)
continue
}
worker, err := NewWorker(done, conn, store)
worker, err := NewWorker(done, conn, s)
if err != nil {
logger.Printf("Failed launching worker to connect to address %s: %v", addr, err)
continue
Expand Down
27 changes: 10 additions & 17 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,20 @@ import (
"git.sr.ht/~whereswaldon/forest-go/store"
)

type SubscribableStore interface {
forest.Store
SubscribeToNewMessages(handler func(n forest.Node)) Subscription
UnsubscribeToNewMessages(Subscription)
AddAs(forest.Node, Subscription) (err error)
}

type Worker struct {
Done <-chan struct{}
DefaultTimeout time.Duration
*Conn
*log.Logger
*Session
SubscribableStore
subscriptionID Subscription
SubscribableStore store.ExtendedStore
subscriptionID store.Subscription
}

func NewWorker(done <-chan struct{}, conn net.Conn, store SubscribableStore) (*Worker, error) {
func NewWorker(done <-chan struct{}, conn net.Conn, s store.ExtendedStore) (*Worker, error) {
w := &Worker{
Done: done,
SubscribableStore: store,
SubscribableStore: s,
Logger: log.New(log.Writer(), "", log.LstdFlags|log.Lshortfile),
DefaultTimeout: time.Minute,
}
Expand Down Expand Up @@ -280,7 +273,7 @@ func (c *Worker) IngestNode(node forest.Node) error {
if err := ancestor.ValidateDeep(c.SubscribableStore); err != nil {
return fmt.Errorf("validation failed for ancestor %s: %w", ancestor.ID(), err)
}
if err := c.AddAs(ancestor, c.subscriptionID); err != nil {
if err := c.SubscribableStore.AddAs(ancestor, c.subscriptionID); err != nil {
return fmt.Errorf("failed inserting ancestory %s into store: %w", ancestor.ID(), err)
}
}
Expand Down Expand Up @@ -362,7 +355,7 @@ func (c *Worker) BootstrapLocalStore(maxCommunities int) {
c.Printf("Couldn't fetch author information for node %s: %v", community.ID().String(), err)
continue
}
if err := c.AddAs(community, c.subscriptionID); err != nil {
if err := c.SubscribableStore.AddAs(community, c.subscriptionID); err != nil {
c.Printf("Couldn't add community %s to store: %v", community.ID().String(), err)
continue
}
Expand Down Expand Up @@ -404,7 +397,7 @@ func (c *Worker) synchronizeFullTree(root forest.Node, maxNodes int, perRequestT
}
c.Printf("Announced local leaves of %s to peer", root.ID())
for _, leaf := range leafList.Nodes {
if _, alreadyInStore, err := c.Get(leaf.ID()); err != nil {
if _, alreadyInStore, err := c.SubscribableStore.Get(leaf.ID()); err != nil {
return fmt.Errorf("failed checking if we already have leaf node %s: %w", leaf.ID().String(), err)
} else if alreadyInStore {
continue
Expand All @@ -424,7 +417,7 @@ func (c *Worker) synchronizeFullTree(root forest.Node, maxNodes int, perRequestT
if err := ancestor.ValidateDeep(c.SubscribableStore); err != nil {
return fmt.Errorf("couldn't validate node %s: %w", ancestor.ID().String(), err)
}
if err := c.AddAs(ancestor, c.subscriptionID); err != nil {
if err := c.SubscribableStore.AddAs(ancestor, c.subscriptionID); err != nil {
return fmt.Errorf("couldn't add node %s to store: %w", ancestor.ID().String(), err)
}
}
Expand All @@ -450,7 +443,7 @@ func (c *Worker) ensureAuthorAvailable(node forest.Node, perRequestTimeout time.
default:
return fmt.Errorf("unsupported type in ensureAuthorAvailable: %T", n)
}
_, inStore, err := c.GetIdentity(authorID)
_, inStore, err := c.SubscribableStore.GetIdentity(authorID)
if err != nil {
return fmt.Errorf("failed looking for author id %s in store: %w", authorID.String(), err)
}
Expand All @@ -468,7 +461,7 @@ func (c *Worker) ensureAuthorAvailable(node forest.Node, perRequestTimeout time.
if err := author.ValidateDeep(c.SubscribableStore); err != nil {
return fmt.Errorf("unable to validate author %s: %w", author.ID().String(), err)
}
if err := c.AddAs(author, c.subscriptionID); err != nil {
if err := c.SubscribableStore.AddAs(author, c.subscriptionID); err != nil {
return fmt.Errorf("failed inserting new valid author %s into store: %w", author.ID().String(), err)
}
return nil
Expand Down

0 comments on commit a418884

Please sign in to comment.