Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Rename global err to shared err
It's not global, just shared among goroutines in the scope of a method
  • Loading branch information
philippgille committed Mar 4, 2024
commit 3e5a7f6e2ea33d715a1b6c684936eaaaf818c51f
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