Skip to content

Commit

Permalink
change to single go routine
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Jul 1, 2024
1 parent 0686be2 commit 44fd3c6
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions internal/db/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (

func (db *db) handleMessages(ctx context.Context, sub *event.Subscription) {
queue := newMergeQueue()
// These are used to ensure we only trigger loadAndPublishP2PCollections and loadAndPublishReplicators
// This is used to ensure we only trigger loadAndPublishP2PCollections and loadAndPublishReplicators
// once per db instanciation.
onceReps := sync.Once{}
onceP2PCollection := sync.Once{}
loadOnce := sync.Once{}
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -66,16 +65,15 @@ func (db *db) handleMessages(ctx context.Context, sub *event.Subscription) {
case event.PeerInfo:
db.peerInfo.Store(evt.Info)
// Load and publish P2P collections and replicators once per db instance start.
// Go routines are used to ensure the message handler is not blocked by these potentially
// A Go routine is used to ensure the message handler is not blocked by these potentially
// long running operations.
go onceP2PCollection.Do(func() {
go loadOnce.Do(func() {
err := db.loadAndPublishP2PCollections(ctx)
if err != nil {
log.ErrorContextE(ctx, "Failed to load P2P collections", err)
}
})
go onceReps.Do(func() {
err := db.loadAndPublishReplicators(ctx)

err = db.loadAndPublishReplicators(ctx)
if err != nil {
log.ErrorContextE(ctx, "Failed to load replicators", err)
}
Expand Down

0 comments on commit 44fd3c6

Please sign in to comment.