From f782846f02ee5d97471d9dc7b3581c6217c4bfcf Mon Sep 17 00:00:00 2001 From: Seednode Date: Tue, 26 Sep 2023 11:15:08 -0500 Subject: [PATCH] Log import/export of cache, and hopefully fix cache rebuild functionality --- cmd/cache.go | 51 +++++++++++++++++++++++++++++-------------------- cmd/favicons.go | 4 +--- cmd/root.go | 2 +- cmd/web.go | 20 +++++++++++++++++-- 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/cmd/cache.go b/cmd/cache.go index 156985d..c3d143a 100644 --- a/cmd/cache.go +++ b/cmd/cache.go @@ -6,9 +6,11 @@ package cmd import ( "encoding/gob" + "fmt" "net/http" "os" "sync" + "time" "github.com/julienschmidt/httprouter" "github.com/klauspost/compress/zstd" @@ -83,6 +85,8 @@ func (cache *fileCache) isEmpty() bool { } func (cache *fileCache) Export(path string) error { + startTime := time.Now() + file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return err @@ -99,12 +103,23 @@ func (cache *fileCache) Export(path string) error { cache.mutex.RLock() enc.Encode(&cache.list) + length := len(cache.list) cache.mutex.RUnlock() + if Verbose { + fmt.Printf("%s | CACHE: Exported %d entries in %s\n", + time.Now().Format(logDate), + length, + time.Since(startTime), + ) + } + return nil } func (cache *fileCache) Import(path string) error { + startTime := time.Now() + file, err := os.OpenFile(path, os.O_RDONLY, 0600) if err != nil { return err @@ -123,26 +138,38 @@ func (cache *fileCache) Import(path string) error { err = dec.Decode(&cache.list) + length := len(cache.list) + cache.mutex.Unlock() if err != nil { return err } + if Verbose { + fmt.Printf("%s | CACHE: Imported %d entries in %s\n", + time.Now().Format(logDate), + length, + time.Since(startTime), + ) + } + return nil } func serveCacheClear(args []string, errorChannel chan<- error) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - list, err := fileList(args, &filters{}, "") + cache.mutex.Lock() + cache.list = nil + cache.mutex.Unlock() + + _, err := fileList(args, &filters{}, "") if err != nil { errorChannel <- err return } - cache.set(list) - w.Header().Set("Content-Type", "text/plain") w.Write([]byte("Ok")) @@ -150,24 +177,6 @@ func serveCacheClear(args []string, errorChannel chan<- error) httprouter.Handle } func registerCacheHandlers(mux *httprouter.Router, args []string, errorChannel chan<- error) error { - skipIndex := false - - if CacheFile != "" { - err := cache.Import(CacheFile) - if err == nil { - skipIndex = true - } - } - - if !skipIndex { - list, err := fileList(args, &filters{}, "") - if err != nil { - return err - } - - cache.set(list) - } - register(mux, Prefix+"/clear_cache", serveCacheClear(args, errorChannel)) return nil diff --git a/cmd/favicons.go b/cmd/favicons.go index f04af23..5350529 100644 --- a/cmd/favicons.go +++ b/cmd/favicons.go @@ -27,14 +27,12 @@ const ( ` ) -func serveFavicons(errorChannel chan<- error) httprouter.Handle { +func serveFavicons() httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { fname := strings.TrimPrefix(r.URL.Path, "/") data, err := favicons.ReadFile(fname) if err != nil { - errorChannel <- err - return } diff --git a/cmd/root.go b/cmd/root.go index 5b3ca54..f282a3c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) const ( - ReleaseVersion string = "0.95.0" + ReleaseVersion string = "0.95.1" ) var ( diff --git a/cmd/web.go b/cmd/web.go index bc18a47..0c3b4f9 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -436,6 +436,22 @@ func ServePage(args []string) error { return ErrNoMediaFound } + skipIndex := false + + if CacheFile != "" { + err := cache.Import(CacheFile) + if err == nil { + skipIndex = true + } + } + + if !skipIndex { + _, err := fileList(args, &filters{}, "") + if err != nil { + return err + } + } + mux := httprouter.New() listenHost := net.JoinHostPort(Bind, strconv.Itoa(Port)) @@ -471,9 +487,9 @@ func ServePage(args []string) error { register(mux, "/", redirectRoot()) } - register(mux, Prefix+"/favicons/*favicon", serveFavicons(errorChannel)) + register(mux, Prefix+"/favicons/*favicon", serveFavicons()) - register(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel)) + register(mux, Prefix+"/favicon.ico", serveFavicons()) register(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, errorChannel))