Only seed rng once, at initialization, and speed up picking random file by using rand.Intn instead of shuffling the slice

This commit is contained in:
Seednode 2022-10-28 19:49:49 -05:00
parent 920bdb2615
commit 97e3b47b19
3 changed files with 6 additions and 5 deletions

View File

@ -495,13 +495,11 @@ func pickFile(args []string, filters *Filters, sort string, fileCache *[]string)
} }
} }
rand.Seed(time.Now().UnixNano()) for true {
rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] }) i := rand.Intn(len(fileList))
for i := 0; i < len(fileList); i++ {
filePath := fileList[i] filePath := fileList[i]
// If not caching, check for valid images just before serving, to speed up scanning
if !Cache { if !Cache {
isImage, err := isImage(filePath) isImage, err := isImage(filePath)
if err != nil { if err != nil {

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.20.2" var Version = "0.20.3"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -7,6 +7,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io" "io"
"math/rand"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -498,6 +499,8 @@ func ServePage(args []string) error {
re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`) re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`)
rand.Seed(time.Now().UnixNano())
fileCache := []string{} fileCache := []string{}
http.Handle("/", serveHtmlHandler(paths, *re, &fileCache)) http.Handle("/", serveHtmlHandler(paths, *re, &fileCache))