mirror of
https://github.com/drakkan/sftpgo.git
synced 2026-03-14 14:25:52 +01:00
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:
parent
f1022db5c1
commit
ed0c1a01ab
2 changed files with 24 additions and 23 deletions
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue