From d4e86f4d8b07eaf21c6e3e37c536ff587482ac89 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 6 Sep 2024 12:12:22 +0100 Subject: [PATCH] rc: add relative to vfs/queue-set-expiry --- vfs/rc.go | 12 ++++++++++-- vfs/vfscache/cache.go | 7 +++++-- vfs/vfscache/cache_test.go | 2 +- vfs/vfscache/writeback/writeback.go | 11 ++++++++++- vfs/vfscache/writeback/writeback_test.go | 8 ++++---- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/vfs/rc.go b/vfs/rc.go index 72060984b..5c74e9f91 100644 --- a/vfs/rc.go +++ b/vfs/rc.go @@ -514,6 +514,7 @@ This takes the following parameters - |fs| - select the VFS in use (optional) - |id| - a numeric ID as returned from |vfs/queue| - |expiry| - a new expiry time as floating point seconds +- |relative| - if set, expiry is to be treated as relative to the current expiry (optional, boolean) This returns an empty result on success, or an error. @@ -540,9 +541,16 @@ func rcQueueSetExpiry(ctx context.Context, in rc.Params) (out rc.Params, err err if err != nil { return nil, err } + relative, err := in.GetBool("relative") + if err != nil && !rc.IsErrParamNotFound(err) { + return nil, err + } // Set expiry - expiryTime := time.Now().Add(time.Duration(float64(time.Second) * expiry)) - err = vfs.cache.QueueSetExpiry(writeback.Handle(id), expiryTime) + var refTime time.Time + if !relative { + refTime = time.Now() + } + err = vfs.cache.QueueSetExpiry(writeback.Handle(id), refTime, time.Duration(float64(time.Second)*expiry)) return nil, err } diff --git a/vfs/vfscache/cache.go b/vfs/vfscache/cache.go index a0af31bd1..a17c79948 100644 --- a/vfs/vfscache/cache.go +++ b/vfs/vfscache/cache.go @@ -178,8 +178,11 @@ func (c *Cache) Queue() (out rc.Params) { } // QueueSetExpiry updates the expiry of a single item in the upload queue -func (c *Cache) QueueSetExpiry(id writeback.Handle, expiry time.Time) error { - return c.writeback.SetExpiry(id, expiry) +// +// The expiry time is set to expiry + relative if expiry is passed in, +// otherwise the expiry of the item is used. +func (c *Cache) QueueSetExpiry(id writeback.Handle, expiry time.Time, relative time.Duration) error { + return c.writeback.SetExpiry(id, expiry, relative) } // createDir creates a directory path, along with any necessary parents diff --git a/vfs/vfscache/cache_test.go b/vfs/vfscache/cache_test.go index 9f8abfa64..327962cf2 100644 --- a/vfs/vfscache/cache_test.go +++ b/vfs/vfscache/cache_test.go @@ -748,6 +748,6 @@ func TestCacheQueueSetExpiry(t *testing.T) { // Check this returns the correct error when called so we know // it is plumbed in correctly. The actual tests are done in // writeback. - err := c.QueueSetExpiry(123123, time.Now()) + err := c.QueueSetExpiry(123123, time.Now(), 0) assert.Equal(t, writeback.ErrorIDNotFound, err) } diff --git a/vfs/vfscache/writeback/writeback.go b/vfs/vfscache/writeback/writeback.go index 6212bfec9..b3c022253 100644 --- a/vfs/vfscache/writeback/writeback.go +++ b/vfs/vfscache/writeback/writeback.go @@ -519,8 +519,11 @@ var ErrorIDNotFound = errors.New("id not found in queue") // // id should be as returned from the Queue call // +// The expiry time is set to expiry + relative if expiry is passed in, +// otherwise the expiry of the item is used. +// // If the item isn't found then it will return ErrorIDNotFound -func (wb *WriteBack) SetExpiry(id Handle, expiry time.Time) error { +func (wb *WriteBack) SetExpiry(id Handle, expiry time.Time, relative time.Duration) error { wb.mu.Lock() defer wb.mu.Unlock() @@ -529,6 +532,12 @@ func (wb *WriteBack) SetExpiry(id Handle, expiry time.Time) error { return ErrorIDNotFound } + // If expiry is not supplied, use expiry of item + if expiry.IsZero() { + expiry = wbItem.expiry + } + expiry = expiry.Add(relative) + // Update the expiry with the user requested value wb.items._update(wbItem, expiry) wb._resetTimer() diff --git a/vfs/vfscache/writeback/writeback_test.go b/vfs/vfscache/writeback/writeback_test.go index 872d19e71..9bbf90f0d 100644 --- a/vfs/vfscache/writeback/writeback_test.go +++ b/vfs/vfscache/writeback/writeback_test.go @@ -549,7 +549,7 @@ func TestWriteBackSetExpiry(t *testing.T) { wb, cancel := newTestWriteBack(t) defer cancel() - err := wb.SetExpiry(123123123, time.Now()) + err := wb.SetExpiry(123123123, time.Now(), 0) assert.Equal(t, ErrorIDNotFound, err) pi := newPutItem(t) @@ -569,12 +569,12 @@ func TestWriteBackSetExpiry(t *testing.T) { assert.Less(t, expiry, 1.0) newExpiry := time.Now().Add(100 * time.Second) - require.NoError(t, wb.SetExpiry(wbItem.id, newExpiry)) + require.NoError(t, wb.SetExpiry(wbItem.id, newExpiry, 0)) assert.Equal(t, newExpiry, getExpiry()) // This starts the transfer - newExpiry = time.Now().Add(-100 * time.Second) - require.NoError(t, wb.SetExpiry(wbItem.id, newExpiry)) + newExpiry = wbItem.expiry.Add(-200 * time.Second) + require.NoError(t, wb.SetExpiry(wbItem.id, time.Time{}, -200*time.Second)) assert.Equal(t, newExpiry, getExpiry()) <-pi.started