Skip to content

Commit

Permalink
Merge pull request #2 from philippgille/improve-collection-add-concur…
Browse files Browse the repository at this point in the history
…rency

Improve concurrency when adding documents to collection
  • Loading branch information
philippgille authored Jan 13, 2024
2 parents 5fa0884 + efb8800 commit 5f35023
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ func (c *Collection) add(ctx context.Context, ids []string, documents []string,
}

ctx, cancel := context.WithCancelCause(ctx)
defer cancel(nil)

var wg sync.WaitGroup
var globalErr error
var globalErrLock sync.RWMutex
var globalErrLock sync.Mutex
semaphore := make(chan struct{}, concurrency)
for i, document := range documents {
var embedding []float32
Expand All @@ -90,13 +91,9 @@ func (c *Collection) add(ctx context.Context, ids []string, documents []string,
defer wg.Done()

// Don't even start if we already have an error
globalErrLock.RLock()
// We don't defer the unlock because we want to unlock much earlier.
if globalErr != nil {
globalErrLock.RUnlock()
if ctx.Err() != nil {
return
}
globalErrLock.RUnlock()

// Wait here while $concurrency other goroutines are creating documents.
semaphore <- struct{}{}
Expand Down

0 comments on commit 5f35023

Please sign in to comment.