From 97e3b47b19028f11b99827bac0c18624abb82fc1 Mon Sep 17 00:00:00 2001 From: Seednode Date: Fri, 28 Oct 2022 19:49:49 -0500 Subject: [PATCH] Only seed rng once, at initialization, and speed up picking random file by using rand.Intn instead of shuffling the slice --- cmd/files.go | 6 ++---- cmd/version.go | 2 +- cmd/web.go | 3 +++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index 0528cde..c23344b 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -495,13 +495,11 @@ func pickFile(args []string, filters *Filters, sort string, fileCache *[]string) } } - rand.Seed(time.Now().UnixNano()) - rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] }) + for true { + i := rand.Intn(len(fileList)) - for i := 0; i < len(fileList); i++ { filePath := fileList[i] - // If not caching, check for valid images just before serving, to speed up scanning if !Cache { isImage, err := isImage(filePath) if err != nil { diff --git a/cmd/version.go b/cmd/version.go index 8c31a1b..5832493 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.20.2" +var Version = "0.20.3" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index c050c34..6a06094 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -7,6 +7,7 @@ package cmd import ( "fmt" "io" + "math/rand" "net/http" "net/url" "os" @@ -498,6 +499,8 @@ func ServePage(args []string) error { re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`) + rand.Seed(time.Now().UnixNano()) + fileCache := []string{} http.Handle("/", serveHtmlHandler(paths, *re, &fileCache))