micro optimization to the function keepConnectionAlive

we don't need a goroutine

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino 2026-01-21 18:30:46 +01:00
commit ed0c1a01ab
No known key found for this signature in database
GPG key ID: 935D2952DEC4EECF
2 changed files with 24 additions and 23 deletions

View file

@ -780,9 +780,8 @@ func (c *BaseConnection) Copy(virtualSourcePath, virtualTargetPath string) error
if err := c.CheckParentDirs(path.Dir(destPath)); err != nil {
return err
}
done := make(chan bool)
defer close(done)
go keepConnectionAlive(c, done, 2*time.Minute)
stopKeepAlive := keepConnectionAlive(c, 2*time.Minute)
defer stopKeepAlive()
return c.doRecursiveCopy(virtualSourcePath, destPath, srcInfo, createTargetDir, 0)
}
@ -848,9 +847,8 @@ func (c *BaseConnection) renameInternal(virtualSourcePath, virtualTargetPath str
if checkParentDestination {
c.CheckParentDirs(path.Dir(virtualTargetPath)) //nolint:errcheck
}
done := make(chan bool)
defer close(done)
go keepConnectionAlive(c, done, 2*time.Minute)
stopKeepAlive := keepConnectionAlive(c, 2*time.Minute)
defer stopKeepAlive()
files, size, err := fsDst.Rename(fsSourcePath, fsTargetPath, checks)
if err != nil {
@ -1869,18 +1867,22 @@ func getPermissionDeniedError(protocol string) error {
}
}
func keepConnectionAlive(c *BaseConnection, done chan bool, interval time.Duration) {
ticker := time.NewTicker(interval)
defer func() {
ticker.Stop()
}()
func keepConnectionAlive(c *BaseConnection, interval time.Duration) func() {
var timer *time.Timer
var closed atomic.Bool
for {
select {
case <-done:
return
case <-ticker.C:
c.UpdateLastActivity()
task := func() {
c.UpdateLastActivity()
if !closed.Load() {
timer.Reset(interval)
}
}
timer = time.AfterFunc(interval, task)
return func() {
closed.Store(true)
timer.Stop()
}
}

View file

@ -627,12 +627,11 @@ func TestErrorResolvePath(t *testing.T) {
func TestConnectionKeepAlive(t *testing.T) {
conn := NewBaseConnection("", ProtocolWebDAV, "", "", dataprovider.User{})
lastActivity := conn.GetLastActivity()
done := make(chan bool)
go func() {
time.Sleep(200 * time.Millisecond)
close(done)
}()
keepConnectionAlive(conn, done, 50*time.Millisecond)
stop := keepConnectionAlive(conn, 50*time.Millisecond)
defer stop()
time.Sleep(200 * time.Millisecond)
assert.Greater(t, conn.GetLastActivity(), lastActivity)
}