Skip to content

Commit

Permalink
Rename global err to shared err
Browse files Browse the repository at this point in the history
It's not global, just shared among goroutines in the scope of a method
  • Loading branch information
philippgille committed Mar 4, 2024
1 parent 7ec2813 commit 3e5a7f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
20 changes: 10 additions & 10 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,18 @@ func (c *Collection) AddDocuments(ctx context.Context, documents []Document, con
}
// For other validations we rely on AddDocument.

var globalErr error
globalErrLock := sync.Mutex{}
var sharedErr error
sharedErrLock := sync.Mutex{}
ctx, cancel := context.WithCancelCause(ctx)
defer cancel(nil)
setGlobalErr := func(err error) {
globalErrLock.Lock()
defer globalErrLock.Unlock()
setSharedErr := func(err error) {
sharedErrLock.Lock()
defer sharedErrLock.Unlock()
// Another goroutine might have already set the error.
if globalErr == nil {
globalErr = err
if sharedErr == nil {
sharedErr = err
// Cancel the operation for all other goroutines.
cancel(globalErr)
cancel(sharedErr)
}
}

Expand All @@ -184,15 +184,15 @@ func (c *Collection) AddDocuments(ctx context.Context, documents []Document, con

err := c.AddDocument(ctx, doc)
if err != nil {
setGlobalErr(fmt.Errorf("couldn't add document '%s': %w", doc.ID, err))
setSharedErr(fmt.Errorf("couldn't add document '%s': %w", doc.ID, err))
return
}
}(doc)
}

wg.Wait()

return globalErr
return sharedErr
}

// AddDocument adds a document to the collection.
Expand Down
22 changes: 11 additions & 11 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ func calcDocSimilarity(ctx context.Context, queryVectors []float32, docs []*Docu
concurrency = numDocs
}

var globalErr error
globalErrLock := sync.Mutex{}
var sharedErr error
sharedErrLock := sync.Mutex{}
ctx, cancel := context.WithCancelCause(ctx)
defer cancel(nil)
setGlobalErr := func(err error) {
globalErrLock.Lock()
defer globalErrLock.Unlock()
setSharedErr := func(err error) {
sharedErrLock.Lock()
defer sharedErrLock.Unlock()
// Another goroutine might have already set the error.
if globalErr == nil {
globalErr = err
if sharedErr == nil {
sharedErr = err
// Cancel the operation for all other goroutines.
cancel(globalErr)
cancel(sharedErr)
}
}

Expand All @@ -139,7 +139,7 @@ func calcDocSimilarity(ctx context.Context, queryVectors []float32, docs []*Docu

sim, err := cosineSimilarity(queryVectors, doc.Embedding)
if err != nil {
setGlobalErr(fmt.Errorf("couldn't calculate similarity for document '%s': %w", doc.ID, err))
setSharedErr(fmt.Errorf("couldn't calculate similarity for document '%s': %w", doc.ID, err))
return
}

Expand Down Expand Up @@ -175,8 +175,8 @@ OuterLoop:

wg.Wait()

if globalErr != nil {
return nil, globalErr
if sharedErr != nil {
return nil, sharedErr
}

return res, nil
Expand Down

0 comments on commit 3e5a7f6

Please sign in to comment.