diff --git a/vfs/vfscache/writeback/writeback.go b/vfs/vfscache/writeback/writeback.go index 3e3575158..5a1d6b4fd 100644 --- a/vfs/vfscache/writeback/writeback.go +++ b/vfs/vfscache/writeback/writeback.go @@ -254,8 +254,8 @@ func (wb *WriteBack) SetID(pid *Handle) { // // Use SetID to create Handles in advance of calling Add. // -// If modified is false then it doesn't cancel a pending upload if -// there is one as there is no need. +// If modified is false then it neither cancels an upload in progress +// nor delays a pending one, as the data to upload hasn't changed. func (wb *WriteBack) Add(id Handle, name string, size int64, modified bool, putFn PutFn) Handle { wb.mu.Lock() defer wb.mu.Unlock() @@ -263,8 +263,8 @@ func (wb *WriteBack) Add(id Handle, name string, size int64, modified bool, putF wbItem, ok := wb.lookup[id] if !ok { wbItem = wb._newItem(id, name, size) - } else { - if wbItem.uploading && modified { + } else if modified { + if wbItem.uploading { // We are uploading already so cancel the upload wb._cancelUpload(wbItem) } diff --git a/vfs/vfscache/writeback/writeback_test.go b/vfs/vfscache/writeback/writeback_test.go index 8122ae2da..0b28958b1 100644 --- a/vfs/vfscache/writeback/writeback_test.go +++ b/vfs/vfscache/writeback/writeback_test.go @@ -467,6 +467,28 @@ func TestWriteBackAddUpdateNotStarted(t *testing.T) { assert.False(t, pi.called) } +// Re-adding an unmodified item, as closing a dirty file which was only +// read does, must not delay its upload, otherwise a file which is read +// more often than the writeback delay never gets uploaded. +func TestWriteBackAddNotModifiedKeepsExpiry(t *testing.T) { + wb, cancel := newTestWriteBack(t) + defer cancel() + wb.opt.WriteBack = fs.Duration(time.Hour) + + pi := newPutItem(t) + id := wb.Add(0, "one", 10, true, pi.put) + wbItem := wb.lookup[id] + expiry := wbItem.expiry + + time.Sleep(10 * time.Millisecond) + wb.Add(id, "one", 10, false, pi.put) + assert.Equal(t, expiry, wbItem.expiry, "unmodified add delayed the upload") + + wb.Add(id, "one", 10, true, pi.put) + assert.True(t, wbItem.expiry.After(expiry), "modified add didn't delay the upload") + assert.False(t, pi.called) +} + func TestWriteBackGetStats(t *testing.T) { wb, cancel := newTestWriteBack(t) defer cancel()