mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2026-10-10 13:03:09 +08:00
8495470e63
- 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>
39 lines
666 B
Go
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()
|
|
}
|
|
}
|