mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-27 02:24:09 -05:00
fix(search): run the debounced work once when both timers fire
Every pending space has a debounce timer and a timeout timer, and both call the same work function. The function checked for a run in progress before taking the mutex, so when both timers were due together, both calls passed the check and the space was indexed twice at the same time. Take the mutex first and return when the work item is no longer the pending one. This is also what made "fires at the timeout even when continuously debounced" flaky: its two timers are due about 50ms apart, which is close enough on a busy CI runner.
This commit is contained in:
1 parent
2032e85c83
commit
ca733e731f
2 files changed
+22
-5
No files matched your search
@@ -61,17 +61,19 @@ func (d *SpaceDebouncer) Debounce(id *provider.StorageSpaceId, ack AckFunc, forc
|
||||
|
||||
wi := &workItem{force: force}
|
||||
wi.work = func() {
|
||||
d.mutex.Lock()
|
||||
if d.pending[id.OpaqueId] != wi {
|
||||
// the debounce timer and the timeout can both fire for the same item
|
||||
d.mutex.Unlock()
|
||||
return
|
||||
}
|
||||
if _, ok := d.inProgress.Load(id.OpaqueId); ok {
|
||||
// Reschedule this run for when the previous run has finished
|
||||
d.mutex.Lock()
|
||||
if wi := d.pending[id.OpaqueId]; wi != nil {
|
||||
wi.t.Reset(d.after)
|
||||
}
|
||||
wi.t.Reset(d.after)
|
||||
d.mutex.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
d.mutex.Lock()
|
||||
wi.timeout.Stop() // stop the timeout timer if it is running
|
||||
delete(d.pending, id.OpaqueId)
|
||||
force := wi.force
|
||||
|
||||
@@ -115,6 +115,21 @@ var _ = Describe("SpaceDebouncer", func() {
|
||||
}, "300ms").Should(Equal(1))
|
||||
})
|
||||
|
||||
It("runs once when the debounce timer and the timeout are due together", func() {
|
||||
debouncer = event.NewSpaceDebouncer(20*time.Millisecond, 20*time.Millisecond, func(id *sprovider.StorageSpaceId, _ bool) {
|
||||
if id.OpaqueId == "spaceid" {
|
||||
callCount.Add(1)
|
||||
}
|
||||
}, log.NewLogger())
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
callCount.Store(0)
|
||||
debouncer.Debounce(spaceid, nil, false)
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
Expect(int(callCount.Load())).To(Equal(1), "iteration %d", i)
|
||||
}
|
||||
})
|
||||
|
||||
It("doesn't run the timeout function if the work function has been called", func() {
|
||||
debouncer = event.NewSpaceDebouncer(100*time.Millisecond, 250*time.Millisecond, func(id *sprovider.StorageSpaceId, _ bool) {
|
||||
if id.OpaqueId == "spaceid" {
|
||||
|
||||
Reference in new issue
Block a user