Skip to content

Commit

Permalink
Merge pull request #37 from philippgille/rename-global-err
Browse files Browse the repository at this point in the history
Rename global err to shared err
  • Loading branch information
philippgille authored Mar 4, 2024
2 parents 9d93a7a + 3e5a7f6 commit 36ca898
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 36ca898

Please sign in to comment.