Files
ShenLin 8495470e63 fix(search): serialize index updates by parent (#2827)
- Prevent concurrent updates from inserting duplicate index entries
- Preserve parallel indexing across different parent paths
- Add serialization, concurrency, and lock cleanup tests

Signed-off-by: jyxjjj <16695261+jyxjjj@users.noreply.github.com>
Co-authored-by: Codex <267193182+codex@users.noreply.github.com>
Co-authored-by: Pikachu Ren <40362270+PIKACHUIM@users.noreply.github.com>
2026-07-23 19:33:25 +08:00

39 lines
666 B
Go

package search
import "sync"
var (
updateLocksMu sync.Mutex
updateLocks = make(map[string]*updateLock)
)
type updateLock struct {
mu sync.Mutex
refs uint
}
// lockUpdate serializes index updates for the same parent while allowing
// unrelated directories to update concurrently.
func lockUpdate(parent string) func() {
updateLocksMu.Lock()
lock, ok := updateLocks[parent]
if !ok {
lock = &updateLock{}
updateLocks[parent] = lock
}
lock.refs++
updateLocksMu.Unlock()
lock.mu.Lock()
return func() {
lock.mu.Unlock()
updateLocksMu.Lock()
lock.refs--
if lock.refs == 0 {
delete(updateLocks, parent)
}
updateLocksMu.Unlock()
}
}