Skip to content

Commit

Permalink
Better lock contention handling within batches
Browse files Browse the repository at this point in the history
  • Loading branch information
radazen committed Oct 20, 2023
1 parent 01f5909 commit a59b27c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions cmd/dataloaders/generator/dataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ type Dataloader[TKey any, TResult any] struct {
}

type batch[TKey any, TResult any] struct {
dataloader *Dataloader[TKey, TResult]
id int32
keys []TKey
jsonKeys []string
results []TResult
errors []error
status batchStatus
done chan struct{}
mu sync.Mutex
dataloader *Dataloader[TKey, TResult]
id int32
keys []TKey
jsonKeys []string
results []TResult
errors []error
status batchStatus
done chan struct{}
mu sync.Mutex
numAssigned int32
}

func NewDataloader[TKey comparable, TResult any](ctx context.Context, maxBatchSize int, batchTimeout time.Duration, cacheResults bool, publishResults bool,
Expand Down Expand Up @@ -252,6 +253,11 @@ func (d *Dataloader[TKey, TResult]) addKeyToBatch(key TKey, jsonKey string) (*ba
}

b := actual.(*batch[TKey, TResult])
numAssigned := atomic.AddInt32(&b.numAssigned, 1)
if numAssigned > int32(d.maxBatchSize) {
atomic.CompareAndSwapInt32(&d.currentBatchID, currentID, currentID+1)
continue
}

b.mu.Lock()

Expand Down

0 comments on commit a59b27c

Please sign in to comment.