fix (ls): configurable ls timeout for plugins

This commit is contained in:
Mickael Kerjean 2023-03-24 22:19:08 +11:00
parent cea8a77cd8
commit fa9f1bc3e4
2 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,26 @@
package plg_backend_s3
import (
. "github.com/mickael-kerjean/filestash/server/common"
"time"
)
var ls_timeout func() time.Duration
func init() {
ls_timeout = func() time.Duration {
return time.Duration(Config.Get("features.protection.ls_timeout").Schema(func(f *FormElement) *FormElement {
if f == nil {
f = &FormElement{}
}
f.Default = 2
f.Name = "ls_timeout"
f.Type = "number"
f.Target = []string{}
f.Description = "failsafe timeout for listing files under a folder"
f.Placeholder = "Default: 2"
return f
}).Int()) * time.Second
}
ls_timeout()
}

View file

@ -19,6 +19,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
)
var S3Cache AppCache
@ -169,6 +170,7 @@ func (s S3Backend) Ls(path string) (files []os.FileInfo, err error) {
}
client := s3.New(s.createSession(p.bucket))
startTime := time.Now()
err = client.ListObjectsV2PagesWithContext(
s.context,
&s3.ListObjectsV2Input{
@ -194,6 +196,11 @@ func (s S3Backend) Ls(path string) (files []os.FileInfo, err error) {
FType: "directory",
})
}
if time.Since(startTime) > ls_timeout() {
Log.Debug("plg_backend_s3::ls timeout triggered after getting %d files", len(files))
return false
}
return aws.BoolValue(objs.IsTruncated)
})
return files, err