Now in hook_pre_recieve.go, when cat file -s is called - the work is no longer shared among workers - race condition fixed

This commit is contained in:
DmitryFrolovTri
2023-07-11 09:27:28 +00:00
parent bc051a3991
commit b521d36be5
+11 -3
View File
@@ -262,15 +262,21 @@ func loadObjectsSizesViaCatFile(ctx *gitea_context.PrivateContext, opts *git.Run
var wg sync.WaitGroup
var mu sync.Mutex
reducedObjectIDs := make([]string, 0, len(objectIDs))
// Prepare numWorker slices to store the work
reducedObjectIDs := make([][]string, 0, numWorkers)
for i := 0; i < numWorkers; i++ {
reducedObjectIDs[i] = make([]string, 0, len(objectIDs)/numWorkers+1)
}
// Loop over all objectIDs and find which ones are missing size information
i := 0
for _, objectID := range objectIDs {
_, exists := objectsSizes[objectID]
// If object doesn't yet have size in objectsSizes add it for further processing
if !exists {
reducedObjectIDs = append(reducedObjectIDs, objectID)
reducedObjectIDs[i%numWorkers] = append(reducedObjectIDs[i%numWorkers], objectID)
i++
}
}
@@ -280,12 +286,14 @@ func loadObjectsSizesViaCatFile(ctx *gitea_context.PrivateContext, opts *git.Run
go func(reducedObjectIDs []string) {
defer wg.Done()
for _, objectID := range reducedObjectIDs {
ctx := ctx
opts := opts
objectSize := calculateSizeOfObject(ctx, opts, objectID)
mu.Lock() // Protecting shared resource
objectsSizes[objectID] = objectSize
mu.Unlock() // Releasing shared resource for other goroutines
}
}(reducedObjectIDs)
}(reducedObjectIDs[(w-1)%numWorkers])
}
// Wait for all workers to finish processing.